Unity Canvas UI line Renderer

来源:互联网 发布:大学 知乎 编辑:程序博客网 时间:2024/05/24 00:59
  1. // from http://forum.unity3d.com/threads/new-ui-and-line-drawing.253772/
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5.  
  6. public class UILineRenderer : Graphic
  7. {
  8.     [SerializeField] Texture m_Texture;
  9.     [SerializeField] Rect m_UVRect = new Rect(0f, 0f, 1f, 1f);
  10.  
  11.     public float LineThickness = 2;
  12.     public bool UseMargins;
  13.     public Vector2 Margin;
  14.     public Vector2[] Points;
  15.     public bool relativeSize;
  16.  
  17.     public override Texture mainTexture
  18.     {
  19.         get
  20.         {
  21.             return m_Texture == null ? s_WhiteTexture : m_Texture;
  22.         }
  23.     }
  24.  
  25.     /// <summary>
  26.     /// Texture to be used.
  27.     /// </summary>
  28.     public Texture texture
  29.     {
  30.         get
  31.         {
  32.             return m_Texture;
  33.         }
  34.         set
  35.         {
  36.             if (m_Texture == value)
  37.                 return;
  38.  
  39.             m_Texture = value;
  40.             SetVerticesDirty();
  41.             SetMaterialDirty();
  42.         }
  43.     }
  44.  
  45.     /// <summary>
  46.     /// UV rectangle used by the texture.
  47.     /// </summary>
  48.     public Rect uvRect
  49.     {
  50.         get
  51.         {
  52.             return m_UVRect;
  53.         }
  54.         set
  55.         {
  56.             if (m_UVRect == value)
  57.                 return;
  58.             m_UVRect = value;
  59.             SetVerticesDirty();
  60.         }
  61.     }
  62.  
  63.     //protected override void OnFillVBO(List<UIVertex> vbo)
  64.     protected override void OnPopulateMesh(Mesh toFill)
  65.     {
  66.         // requires sets of quads
  67.         if (Points == null || Points.Length < 2)
  68.             Points = new[] { new Vector2(00)new Vector2(11) };
  69.         var capSize = 24;
  70.         var sizeX = rectTransform.rect.width;
  71.         var sizeY = rectTransform.rect.height;
  72.         var offsetX = -rectTransform.pivot.x * rectTransform.rect.width;
  73.         var offsetY = -rectTransform.pivot.y * rectTransform.rect.height;
  74.  
  75.         // don't want to scale based on the size of the rect, so this is switchable now
  76.         if (!relativeSize)
  77.         {
  78.             sizeX = 1;
  79.             sizeY = 1;
  80.         }
  81.         // build a new set of points taking into account the cap sizes.
  82.         // would be cool to support corners too, but that might be a bit tough :)
  83.         var pointList = new List<Vector2> ();
  84.         pointList.Add (Points [0]);
  85.         var capPoint = Points [0] + (Points [1] - Points [0]).normalized * capSize;
  86.         pointList.Add (capPoint);
  87.  
  88.         // should bail before the last point to add another cap point
  89.         for (int i = 1; i < Points.Length-1; i++)
  90.         {
  91.             pointList.Add (Points [i]);
  92.         }
  93.         capPoint = Points [Points.Length-1] - (Points [Points.Length-1] - Points [Points.Length-2]).normalized * capSize;
  94.         pointList.Add (capPoint);
  95.         pointList.Add (Points [Points.Length - 1]);
  96.  
  97.         var TempPoints = pointList.ToArray ();
  98.         if (UseMargins)
  99.         {
  100.             sizeX -= Margin.x;
  101.             sizeY -= Margin.y;
  102.             offsetX += Margin.x/2f;
  103.             offsetY += Margin.y/2f;
  104.         }
  105.  
  106.         toFill.Clear();
  107.         var vbo = new VertexHelper(toFill);
  108.  
  109.         Vector2 prevV1 = Vector2.zero;
  110.         Vector2 prevV2 = Vector2.zero;
  111.  
  112.         for (int i = 1; i < TempPoints.Length; i++)
  113.         {
  114.             var prev = TempPoints[- 1];
  115.             var cur = TempPoints[i];
  116.             prev = new Vector2(prev.x * sizeX + offsetX, prev.y * sizeY + offsetY);
  117.             cur = new Vector2(cur.x * sizeX + offsetX, cur.y * sizeY + offsetY);
  118.  
  119.             float angle = Mathf.Atan2(cur.y - prev.y, cur.x - prev.x) * 180f / Mathf.PI;
  120.  
  121.             var v1 = prev + new Vector2(0-LineThickness / 2);
  122.             var v2 = prev + new Vector2(0+LineThickness / 2);
  123.             var v3 = cur + new Vector2(0+LineThickness / 2);
  124.             var v4 = cur + new Vector2(0-LineThickness / 2);
  125.  
  126.             v1 = RotatePointAroundPivot(v1, prev, new Vector3(00, angle));
  127.             v2 = RotatePointAroundPivot(v2, prev, new Vector3(00, angle));
  128.             v3 = RotatePointAroundPivot(v3, cur, new Vector3(00, angle));
  129.             v4 = RotatePointAroundPivot(v4, cur, new Vector3(00, angle));
  130.  
  131.             Vector2 uvTopLeft = Vector2.zero;
  132.             Vector2 uvBottomLeft = new Vector2(01);
  133.  
  134.             Vector2 uvTopCenter = new Vector2(0.5f, 0);
  135.             Vector2 uvBottomCenter = new Vector2(0.5f, 1);
  136.  
  137.             Vector2 uvTopRight = new Vector2(10);
  138.             Vector2 uvBottomRight = new Vector2(11);
  139.  
  140.             Vector2[] uvs = new[]{ uvTopCenter,uvBottomCenter,uvBottomCenter,uvTopCenter };
  141.  
  142.             if (> 1)
  143.                 SetVbo(vbo, new[] { prevV1, prevV2, v1, v2 }, uvs);
  144.  
  145.             if(i==1)
  146.                 uvs = new[]{ uvTopLeft,uvBottomLeft,uvBottomCenter,uvTopCenter };
  147.             else if(i==TempPoints.Length-1)
  148.                 uvs = new[]{uvTopCenter,uvBottomCenter, uvBottomRight, uvTopRight };
  149.  
  150.             //SetVbo(vbo, new[] { v1, v2, v3, v4 }, uvs, toFill);
  151.             vbo.AddUIVertexQuad(SetVbo(vbo, new[] { v1, v2, v3, v4 }, uvs));
  152.             vbo.FillMesh (toFill);
  153.  
  154.             prevV1 = v3;
  155.             prevV2 = v4;
  156.         }
  157.     }
  158.  
  159.     //protected void SetVbo(UIVertex vbo, Vector2[] vertices, Vector2[] uvs)
  160.     protected UIVertex[] SetVbo(VertexHelper vbo, Vector2[] verticesVector2[] uvs)
  161.     {
  162.         UIVertex[] VboVertices = new UIVertex[4];
  163.  
  164.         for (int i = 0; i < vertices.Length; i++)
  165.         {
  166.             var vert = UIVertex.simpleVert;
  167.             vert.color = color;
  168.             vert.position = vertices[i];
  169.             vert.uv0 = uvs [i];
  170.             VboVertices[i] = vert;
  171.         }
  172.  
  173.         return VboVertices;
  174.     }
  175.  
  176.     public Vector3 RotatePointAroundPivot(Vector3 pointVector3 pivot, Vector3 angles)
  177.     {
  178.         Vector3 dir = point - pivot; // get point direction relative to pivot
  179.         dir = Quaternion.Euler(angles)*dir; // rotate it
  180.         point = dir + pivot; // calculate rotated point
  181.         return point// return it
  182.     }
  183. }
原创粉丝点击