using SUIFW; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PaintLine : MonoBehaviour { private GameObject clone; private LineRenderer line; private int i; private int index = 0; public static Material mat; public static bool canUse; public GameObject obs; public GameObject lineparent; private Vector3 startpos; private RectTransform canvas; public GameObject listColor; public Sprite[] spriteList; public Image img; int colorIndex = 1; void Start() { if (UIManager.GetInstance()._CanvasTransform) { canvas = UIManager.GetInstance()._CanvasTransform.GetComponent(); } } private void OnEnable() { canUse = true; listColor.SetActive(false); colorIndex = 1; } public void showAndCloseColor() { listColor.SetActive(!listColor.activeSelf); canUse = !listColor.activeSelf; } void Update() { if (mat == null) { mat = new Material(Shader.Find("Shader/line")); } if (canUse) { if (Input.GetMouseButtonDown(0)) { clone = (GameObject)Instantiate(obs, new Vector3(0, 0, this.transform.position.z), this.transform.rotation, lineparent.transform);//克隆一个带有LineRender的物体 clone.name += index.ToString(); line = clone.AddComponent();//获得该物体上的LineRender组件 //line.material = new Material(Shader.Find("Shader/Additive")); line.material = new Material(Shader.Find("Sprites/Default")); switch(colorIndex) { case 1: mat.color = Color.red; break; case 2: mat.color = Color.blue; break; case 3: mat.color = Color.green; break; case 4: mat.color = Color.yellow; break; default: mat.color = Color.red; break; } line.sortingOrder = 1; //设置颜色 line.startColor = mat.color; line.endColor = mat.color; //设置宽度 line.startWidth = 0.5f; line.endWidth = 0.5f; line.useWorldSpace = false; i = 0; index++; startpos = GetuiPos(); } if (Input.GetMouseButton(0)) { if (line != null) { if (Input.mousePosition.x < 0 || Input.mousePosition.x > Screen.width || Input.mousePosition.y < 0 || Input.mousePosition.y > Screen.height) { Destroy(clone); return; } listColor.SetActive(false); Vector3 pos = GetuiPos(); if (Vector3.Distance(startpos, pos) > 0.5f) { i++; line.positionCount = i; line.SetPosition(i - 1, pos);//设置顶点位置 s } } } if (Input.GetMouseButtonUp(0)) { line = null; } } } public Vector2 GetuiPos() { Vector2 uisize = canvas.sizeDelta;//得到画布的尺寸 Vector2 screenpos = Input.mousePosition; Vector2 screenpos2; screenpos2.x = screenpos.x - (Screen.width / 2);//转换为以屏幕中心为原点的屏幕坐标 screenpos2.y = screenpos.y - (Screen.height / 2); Vector2 uipos;//UI坐标 uipos.x = screenpos2.x * (uisize.x / Screen.width);//转换后的屏幕坐标*画布与屏幕宽高比 uipos.y = screenpos2.y * (uisize.y / Screen.height); return uipos; } public void ClickPaint(int ci) { listColor.SetActive(false); colorIndex = ci; canUse = true; img.sprite = spriteList[ci - 1]; } public void DeleteLine() { if (lineparent.transform.childCount > 0) { for (int i = 0; i < lineparent.transform.childCount; i++) { Destroy(lineparent.transform.GetChild(i).gameObject); } } line = null; } private void OnDisable() { if (lineparent.transform.childCount > 0) { for (int i = 0; i < lineparent.transform.childCount; i++) { Destroy(lineparent.transform.GetChild(i).gameObject); } } } }