当前位置:   article > 正文

在Unity3D中实现高效的战斗飘字_unity 打字游戏

unity 打字游戏

版权所有,转载须注明出处!
喜欢火影、喜欢Java、喜欢unity3D、喜欢游戏开发的都可以加入木叶村Q群:379076227



这周有网友提到用NGUI做战斗冒血,冒伤害等文字性能太差。确实如此,我之前(NGUI 3.8.2,1000块的Android机器上)在Profile中也发现UIPanel占用CPU奇高。

因此最终自己在战斗飘文字这一块没有使用NGUI,而是自己创建Mesh来处理。优化前后在手机上每秒大概增加了10帧。


先看一下效果。


可以看到DrawCall和NGUI一样都为1。

这个 MeshText 就是我自己写的HUD文本组件了。


代码如下:

  1. using UnityEngine;
  2. [ExecuteInEditMode]
  3. public class MeshText : MonoBehaviour
  4. {
  5. [SerializeField]
  6. private string _text = "";
  7. public MeshFilter meshFilter;
  8. public MeshRenderer meshRenderer;
  9. [HideInInspector]
  10. public Material material;
  11. public UIAtlas uiAtlas;
  12. [SerializeField]
  13. private Color _color1 = Color.white;
  14. public Color color1
  15. {
  16. get { return _color1; }
  17. set {
  18. _color1 = value;
  19. }
  20. }
  21. [SerializeField]
  22. private Color _color2 = Color.white;
  23. public Color color2
  24. {
  25. get { return _color2; }
  26. set
  27. {
  28. _color2 = value;
  29. }
  30. }
  31. public enum HorizontalAlignType
  32. {
  33. Left,
  34. Center,
  35. Right
  36. }
  37. //当text中存在宽度不一致的字体时,计算Center和Right会有误差。不过对于战斗HUD,够用了。
  38. public HorizontalAlignType HAlignType;
  39. public string Text {
  40. get { return _text; }
  41. set
  42. {
  43. _text = value;
  44. GenerateFilter();
  45. }
  46. }
  47. void Awake() {
  48. #if UNITY_EDITOR
  49. meshRenderer.sharedMaterial = uiAtlas.spriteMaterial;
  50. material = meshRenderer.sharedMaterial;
  51. #else
  52. meshRenderer.material = uiAtlas.spriteMaterial;
  53. material = meshRenderer.material;
  54. #endif
  55. if (!string.IsNullOrEmpty(_text))
  56. {
  57. GenerateFilter();
  58. }
  59. }
  60. public void GenerateFilter() {
  61. Mesh mesh = new Mesh();
  62. int length = Text.Length;
  63. Vector3[] vertices = new Vector3[length<<2];
  64. Vector2[] uvs = new Vector2[vertices.Length];
  65. int[] triangles = new int[(length<<1)*3];
  66. Texture tex = uiAtlas.texture;
  67. Color[] colors = new Color[vertices.Length];
  68. int tmp = 0;
  69. float tmp2 = 0;
  70. switch (HAlignType)
  71. {
  72. case HorizontalAlignType.Center:
  73. tmp2 = - (vertices.Length >> 3);
  74. break;
  75. case HorizontalAlignType.Left:
  76. tmp2 = 0;
  77. break;
  78. case HorizontalAlignType.Right:
  79. tmp2 = -(vertices.Length >> 2);
  80. break;
  81. default:
  82. tmp2 = 0;
  83. break;
  84. }
  85. float r = 1;
  86. for (int i = 0; i < vertices.Length; i+=4) {
  87. tmp = (i + 1) % 2;
  88. string s = Text[i / 4].ToString();
  89. UISpriteData mSprite = uiAtlas.GetSprite(s);
  90. r = (mSprite.width * 1.0f / mSprite.height);
  91. //setting vertices
  92. vertices[i ] = new Vector3( tmp2, tmp + 1 );
  93. vertices[i + 1] = new Vector3( tmp2, tmp );
  94. tmp2 += r;
  95. vertices[i + 2] = new Vector3( tmp2, tmp + 1 );
  96. vertices[i + 3] = new Vector3( tmp2, tmp );
  97. colors[i] = color1;
  98. colors[i+1] = color2;
  99. colors[i+2] = color1;
  100. colors[i+3] = color2;
  101. //setting uvs
  102. Rect inner = new Rect(mSprite.x + mSprite.borderLeft, mSprite.y + mSprite.borderTop,
  103. mSprite.width - mSprite.borderLeft - mSprite.borderRight,
  104. mSprite.height - mSprite.borderBottom - mSprite.borderTop);
  105. inner = NGUIMath.ConvertToTexCoords(inner, tex.width, tex.height);
  106. uvs[i] = new Vector2(inner.xMin, inner.yMax);
  107. uvs[i + 1] = new Vector2(inner.xMin, inner.yMin);
  108. uvs[i + 2] = new Vector2(inner.xMax, inner.yMax);
  109. uvs[i + 3] = new Vector2(inner.xMax, inner.yMin);
  110. }
  111. for (int i = 0; i < triangles.Length; i+=6) {
  112. tmp = (i / 3) << 1;
  113. triangles[i] = triangles[i + 3] = tmp;
  114. triangles[i + 1] = triangles[i + 5] = tmp + 3;
  115. triangles[i + 2] = tmp + 1;
  116. triangles[i + 4] = tmp + 2;
  117. }
  118. mesh.vertices = vertices;
  119. mesh.colors = colors;
  120. mesh.triangles = triangles;
  121. mesh.uv = uvs;
  122. meshFilter.mesh = mesh;
  123. }
  124. void OnDrawGizmos()
  125. {
  126. Gizmos.color = Color.gray;
  127. DrawMesh();
  128. }
  129. void OnDrawGizmosSelected()
  130. {
  131. Gizmos.color = Color.green;
  132. DrawMesh();
  133. }
  134. private void DrawMesh()
  135. {
  136. if (meshFilter == null)
  137. {
  138. return;
  139. }
  140. Mesh mesh = meshFilter.sharedMesh;
  141. if (mesh == null)
  142. {
  143. return;
  144. }
  145. int[] tris = mesh.triangles;
  146. for (int i = 0; i < tris.Length; i += 3)
  147. {
  148. Gizmos.DrawLine(convert2World(mesh.vertices[tris[i]]), convert2World(mesh.vertices[tris[i + 1]]));
  149. Gizmos.DrawLine(convert2World(mesh.vertices[tris[i]]), convert2World(mesh.vertices[tris[i + 2]]));
  150. Gizmos.DrawLine(convert2World(mesh.vertices[tris[i + 1]]), convert2World(mesh.vertices[tris[i + 2]]));
  151. }
  152. }
  153. private Vector3 convert2World(Vector3 src)
  154. {
  155. return transform.TransformPoint(src);
  156. }
  157. }



关于这段代码应该没什么解释的,主要是关于三角形顶点计算、uv计算这些。随便找一篇创建Mesh的文章看完应该就可以看懂了。

虽然里面用到了NGUI的图集,但渲染和更新已经完全和NGUI无关了。

如果想改成不用NGUI的图集也可以。不过那时候比较懒,已经有图集了,就直接拿过来用咯。

不想用NGUI图集的可以自行修改掉。这样可以脱离NGUI运行。


完整工程(基于Unity3D 4.3.3 和 NGUI3.8.2测试)在这里:

链接: http://pan.baidu.com/s/1pKpalh5 密码: snpq


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/125638
推荐阅读
相关标签
  

闽ICP备14008679号