Unity中 生成箭头(2D)
1 /* 2 /// 功能: 绘制箭头 3 /// 时间: 4 /// 版本: 5 */ 6 7 using System.Collections; 8 using System.Collections.Generic; 9 using UnityEngine; 10 using UnityEngine.UI; 11 12 [AddComponentMenu("UI/Effects/Arrow")] 13 [RequireComponent(typeof(Image))] 14 [ExecuteInEditMode] 15 public class Arrow : BaseMeshEffect 16 { 17 public enum DirectionType 18 { 19 Left, 20 Right, 21 Up, 22 Down, 23 } 24 25 public DirectionType directionType; 26 27 public float width = 1; 28 public float height = 1; 29 public float angle; 30 public Color color; 31 32 private Image image; 33 34 protected override void Start() 35 { 36 image = this.GetComponent<Image>(); 37 } 38 39 public override void ModifyMesh(VertexHelper vh) 40 { 41 vh.Clear(); 42 43 List<Vector3> vertexList = new List<Vector3>(); 44 Vector3 v0=Vector3.zero; 45 Vector3 v1=Vector3.zero; 46 Vector3 v2=Vector3.zero; 47 Vector3 v3=Vector3.zero; 48 Vector3 v4 = Vector3.zero; 49 50 //有5个点" 51 switch (directionType) 52 { 53 case DirectionType.Left: 54 v1 = new Vector3(-width, 0); 55 v2 = new Vector3(-width - height / Mathf.Tan(angle * Mathf.PI / 180), height / 2); 56 v3 = new Vector3(-width, height); 57 v4 = new Vector3(0, height); 58 break; 59 case DirectionType.Right: 60 v1 = new Vector3(width, 0); 61 v2 = new Vector3(width + height / Mathf.Tan(angle * Mathf.PI / 180), height / 2); 62 v3 = new Vector3(width, height); 63 v4 = new Vector3(0, height); 64 break; 65 case DirectionType.Up: 66 v1 = new Vector3(width, 0); 67 v2 = new Vector3(width,height); 68 v3 = new Vector3(width/2, height+height/Mathf.Tan(angle*Mathf.PI/180)); 69 v4 = new Vector3(0, height); 70 break; 71 case DirectionType.Down: 72 v1 = new Vector3(width, 0); 73 v2 = new Vector3(width, -height); 74 v3 = new Vector3(width / 2, -height - height / Mathf.Tan(angle * Mathf.PI / 180)); 75 v4 = new Vector3(0, -height); 76 break; 77 default: 78 break; 79 } 80 81 vertexList.Add(v0, v1, v2, v3, v4); 82 83 for (int i = 0; i < vertexList.Count; i++) 84 { 85 vh.AddVert(vertexList[i], color, Vector3.zero); 86 } 87 88 vh.AddTriangle(0, 1, 2); 89 vh.AddTriangle(0, 1, 3); 90 vh.AddTriangle(1, 2, 3); 91 vh.AddTriangle(2, 3, 4); 92 vh.AddTriangle(3, 4, 0); 93 vh.AddTriangle(0, 1, 4); 94 } 95 96 private void Update() 97 { 98 image.SetVerticesDirty(); 99 } 100 }