AdvancedDrawingPad.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using SUIFW;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class AdvancedDrawingPad : MonoBehaviour
  8. {
  9. private GameObject clone;
  10. private LineRenderer line;
  11. private int i;
  12. private int index = 0;
  13. public static Material mat;
  14. public static bool canUse;
  15. public GameObject obs;
  16. public GameObject lineparent;
  17. private Vector3 startpos;
  18. public RectTransform canvas;
  19. public GameObject listColor;
  20. public Sprite[] spriteList;
  21. public Image img;
  22. int colorIndex = 1;
  23. public static AdvancedDrawingPad Instance;
  24. void Start()
  25. {
  26. Instance = this;
  27. }
  28. void Awake()
  29. {
  30. }
  31. public static bool isqianming;
  32. private void OnEnable()
  33. {
  34. isqianming=false;
  35. // 强制横屏并禁止
  36. // 自动旋转
  37. Screen.orientation = ScreenOrientation.LandscapeLeft;
  38. Screen.autorotateToLandscapeLeft = false;
  39. Screen.autorotateToLandscapeRight = false;
  40. Screen.autorotateToPortrait = false;
  41. Screen.autorotateToPortraitUpsideDown = false;
  42. canUse = true;
  43. listColor.SetActive(false);
  44. colorIndex = 1;
  45. }
  46. public void showAndCloseColor()
  47. {
  48. listColor.SetActive(!listColor.activeSelf);
  49. canUse = !listColor.activeSelf;
  50. }
  51. public static Action<Texture2D> callback;
  52. public void closejietu()
  53. {
  54. if(isqianming)
  55. {
  56. Rect rect = new Rect(0, 0, Screen.width, Screen.height);
  57. callback.Invoke(ScreenShot(Camera.main,rect));
  58. }
  59. }
  60. // <summary>
  61. /// 传入一个 RectTransform 区域!, 就不把资格区域的图像保存下来
  62. /// </summary>
  63. /// <param name="rect"></param>
  64. public Texture2D ScreenShot(Camera camera, Rect rect)
  65. {
  66. RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
  67. camera.targetTexture = rt;
  68. camera.Render();
  69. RenderTexture.active = rt;
  70. Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32, true);
  71. screenShot.ReadPixels(rect, 0, 125);
  72. screenShot.Apply();
  73. camera.targetTexture = null;
  74. RenderTexture.active = null;
  75. GameObject.Destroy(rt);
  76. //Debug.Log("截图成功");
  77. return screenShot;
  78. }
  79. void Update()
  80. {
  81. if (mat == null)
  82. {
  83. mat = new Material(Shader.Find("Shader/line"));
  84. }
  85. if (canUse)
  86. {
  87. if (Input.GetMouseButtonDown(0))
  88. {
  89. clone = (GameObject)Instantiate(obs, new Vector3(0, 0, this.transform.position.z), this.transform.rotation, lineparent.transform);//克隆一个带有LineRender的物体
  90. clone.name += index.ToString();
  91. line = clone.AddComponent<LineRenderer>();//获得该物体上的LineRender组件
  92. //line.material = new Material(Shader.Find("Shader/Additive"));
  93. line.material = new Material(Shader.Find("Sprites/Default"));
  94. switch(colorIndex)
  95. {
  96. case 1:
  97. mat.color = Color.black;
  98. break;
  99. case 2:
  100. mat.color = Color.black;
  101. break;
  102. case 3:
  103. mat.color = Color.black;
  104. break;
  105. case 4:
  106. mat.color = Color.black;
  107. break;
  108. default:
  109. mat.color = Color.black;
  110. break;
  111. }
  112. line.sortingOrder = 1;
  113. //设置颜色
  114. line.startColor = mat.color;
  115. line.endColor = mat.color;
  116. //设置宽度
  117. line.startWidth = 0.5f;
  118. line.endWidth = 0.5f;
  119. line.useWorldSpace = false;
  120. i = 0;
  121. index++;
  122. startpos = GetuiPos();
  123. }
  124. if (Input.GetMouseButton(0))
  125. {
  126. if (line != null)
  127. {
  128. if (Input.mousePosition.x < 0 || Input.mousePosition.x > Screen.width || Input.mousePosition.y < 0 || Input.mousePosition.y > Screen.height)
  129. {
  130. }
  131. listColor.SetActive(false);
  132. Vector3 pos = GetuiPos();
  133. if (Vector3.Distance(startpos, pos) > 0.5f)
  134. {
  135. i++;
  136. line.positionCount = i;
  137. line.SetPosition(i - 1, pos);//设置顶点位置 s
  138. }
  139. isqianming=true;
  140. }
  141. }
  142. if (Input.GetMouseButtonUp(0))
  143. {
  144. line = null;
  145. }
  146. }
  147. }
  148. public Vector2 GetuiPos()
  149. {
  150. Vector2 uisize = canvas.sizeDelta;//得到画布的尺寸
  151. Vector2 screenpos = Input.mousePosition;
  152. Vector2 screenpos2;
  153. screenpos2.x = screenpos.x - (Screen.width / 2);//转换为以屏幕中心为原点的屏幕坐标
  154. screenpos2.y = screenpos.y - (Screen.height / 2);
  155. Vector2 uipos;//UI坐标
  156. uipos.x = screenpos2.x * (uisize.x / Screen.width);//转换后的屏幕坐标*画布与屏幕宽高比
  157. uipos.y = screenpos2.y * (uisize.y / Screen.height);
  158. return uipos;
  159. }
  160. public void ClickPaint(int ci)
  161. {
  162. listColor.SetActive(false);
  163. colorIndex = ci;
  164. canUse = true;
  165. img.sprite = spriteList[ci - 1];
  166. }
  167. public void DeleteLine()
  168. {
  169. if (lineparent.transform.childCount > 0)
  170. {
  171. for (int i = 0; i < lineparent.transform.childCount; i++)
  172. {
  173. Destroy(lineparent.transform.GetChild(i).gameObject);
  174. }
  175. }
  176. line = null;
  177. }
  178. private void OnDisable()
  179. {
  180. // 强制横屏并禁止自动旋转
  181. Screen.orientation = ScreenOrientation.Portrait;
  182. Screen.autorotateToLandscapeLeft = false;
  183. Screen.autorotateToLandscapeRight = false;
  184. Screen.autorotateToPortrait = false;
  185. Screen.autorotateToPortraitUpsideDown = false;
  186. if (lineparent.transform.childCount > 0)
  187. {
  188. for (int i = 0; i < lineparent.transform.childCount; i++)
  189. {
  190. Destroy(lineparent.transform.GetChild(i).gameObject);
  191. }
  192. }
  193. }
  194. }