ProceduralYuanImage.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Sprites;
  4. using UnityEngine.UI;
  5. /* Author: Josh H.
  6. * Procedural UI Image
  7. * assetstore.joshh@gmail.com for feedback or questions
  8. */
  9. namespace UnityEngine.UI {
  10. public class ProceduralYuanImage : RawImage {
  11. protected override void OnPopulateMesh(VertexHelper toFill)
  12. {
  13. //note: Sliced and Tiled have no effect to this currently.
  14. GenerateSimpleSprite(toFill);
  15. }
  16. private Vector4 GetDrawingDimensions(bool shouldPreserveAspect)
  17. {
  18. var padding = sprite == null ? Vector4.zero : DataUtility.GetPadding(sprite);
  19. Rect r = GetPixelAdjustedRect();
  20. var size = sprite == null ? new Vector2(r.width, r.height) : new Vector2(sprite.rect.width, sprite.rect.height);
  21. //Debug.Log(string.Format("r:{2}, size:{0}, padding:{1}", size, padding, r));
  22. int spriteW = Mathf.RoundToInt(size.x);
  23. int spriteH = Mathf.RoundToInt(size.y);
  24. if (shouldPreserveAspect && size.sqrMagnitude > 0.0f)
  25. {
  26. var spriteRatio = size.x / size.y;
  27. var rectRatio = r.width / r.height;
  28. if (spriteRatio > rectRatio)
  29. {
  30. var oldHeight = r.height;
  31. r.height = r.width * (1.0f / spriteRatio);
  32. r.y += (oldHeight - r.height) * rectTransform.pivot.y;
  33. }
  34. else
  35. {
  36. var oldWidth = r.width;
  37. r.width = r.height * spriteRatio;
  38. r.x += (oldWidth - r.width) * rectTransform.pivot.x;
  39. }
  40. }
  41. var v = new Vector4(
  42. padding.x / spriteW,
  43. padding.y / spriteH,
  44. (spriteW - padding.z) / spriteW,
  45. (spriteH - padding.w) / spriteH);
  46. v = new Vector4(
  47. r.x + r.width * v.x,
  48. r.y + r.height * v.y,
  49. r.x + r.width * v.z,
  50. r.y + r.height * v.w
  51. );
  52. return v;
  53. }
  54. //每个角最大的三角形数,一般5-8个就有不错的圆角效果,设置Max防止不必要的性能浪费
  55. const int MaxTriangleNum = 20;
  56. const int MinTriangleNum = 1;
  57. float Radius=120;
  58. //使用几个三角形去填充每个角的四分之一圆
  59. [Range(MinTriangleNum, MaxTriangleNum)]
  60. int TriangleNum=10;
  61. private Texture2D TextureToTexture2D(Texture texture)
  62. {
  63. Texture2D texture2d = texture as Texture2D;
  64. return texture2d;
  65. }
  66. static Sprite sprite;
  67. private VertexHelper nvh;
  68. /// <summary>
  69. /// Generates the Verticies needed.
  70. /// </summary>
  71. /// <param name="vh">vertex helper</param>
  72. void GenerateSimpleSprite(VertexHelper vh){
  73. if(texture!=null&&sprite==null)
  74. {
  75. sprite = Sprite.Create(TextureToTexture2D(texture), new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  76. }
  77. Vector4 v = GetDrawingDimensions(false);
  78. Vector4 uv = sprite != null ? DataUtility.GetOuterUV(sprite) : Vector4.zero;
  79. var color32 = color;
  80. vh.Clear();
  81. //对radius的值做限制,必须在0-较小的边的1/2的范围内
  82. float radius = Radius;
  83. if (radius > (v.z - v.x) / 2) radius = (v.z - v.x) / 2;
  84. if (radius > (v.w - v.y) / 2) radius = (v.w - v.y) / 2;
  85. if (radius < 0) radius = 0;
  86. //计算出uv中对应的半径值坐标轴的半径
  87. float uvRadiusX = radius / (v.z - v.x);
  88. float uvRadiusY = radius / (v.w - v.y);
  89. //0,1
  90. vh.AddVert(new Vector3(v.x, v.w - radius), color32, new Vector2(uv.x, uv.w - uvRadiusY));
  91. vh.AddVert(new Vector3(v.x, v.y + radius), color32, new Vector2(uv.x, uv.y + uvRadiusY));
  92. //2,3,4,5
  93. vh.AddVert(new Vector3(v.x + radius, v.w), color32, new Vector2(uv.x + uvRadiusX, uv.w));
  94. vh.AddVert(new Vector3(v.x + radius, v.w - radius), color32, new Vector2(uv.x + uvRadiusX, uv.w - uvRadiusY));
  95. vh.AddVert(new Vector3(v.x + radius, v.y + radius), color32, new Vector2(uv.x + uvRadiusX, uv.y + uvRadiusY));
  96. vh.AddVert(new Vector3(v.x + radius, v.y), color32, new Vector2(uv.x + uvRadiusX, uv.y));
  97. //6,7,8,9
  98. vh.AddVert(new Vector3(v.z - radius, v.w), color32, new Vector2(uv.z - uvRadiusX, uv.w));
  99. vh.AddVert(new Vector3(v.z - radius, v.w - radius), color32, new Vector2(uv.z - uvRadiusX, uv.w - uvRadiusY));
  100. vh.AddVert(new Vector3(v.z - radius, v.y + radius), color32, new Vector2(uv.z - uvRadiusX, uv.y + uvRadiusY));
  101. vh.AddVert(new Vector3(v.z - radius, v.y), color32, new Vector2(uv.z - uvRadiusX, uv.y));
  102. //10,11
  103. vh.AddVert(new Vector3(v.z, v.w - radius), color32, new Vector2(uv.z, uv.w - uvRadiusY));
  104. vh.AddVert(new Vector3(v.z, v.y + radius), color32, new Vector2(uv.z, uv.y + uvRadiusY));
  105. //左边的矩形
  106. vh.AddTriangle(1, 0, 3);
  107. vh.AddTriangle(1, 3, 4);
  108. //中间的矩形
  109. vh.AddTriangle(5, 2, 6);
  110. vh.AddTriangle(5, 6, 9);
  111. //右边的矩形
  112. vh.AddTriangle(8, 7, 10);
  113. vh.AddTriangle(8, 10, 11);
  114. //开始构造四个角
  115. List<Vector2> vCenterList = new List<Vector2>();
  116. List<Vector2> uvCenterList = new List<Vector2>();
  117. List<int> vCenterVertList = new List<int>();
  118. //右上角的圆心
  119. vCenterList.Add(new Vector2(v.z - radius, v.w - radius));
  120. uvCenterList.Add(new Vector2(uv.z - uvRadiusX, uv.w - uvRadiusY));
  121. vCenterVertList.Add(7);
  122. //左上角的圆心
  123. vCenterList.Add(new Vector2(v.x + radius, v.w - radius));
  124. uvCenterList.Add(new Vector2(uv.x + uvRadiusX, uv.w - uvRadiusY));
  125. vCenterVertList.Add(3);
  126. //左下角的圆心
  127. vCenterList.Add(new Vector2(v.x + radius, v.y + radius));
  128. uvCenterList.Add(new Vector2(uv.x + uvRadiusX, uv.y + uvRadiusY));
  129. vCenterVertList.Add(4);
  130. //右下角的圆心
  131. vCenterList.Add(new Vector2(v.z - radius, v.y + radius));
  132. uvCenterList.Add(new Vector2(uv.z - uvRadiusX, uv.y + uvRadiusY));
  133. vCenterVertList.Add(8);
  134. //每个三角形的顶角
  135. float degreeDelta = (float)(Mathf.PI / 2 / TriangleNum);
  136. //当前的角度
  137. float curDegree = 0;
  138. for (int i = 0; i < vCenterVertList.Count; i++)
  139. {
  140. int preVertNum = vh.currentVertCount;
  141. for (int j = 0; j <= TriangleNum; j++)
  142. {
  143. float cosA = Mathf.Cos(curDegree);
  144. float sinA = Mathf.Sin(curDegree);
  145. Vector3 vPosition = new Vector3(vCenterList[i].x + cosA * radius, vCenterList[i].y + sinA * radius);
  146. Vector3 uvPosition = new Vector2(uvCenterList[i].x + cosA * uvRadiusX, uvCenterList[i].y + sinA * uvRadiusY);
  147. vh.AddVert(vPosition, color32, uvPosition);
  148. curDegree += degreeDelta;
  149. }
  150. curDegree -= degreeDelta;
  151. for (int j = 0; j <= TriangleNum - 1; j++)
  152. {
  153. vh.AddTriangle(vCenterVertList[i], preVertNum + j + 1, preVertNum + j);
  154. }
  155. }
  156. }
  157. }
  158. }