CurvedUIExtensionMethods.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace CurvedUI{
  5. public static class CurvedUIExtensionMethods
  6. {
  7. /// <summary>
  8. ///Direct Vector3 comparison can produce wrong results sometimes due to float inacuracies.
  9. ///This is an aproximate comparison.
  10. /// <returns></returns>
  11. public static bool AlmostEqual(this Vector3 a, Vector3 b, double accuracy = 0.01)
  12. {
  13. return Vector3.SqrMagnitude(a - b) < accuracy;
  14. }
  15. public static float Remap(this float value, float from1, float to1, float from2, float to2)
  16. {
  17. return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
  18. }
  19. public static float RemapAndClamp(this float value, float from1, float to1, float from2, float to2)
  20. {
  21. return value.Remap(from1, to1, from2, to2).Clamp(from2, to2);
  22. }
  23. public static float Remap(this int value, float from1, float to1, float from2, float to2)
  24. {
  25. return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
  26. }
  27. public static double Remap(this double value, double from1, double to1, double from2, double to2)
  28. {
  29. return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
  30. }
  31. public static float Clamp(this float value, float min, float max)
  32. {
  33. return Mathf.Clamp(value, min, max);
  34. }
  35. public static float Clamp(this int value, int min, int max)
  36. {
  37. return Mathf.Clamp(value, min, max);
  38. }
  39. public static int Abs(this int value)
  40. {
  41. return Mathf.Abs(value);
  42. }
  43. public static float Abs(this float value)
  44. {
  45. return Mathf.Abs(value);
  46. }
  47. /// <summary>
  48. /// Returns value rounded to nearest integer (both up and down).
  49. /// </summary>
  50. /// <returns>The int.</returns>
  51. /// <param name="value">Value.</param>
  52. public static int ToInt(this float value)
  53. {
  54. return Mathf.RoundToInt(value);
  55. }
  56. public static int FloorToInt(this float value)
  57. {
  58. return Mathf.FloorToInt(value);
  59. }
  60. public static int CeilToInt(this float value)
  61. {
  62. return Mathf.FloorToInt(value);
  63. }
  64. public static Vector3 ModifyX(this Vector3 trans, float newVal)
  65. {
  66. trans = new Vector3(newVal, trans.y, trans.z);
  67. return trans;
  68. }
  69. public static Vector3 ModifyY(this Vector3 trans, float newVal)
  70. {
  71. trans = new Vector3(trans.x, newVal, trans.z);
  72. return trans;
  73. }
  74. public static Vector3 ModifyZ(this Vector3 trans, float newVal)
  75. {
  76. trans = new Vector3(trans.x, trans.y, newVal);
  77. return trans;
  78. }
  79. public static Vector2 ModifyVectorX(this Vector2 trans, float newVal)
  80. {
  81. trans = new Vector3(newVal, trans.y);
  82. return trans;
  83. }
  84. public static Vector2 ModifyVectorY(this Vector2 trans, float newVal)
  85. {
  86. trans = new Vector3(trans.x, newVal);
  87. return trans;
  88. }
  89. /// <summary>
  90. /// Resets transform's local position, rotation and scale
  91. /// </summary>
  92. /// <param name="trans">Trans.</param>
  93. public static void ResetTransform(this Transform trans)
  94. {
  95. trans.localPosition = Vector3.zero;
  96. trans.localRotation = Quaternion.identity;
  97. trans.localScale = Vector3.one;
  98. }
  99. public static T AddComponentIfMissing<T>(this GameObject go) where T : Component
  100. {
  101. if (go.GetComponent<T>() == null)
  102. {
  103. return go.AddComponent<T>();
  104. }
  105. else return go.GetComponent<T>();
  106. }
  107. /// <summary>
  108. /// Checks if given component is preset and if not, adds it and returns it.
  109. /// </summary>
  110. public static T AddComponentIfMissing<T>(this Component go) where T : Component
  111. {
  112. return go.gameObject.AddComponentIfMissing<T>();
  113. }
  114. }
  115. }