PaintLine.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using SUIFW;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class PaintLine : MonoBehaviour
  7. {
  8. private GameObject clone;
  9. private LineRenderer line;
  10. private int i;
  11. private int index = 0;
  12. public static Material mat;
  13. public static bool canUse;
  14. public GameObject obs;
  15. public GameObject lineparent;
  16. private Vector3 startpos;
  17. private RectTransform canvas;
  18. public GameObject listColor;
  19. public Sprite[] spriteList;
  20. public Image img;
  21. int colorIndex = 1;
  22. void Start()
  23. {
  24. if (UIManager.GetInstance()._CanvasTransform)
  25. {
  26. canvas = UIManager.GetInstance()._CanvasTransform.GetComponent<RectTransform>();
  27. }
  28. }
  29. private void OnEnable()
  30. {
  31. canUse = true;
  32. listColor.SetActive(false);
  33. colorIndex = 1;
  34. }
  35. public void showAndCloseColor()
  36. {
  37. listColor.SetActive(!listColor.activeSelf);
  38. canUse = !listColor.activeSelf;
  39. }
  40. void Update()
  41. {
  42. if (mat == null)
  43. {
  44. mat = new Material(Shader.Find("Shader/line"));
  45. }
  46. if (canUse)
  47. {
  48. if (Input.GetMouseButtonDown(0))
  49. {
  50. clone = (GameObject)Instantiate(obs, new Vector3(0, 0, this.transform.position.z), this.transform.rotation, lineparent.transform);//克隆一个带有LineRender的物体
  51. clone.name += index.ToString();
  52. line = clone.AddComponent<LineRenderer>();//获得该物体上的LineRender组件
  53. //line.material = new Material(Shader.Find("Shader/Additive"));
  54. line.material = new Material(Shader.Find("Sprites/Default"));
  55. switch(colorIndex)
  56. {
  57. case 1:
  58. mat.color = Color.red;
  59. break;
  60. case 2:
  61. mat.color = Color.blue;
  62. break;
  63. case 3:
  64. mat.color = Color.green;
  65. break;
  66. case 4:
  67. mat.color = Color.yellow;
  68. break;
  69. default:
  70. mat.color = Color.red;
  71. break;
  72. }
  73. line.sortingOrder = 1;
  74. //设置颜色
  75. line.startColor = mat.color;
  76. line.endColor = mat.color;
  77. //设置宽度
  78. line.startWidth = 0.5f;
  79. line.endWidth = 0.5f;
  80. line.useWorldSpace = false;
  81. i = 0;
  82. index++;
  83. startpos = GetuiPos();
  84. }
  85. if (Input.GetMouseButton(0))
  86. {
  87. if (line != null)
  88. {
  89. if (Input.mousePosition.x < 0 || Input.mousePosition.x > Screen.width || Input.mousePosition.y < 0 || Input.mousePosition.y > Screen.height)
  90. {
  91. Destroy(clone);
  92. return;
  93. }
  94. listColor.SetActive(false);
  95. Vector3 pos = GetuiPos();
  96. if (Vector3.Distance(startpos, pos) > 0.5f)
  97. {
  98. i++;
  99. line.positionCount = i;
  100. line.SetPosition(i - 1, pos);//设置顶点位置 s
  101. }
  102. }
  103. }
  104. if (Input.GetMouseButtonUp(0))
  105. {
  106. line = null;
  107. }
  108. }
  109. }
  110. public Vector2 GetuiPos()
  111. {
  112. Vector2 uisize = canvas.sizeDelta;//得到画布的尺寸
  113. Vector2 screenpos = Input.mousePosition;
  114. Vector2 screenpos2;
  115. screenpos2.x = screenpos.x - (Screen.width / 2);//转换为以屏幕中心为原点的屏幕坐标
  116. screenpos2.y = screenpos.y - (Screen.height / 2);
  117. Vector2 uipos;//UI坐标
  118. uipos.x = screenpos2.x * (uisize.x / Screen.width);//转换后的屏幕坐标*画布与屏幕宽高比
  119. uipos.y = screenpos2.y * (uisize.y / Screen.height);
  120. return uipos;
  121. }
  122. public void ClickPaint(int ci)
  123. {
  124. listColor.SetActive(false);
  125. colorIndex = ci;
  126. canUse = true;
  127. img.sprite = spriteList[ci - 1];
  128. }
  129. public void DeleteLine()
  130. {
  131. if (lineparent.transform.childCount > 0)
  132. {
  133. for (int i = 0; i < lineparent.transform.childCount; i++)
  134. {
  135. Destroy(lineparent.transform.GetChild(i).gameObject);
  136. }
  137. }
  138. line = null;
  139. }
  140. private void OnDisable()
  141. {
  142. if (lineparent.transform.childCount > 0)
  143. {
  144. for (int i = 0; i < lineparent.transform.childCount; i++)
  145. {
  146. Destroy(lineparent.transform.GetChild(i).gameObject);
  147. }
  148. }
  149. }
  150. }