查看: 1500|回复: 2
打印 上一主题 下一主题

[其他] unity制作饼状图

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-12-31 22:25:51 |只看该作者 |倒序浏览
  1. using UnityEngine;

  2. public class PieChartMesh : MonoBehaviour

  3. {

  4. float[] mData;

  5. int mSlices;

  6. float mRotationAngle;

  7. float mRadius;

  8. Vector3[] mVertices;

  9. Vector3[] mNormals;

  10. Vector3 mNormal = new Vector3(0f, 0f, -1f);

  11. Vector2[] mUvs;

  12. int[] mTriangles;

  13. MeshRenderer mMeshRenderer;

  14. public void Init(float[] data, int slices, float rotatioAngle, float radius)

  15. {

  16. mData = data;

  17. mSlices = slices;

  18. mRotationAngle = rotatioAngle;

  19. mRadius = radius;

  20. // Get Mesh Renderer

  21. mMeshRenderer = gameObject.GetComponent(“MeshRenderer”) as MeshRenderer;

  22. if (mMeshRenderer == null)

  23. {

  24. gameObject.AddComponent(“MeshRenderer”);

  25. mMeshRenderer = gameObject.GetComponent(“MeshRenderer”) as MeshRenderer;

  26. }

  27. }

  28. public void Draw(float[] data)

  29. {

  30. mData = data;

  31. Draw();

  32. }

  33. public void Draw()

  34. {

  35. //Check data validity for pie chart...

  36. if (mData == null)

  37. {

  38. print(“PieChart: Data null”);

  39. return;

  40. }

  41. // Calculate sum of data values

  42. float sumOfData = 0;

  43. foreach (float value in mData)

  44. {

  45. sumOfData += value;

  46. }

  47. // Determine how many triangles in slice

  48. int[] slice = new int[mData.Length];

  49. int numOfTris = 0;

  50. int numOfSlices = 0;

  51. int countedSlices = 0;

  52. // Caluclate slice size

  53. for (int i = 0; i < mData.Length; i++)

  54. {

  55. numOfTris = (int)((mData[i] / sumOfData) * mSlices);

  56. slice[numOfSlices++] = numOfTris;

  57. countedSlices += numOfTris;

  58. }

  59. // Check that all slices are counted.. if not -> add/sub to/from biggest slice..

  60. int idxOfLargestSlice = 0;

  61. int largestSliceCount = 0;

  62. for (int i = 0; i < mData.Length; i++)

  63. {

  64. if (largestSliceCount < slice[i])

  65. {

  66. idxOfLargestSlice = i;

  67. largestSliceCount = slice[i];

  68. }

  69. }

  70. // Adjust largest dataset to get proper slice

  71. slice[idxOfLargestSlice] += mSlices - countedSlices;

  72. // Init vertices and triangles arrays

  73. mVertices = new Vector3[mSlices * 3];

  74. mNormals = new Vector3[mSlices * 3];

  75. mUvs = new Vector2[mSlices * 3];

  76. mTriangles = new int[mSlices * 3];

  77. Mesh mesh = ((MeshFilter)GetComponent(“MeshFilter”)).mesh;

  78. mesh.Clear();

  79. mesh.name = “Pie Chart Mesh”;

  80. // Roration offset (to get star point to “12 o'clock”)

  81. float rotOffset = mRotationAngle / 360f * 2f * Mathf.PI;

  82. // Calc the points in circle

  83. float angle;

  84. float[] x = new float[mSlices];

  85. float[] y = new float[mSlices];

  86. for (int i = 0; i < mSlices; i++)

  87. {

  88. angle = i * 2f * Mathf.PI / mSlices;

  89. x[i] = (Mathf.Cos(angle + rotOffset) * mRadius);

  90. y[i] = (Mathf.Sin(angle + rotOffset) * mRadius);

  91. }

  92. // Generate mesh with slices (vertices and triangles)

  93. for (int i = 0; i < mSlices; i++)

  94. {

  95. mVertices[i * 3 + 0] = new Vector3(0f, 0f, 0f);

  96. mVertices[i * 3 + 1] = new Vector3(x[i], y[i], 0f);

  97. // This will ensure that last vertex = first vertex..

  98. mVertices[i * 3 + 2] = new Vector3(x[(i + 1) % mSlices], y[(i + 1) % mSlices], 0f);

  99. mNormals[i * 3 + 0] = mNormal;

  100. mNormals[i * 3 + 1] = mNormal;

  101. mNormals[i * 3 + 2] = mNormal;

  102. mUvs[i * 3 + 0] = new Vector2(0f, 0f);

  103. mUvs[i * 3 + 1] = new Vector2(x[i], y[i]);

  104. // This will ensure that last uv = first uv..

  105. mUvs[i * 3 + 2] = new Vector2(x[(i + 1) % mSlices], y[(i + 1) % mSlices]);

  106. mTriangles[i * 3 + 0] = i * 3 + 0;

  107. mTriangles[i * 3 + 1] = i * 3 + 1;

  108. mTriangles[i * 3 + 2] = i * 3 + 2;

  109. }

  110. // Assign verts, norms, uvs and tris to mesh and calc normals

  111. mesh.vertices = mVertices;

  112. mesh.normals = mNormals;

  113. mesh.uv = mUvs;

  114. //mesh.triangles = triangles;

  115. mesh.subMeshCount = mData.Length;

  116. int[][] subTris = new int[mData.Length][];

  117. countedSlices = 0;

  118. // Set sub meshes

  119. for (int i = 0; i < mData.Length; i++)

  120. {

  121. // Every triangle has three veritces..

  122. subTris[i] = new int[slice[i] * 3];

  123. // Add tris to subTris

  124. for (int j = 0; j < slice[i]; j++)

  125. {

  126. subTris[i][j * 3 + 0] = mTriangles[countedSlices * 3 + 0];

  127. subTris[i][j * 3 + 1] = mTriangles[countedSlices * 3 + 1];

  128. subTris[i][j * 3 + 2] = mTriangles[countedSlices * 3 + 2];

  129. countedSlices++;

  130. }

  131. mesh.SetTriangles(subTris[i], i);

  132. }

  133. }

  134. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

100

主题

3

听众

7683

积分

高级设计师

Rank: 6Rank: 6

纳金币
2378
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-12-31 23:26:08 |只看该作者
涨姿势了。。。。。。。
回复

使用道具 举报

47

主题

2

听众

2810

积分

中级设计师

Rank: 5Rank: 5

纳金币
571
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2015-1-2 19:23:29 |只看该作者
我是用序列图的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-10 21:00 , Processed in 0.890047 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部