using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using XFramework.UIExtensions; public class DrawLineCtrl : MaskableGraphic { //public List< Vector2> points; //public List colors; // public float segments; //public float linewidth; //非全屏时的修正值 全屏时可为0 public Vector3 anchorPos = new Vector3(0, 0, 0); List> vertexQuadList = new List>(); List vertexQuad; public float lineWidth = 2; Vector3 lastleftPoint; Vector3 lastrightPoint; Vector3 lastPos= Vector3.zero; protected override void OnPopulateMesh(VertexHelper vh) { vh.Clear(); for (int i = 0; i < vertexQuadList.Count; i++) { vh.AddUIVertexQuad(vertexQuadList[i].ToArray()); } } void Update() { if (Input.GetMouseButtonDown(0)) { // vertexQuadList.Clear(); if (lastPos == Vector3.zero) { lastPos = Input.mousePosition - anchorPos; lastleftPoint = lastPos - new Vector3(Screen.width / 2, Screen.height / 2, 0) + Vector3.up * lineWidth; lastrightPoint = lastPos - new Vector3(Screen.width / 2, Screen.height / 2, 0) - Vector3.up * lineWidth; } else { Vector3 newVec = Input.mousePosition - anchorPos - lastPos; int colorIndex = Random.Range(0, 2); if (colorIndex == 0) { color = Color.green; } else color = Color.red; if (newVec.magnitude < 0.1f) { return; } vertexQuad = new List(); Vector3 vec = Vector3.Cross(newVec.normalized, Vector3.forward).normalized; Vector3 newleftPoint = Input.mousePosition - anchorPos - new Vector3(Screen.width / 2, Screen.height / 2, 0) + vec * lineWidth; Vector3 newrightPoint = Input.mousePosition - anchorPos - new Vector3(Screen.width / 2, Screen.height / 2, 0) - vec * lineWidth; UIVertex uIVertex = new UIVertex(); uIVertex.position = lastleftPoint; uIVertex.color = color; vertexQuad.Add(uIVertex); UIVertex uIVertex1 = new UIVertex(); uIVertex1.position = lastrightPoint; uIVertex1.color = color; vertexQuad.Add(uIVertex1); UIVertex uIVertex3 = new UIVertex(); uIVertex3.position = newrightPoint; uIVertex3.color = color; vertexQuad.Add(uIVertex3); UIVertex uIVertex2 = new UIVertex(); uIVertex2.position = newleftPoint; uIVertex2.color = color; vertexQuad.Add(uIVertex2); lastleftPoint = newleftPoint; lastrightPoint = newrightPoint; vertexQuadList.Add(vertexQuad); lastPos = Input.mousePosition - anchorPos; SetVerticesDirty(); } } if (Input.GetMouseButtonDown(1)) { lastPos = Vector3.zero; vertexQuadList.Clear(); SetVerticesDirty(); } } }