ProceduralMImage.cs 7.5 KB

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