BatchQuadsGraphics.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using CommonUI.Gemo;
  2. using CommonUI_Unity3D.Impl;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace CommonUnity3D.UGUI
  10. {
  11. public class BatchQuadsGraphics : UnityEngine.UI.MaskableGraphic
  12. {
  13. private DisplayNode mBinding;
  14. private Texture2D mTexture;
  15. private CommonUI.Cell.CPJAtlas mAtlas;
  16. CommonUI_Unity3D.Impl.UnityImage mSrc;
  17. private List<BatchImageQuad> mQuads = new List<BatchImageQuad>();
  18. public override Texture mainTexture { get { return mTexture; } }
  19. public DisplayNode Binding { get { return mBinding; } }
  20. public CommonUI.Cell.CPJAtlas Atlas
  21. {
  22. get { return mAtlas; }
  23. set
  24. {
  25. mSrc = value.GetTile(0) as CommonUI_Unity3D.Impl.UnityImage;
  26. this.mAtlas = value;
  27. this.mTexture = mSrc.Texture2D;
  28. this.material = mSrc.TextureMaterial;
  29. base.SetAllDirty();
  30. }
  31. }
  32. public BatchImageQuad[] Quads
  33. {
  34. get { return mQuads.ToArray(); }
  35. }
  36. public int QuadsCount
  37. {
  38. get { return mQuads.Count; }
  39. }
  40. public BatchImageQuad GetQuad(int i)
  41. {
  42. return mQuads[i];
  43. }
  44. public void SetQuadVisible(int i, bool visible)
  45. {
  46. if (i >= 0 && i < mQuads.Count)
  47. {
  48. var b = mQuads[i];
  49. if (b.visible != visible)
  50. {
  51. b.visible = visible;
  52. mQuads[i] = b;
  53. base.SetVerticesDirty();
  54. }
  55. }
  56. }
  57. public void SetQuad(int i, BatchImageQuad quad)
  58. {
  59. if (i >= 0 && i < mQuads.Count)
  60. {
  61. mQuads[i] = quad;
  62. base.SetVerticesDirty();
  63. }
  64. }
  65. public int AddQuad(BatchImageQuad qd)
  66. {
  67. int ret = mQuads.Count;
  68. mQuads.Add(qd);
  69. base.SetVerticesDirty();
  70. return ret;
  71. }
  72. public int AddQuad(int index)
  73. {
  74. int ret = mQuads.Count;
  75. mQuads.Add(new BatchImageQuad(index));
  76. base.SetVerticesDirty();
  77. return ret;
  78. }
  79. public int AddQuad(int index, CommonUI.Data.ImageAnchor anchor, Vector2 pivot, Vector2 offset, Vector2 scale, float rotate)
  80. {
  81. int ret = mQuads.Count;
  82. var qd = new BatchImageQuad(index);
  83. qd.anchor = anchor;
  84. qd.pivot = pivot;
  85. qd.offset = offset;
  86. qd.scale = scale;
  87. qd.rotate = rotate;
  88. mQuads.Add(qd);
  89. base.SetVerticesDirty();
  90. return ret;
  91. }
  92. public int AddQuadByKey(string key, CommonUI.Data.ImageAnchor anchor, Vector2 pivot, Vector2 offset, Vector2 scale, float rotate)
  93. {
  94. int index = mAtlas.GetIndexByKey(key);
  95. if (index >= 0)
  96. {
  97. return AddQuad(index, anchor, pivot, offset, scale, rotate);
  98. }
  99. return index;
  100. }
  101. public int AddQuadImageFont(string text)
  102. {
  103. int ret = mQuads.Count;
  104. var qd = new BatchImageQuad(text);
  105. mQuads.Add(qd);
  106. base.SetVerticesDirty();
  107. return ret;
  108. }
  109. public int AddQuadImageFont(string text, CommonUI.Data.ImageAnchor anchor, Vector2 pivot, Vector2 offset, Vector2 scale, float rotate)
  110. {
  111. int ret = mQuads.Count;
  112. var qd = new BatchImageQuad(text);
  113. qd.anchor = anchor;
  114. qd.pivot = pivot;
  115. qd.offset = offset;
  116. qd.scale = scale;
  117. qd.rotate = rotate;
  118. mQuads.Add(qd);
  119. base.SetVerticesDirty();
  120. return ret;
  121. }
  122. public void ClearQuads()
  123. {
  124. if (mQuads.Count > 0)
  125. {
  126. mQuads.Clear();
  127. base.SetVerticesDirty();
  128. }
  129. }
  130. public BatchQuadsGraphics()
  131. {
  132. this.material = ImageGraphics.DefaultImageMaterial;
  133. }
  134. protected override void Start()
  135. {
  136. this.mBinding = DisplayNode.AsDisplayNode(gameObject);
  137. base.Start();
  138. }
  139. protected override void OnPopulateMesh(VertexHelper vh)
  140. {
  141. vh.Clear();
  142. if (mAtlas != null)
  143. {
  144. uint rgba = UIUtils.Color_To_UInt32_RGBA(base.color);
  145. using (VertexHelperBuffer vertex = VertexHelperBuffer.AllocAutoRelease(vh))
  146. {
  147. for (int i = 0; i < mQuads.Count; ++i)
  148. {
  149. var batch = mQuads[i];
  150. if (batch.visible)
  151. {
  152. vertex.pushTransform();
  153. if (!string.IsNullOrEmpty(batch.text))
  154. {
  155. OnPopulateText(vertex, ref batch, rgba);
  156. }
  157. else if (batch.index >= 0)
  158. {
  159. OnPopulateTile(vertex, ref batch, rgba);
  160. }
  161. vertex.popTransform();
  162. }
  163. }
  164. }
  165. }
  166. }
  167. protected virtual void OnPopulateTile(VertexHelperBuffer vertex, ref BatchImageQuad batch, uint rgba)
  168. {
  169. var rect = new Rect(0, 0, mAtlas.getWidth(batch.index), mAtlas.getHeight(batch.index));
  170. var pivot = new Vector2(rect.width * batch.pivot.x, rect.height * batch.pivot.y);
  171. UIUtils.AdjustAnchor(batch.anchor, rectTransform.sizeDelta, ref rect);
  172. vertex.translate(rect.x + pivot.x + batch.offset.x, rect.y + pivot.y + batch.offset.y);
  173. vertex.scale(batch.scale.x, batch.scale.y);
  174. vertex.rotate(batch.rotate);
  175. mAtlas.addVertex(vertex, batch.index, -pivot.x, -pivot.y, 0, rgba);
  176. }
  177. protected virtual void OnPopulateText(VertexHelperBuffer vertex, ref BatchImageQuad batch, uint rgba)
  178. {
  179. var size = ImageFontGraphics.GetImageFontPreferredSize(mAtlas, batch.text);
  180. var rect = new Rect(0, 0, size.x, size.y);
  181. var pivot = new Vector2(rect.width * batch.pivot.x, rect.height * batch.pivot.y);
  182. UIUtils.AdjustAnchor(batch.anchor, rectTransform.sizeDelta, ref rect);
  183. vertex.translate(rect.x + pivot.x + batch.offset.x, rect.y + pivot.y + batch.offset.y);
  184. vertex.scale(batch.scale.x, batch.scale.y);
  185. vertex.rotate(batch.rotate);
  186. float sw = 0;
  187. for (int i = 0; i < batch.text.Length; i++)
  188. {
  189. char ch = batch.text[i];
  190. int index = mAtlas.GetIndexByKey(ch.ToString());
  191. if (index >= 0)
  192. {
  193. Vector2 char_size = new Vector2(mAtlas.getWidth(index), mAtlas.getHeight(index));
  194. if (char_size.x > 0)
  195. {
  196. mAtlas.addVertex(vertex, index, sw - pivot.x, 0 - pivot.y, 0, rgba);
  197. sw += char_size.x;
  198. }
  199. }
  200. }
  201. }
  202. public override bool Raycast(Vector2 sp, Camera eventCamera)
  203. {
  204. if (mBinding.Enable && mBinding.EnableTouchInParents)
  205. {
  206. return base.Raycast(sp, eventCamera);
  207. }
  208. return false;
  209. }
  210. public struct BatchImageQuad
  211. {
  212. /// <summary>
  213. /// 图片索引
  214. /// </summary>
  215. public int index;
  216. /// <summary>
  217. /// 图片字,不为空起效
  218. /// </summary>
  219. public string text;
  220. /// <summary>
  221. /// 是否显示
  222. /// </summary>
  223. public bool visible;
  224. /// <summary>
  225. /// 对其方式
  226. /// </summary>
  227. public CommonUI.Data.ImageAnchor anchor;
  228. /// <summary>
  229. /// 旋转缩放参考点
  230. /// </summary>
  231. public Vector2 pivot;
  232. /// <summary>
  233. /// 坐标
  234. /// </summary>
  235. public Vector2 offset;
  236. /// <summary>
  237. /// 缩放
  238. /// </summary>
  239. public Vector2 scale;
  240. /// <summary>
  241. /// 旋转
  242. /// </summary>
  243. public float rotate;
  244. public BatchImageQuad(int index)
  245. {
  246. this.index = index;
  247. this.text = null;
  248. this.visible = true;
  249. this.anchor = CommonUI.Data.ImageAnchor.C_C;
  250. this.pivot = new Vector2(.5f, .5f);
  251. this.offset = Vector2.zero;
  252. this.scale = Vector2.one;
  253. this.rotate = 0;
  254. }
  255. public BatchImageQuad(string text)
  256. {
  257. this.index = -1;
  258. this.text = text;
  259. this.visible = true;
  260. this.anchor = CommonUI.Data.ImageAnchor.C_C;
  261. this.pivot = new Vector2(.5f, .5f);
  262. this.offset = Vector2.zero;
  263. this.scale = Vector2.one;
  264. this.rotate = 0;
  265. }
  266. }
  267. private void ResetTexture()
  268. {
  269. if (mSrc != null && mSrc.Texture2D != mTexture)
  270. {
  271. this.mTexture = mSrc.Texture2D;
  272. this.material = mSrc.TextureMaterial;
  273. base.SetAllDirty();
  274. }
  275. }
  276. protected override void OnEnable()
  277. {
  278. base.OnEnable();
  279. ResetTexture();
  280. }
  281. void Update()
  282. {
  283. ResetTexture();
  284. }
  285. }
  286. }