CylinderText.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /// adaption for cylindrical bending by herbst
  2. /// Credit Breyer
  3. /// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/#post-1777407
  4. namespace UnityEngine.UI.Extensions
  5. {
  6. [RequireComponent(typeof(Text), typeof(RectTransform))]
  7. [AddComponentMenu("UI/Effects/Extensions/Cylinder Text")]
  8. public class CylinderText : BaseMeshEffect
  9. {
  10. public float radius;
  11. #if UNITY_EDITOR
  12. protected override void OnValidate()
  13. {
  14. base.OnValidate();
  15. }
  16. #endif
  17. protected override void Awake()
  18. {
  19. base.Awake();
  20. OnRectTransformDimensionsChange();
  21. }
  22. protected override void OnEnable()
  23. {
  24. base.OnEnable();
  25. OnRectTransformDimensionsChange();
  26. }
  27. public override void ModifyMesh(VertexHelper vh)
  28. {
  29. if (! IsActive()) return;
  30. int count = vh.currentVertCount;
  31. if (!IsActive() || count == 0)
  32. {
  33. return;
  34. }
  35. for (int index = 0; index < vh.currentVertCount; index++)
  36. {
  37. UIVertex uiVertex = new UIVertex();
  38. vh.PopulateUIVertex(ref uiVertex, index);
  39. // get x position
  40. var x = uiVertex.position.x;
  41. // calculate bend based on pivot and radius
  42. uiVertex.position.z = -radius * Mathf.Cos(x / radius);
  43. uiVertex.position.x = radius * Mathf.Sin(x / radius);
  44. vh.SetUIVertex(uiVertex, index);
  45. }
  46. }
  47. }
  48. }