PrimitiveShape.cs 930 B

1234567891011121314151617181920212223242526272829303132
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. namespace Devdog.SciFiDesign.UI
  5. {
  6. [RequireComponent(typeof(RectTransform))]
  7. public class PrimitiveShape : Graphic
  8. {
  9. public int elementCount = 6;
  10. public float edgeThickness = 5f;
  11. protected override void OnPopulateMesh(VertexHelper vh)
  12. {
  13. var r = GetPixelAdjustedRect();
  14. vh.Clear();
  15. for (int j = 0; j < elementCount *2; j++)
  16. {
  17. float val = ((float)j / elementCount) * Mathf.PI * 2;
  18. var x = Mathf.Cos(val);
  19. var y = Mathf.Sin(val);
  20. vh.AddVert(new Vector3(((x * edgeThickness) + x) * r.x, ((y * edgeThickness) + y) * r.y), color, Vector2.zero);
  21. vh.AddVert(new Vector3(x * r.x, y * r.y), color, Vector2.zero);
  22. vh.AddTriangle(j, j + 1, j + 2);
  23. }
  24. }
  25. }
  26. }