当前位置:   article > 正文

Unity完美的UI绘画工具_unity ui 绘图

unity ui 绘图

直接将代码放在RawImage上(不能放在Image上面)

包含了撤销和清除功能,两个功能也可以联动

需要注意revocations变量不能等于1或者0,否则会进入死循环

用起来如果有卡顿,就改变RawImage的Scale值,Scale越大绘画越流畅

文中用到的GameManager在这篇文章:Unity学习笔记:在GameManager里记录手游操作框架_努力长头发的程序猿的博客-CSDN博客

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. public class PaintingGraffiti : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
  7. {
  8. public Color color;
  9. //radius的值不能等于1和0,不然会进入死循环
  10. public int radius = 5;
  11. private Canvas canvas;
  12. private RawImage mainImage;
  13. private RectTransform mainRect;
  14. private Texture2D texture;
  15. private bool handIn;
  16. private Vector2 lastPos;
  17. private Color backgroundColor = new Color(1,1,1,0);
  18. List<Dictionary<Vector2,Color>> revocations = new List<Dictionary<Vector2,Color>>();
  19. public Action<float> setColorFill = fill => { };
  20. private float setNow = 0, setMax = 0;
  21. public bool isClose;
  22. public float getFill
  23. {
  24. get { return setNow / setMax; }
  25. }
  26. void Start()
  27. {
  28. canvas = transform.root.GetComponent<Canvas>();
  29. mainImage = GetComponent<RawImage>();
  30. mainRect = mainImage.rectTransform;
  31. texture = new Texture2D((int)mainRect.sizeDelta.x,(int)mainRect.sizeDelta.y);
  32. for (int i = 0; i < texture.width; i++)
  33. {
  34. for (int j = 0; j < texture.height; j++)
  35. {
  36. texture.SetPixel(i,j,backgroundColor);
  37. setMax++;
  38. }
  39. }
  40. setMax = setMax * 0.9f;
  41. texture.Apply();
  42. mainImage.texture = texture;
  43. mainImage.SetNativeSize();
  44. }
  45. private void Update()
  46. {
  47. if(!handIn || isClose){return;}
  48. if (GameManager.Main.GetClick())
  49. {
  50. revocations.Insert(0,new Dictionary<Vector2, Color>());
  51. }
  52. if (GameManager.Main.GetClickAlways())
  53. {
  54. Vector2 pos = GetPosForTexture();
  55. if (lastPos.x == 0 && lastPos.y == 0 || Vector2.Distance(pos,lastPos) <= radius/2)
  56. {
  57. DrawPoint((int)pos.x,(int)pos.y);
  58. }
  59. else
  60. {
  61. DrawLine((int)lastPos.x,(int)lastPos.y,(int)pos.x,(int)pos.y);
  62. }
  63. lastPos = pos;
  64. }
  65. if (GameManager.Main.GetClickUp())
  66. {
  67. lastPos = Vector2.zero;
  68. }
  69. if (Input.GetKeyDown(KeyCode.Z))
  70. {
  71. Revocation();
  72. }
  73. if (Input.GetKeyDown(KeyCode.C))
  74. {
  75. ClearAll();
  76. }
  77. }
  78. Vector2 GetPosForTexture()
  79. {
  80. Vector2 pos = Vector2.zero;
  81. RectTransformUtility.ScreenPointToLocalPointInRectangle
  82. (mainImage.rectTransform,GameManager.Main.GetNumPos(), canvas.worldCamera, out pos);
  83. pos.x += mainRect.sizeDelta.x * 0.5f;
  84. pos.y += mainRect.sizeDelta.y * 0.5f;
  85. return pos;
  86. }
  87. void DrawLine(int startX, int startY, int endX, int endY)
  88. {
  89. float k = ((float)endY - startY) / ((float)endX - startX);
  90. int b = startY - (int)(startX*k);
  91. int minX = startX < endX ? startX : endX;
  92. int maxX = startX > endX ? startX : endX;
  93. int minY = startY < endY ? startY : endY;
  94. int maxY = startY > endY ? startY : endY;
  95. if (maxY - minY > maxX - minX)
  96. {
  97. for (int i = 0; i <= maxY - minY; i+=radius/2)
  98. {
  99. if (endX == startX)
  100. {
  101. DrawPoint(minX,minY + i);
  102. }
  103. else
  104. {
  105. DrawPoint((int)(((minY + i) - b) / k),minY + i);
  106. }
  107. }
  108. }
  109. else
  110. {
  111. for (int i = 0; i <= maxX - minX; i+=radius/2)
  112. {
  113. DrawPoint(minX + i,b + (int)((minX + i) * k));
  114. }
  115. }
  116. }
  117. void DrawPoint(int x,int y)
  118. {
  119. int setY = 0;
  120. for (int i = -radius + x; i <= radius + x; i++)
  121. {
  122. setY = (radius * radius) - ((i - x) * (i - x));
  123. setY = (int) (Math.Sqrt(setY)) + y;
  124. for (int j = y - (setY - y); j <= setY; j++)
  125. {
  126. if(i<0||i>texture.width||j<0||j>texture.height){break;}
  127. if (texture.GetPixel(i, j) != color)
  128. {
  129. setNow++;
  130. setColorFill(setNow / setMax);
  131. if (!revocations[0].ContainsKey(new Vector2(i, j)))
  132. {
  133. revocations[0].Add(new Vector2(i,j),texture.GetPixel(i,j));
  134. }
  135. }
  136. texture.SetPixel(i,j,color);
  137. }
  138. }
  139. SetTexture();
  140. }
  141. void SetTexture()
  142. {
  143. texture.Apply();
  144. mainImage.texture = texture;
  145. }
  146. void Revocation()
  147. {
  148. if(revocations.Count == 0){return;}
  149. foreach (var pos in revocations[0])
  150. {
  151. texture.SetPixel((int)pos.Key.x,(int)pos.Key.y,pos.Value);
  152. }
  153. revocations.RemoveAt(0);
  154. SetTexture();
  155. }
  156. void ClearAll()
  157. {
  158. revocations.Insert(0,new Dictionary<Vector2, Color>());
  159. for (int i = 0; i <= texture.width; i++)
  160. {
  161. for (int j = 0; j <= texture.height; j++)
  162. {
  163. if (texture.GetPixel(i, j) != backgroundColor)
  164. {
  165. revocations[0].Add(new Vector2(i,j), texture.GetPixel(i,j));
  166. }
  167. texture.SetPixel(i, j, backgroundColor);
  168. }
  169. }
  170. if (revocations[0].Count == 0)
  171. {
  172. revocations.RemoveAt(0);
  173. }
  174. SetTexture();
  175. }
  176. public void OnPointerEnter(PointerEventData eventData)
  177. {
  178. handIn = true;
  179. }
  180. public void OnPointerExit(PointerEventData eventData)
  181. {
  182. handIn = false;
  183. }
  184. public Texture2D GetTexture()
  185. {
  186. return texture;
  187. }
  188. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/117069
推荐阅读
相关标签
  

闽ICP备14008679号