ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Unity中 生成箭头(2D)

2021-05-28 14:35:26  阅读:327  来源: 互联网

标签:width vh Vector3 Mathf height 2D 箭头 Unity new


  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 }
View Code

 

标签:width,vh,Vector3,Mathf,height,2D,箭头,Unity,new
来源: https://www.cnblogs.com/nanyang0310/p/14822005.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有