Extensions.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #if !NETFX_CORE
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System;
  6. using System.Linq;
  7. namespace HeurekaGames
  8. {
  9. public static class Extensions
  10. {
  11. #region Vector3
  12. public static Vector2 YZ(this Vector3 v)
  13. {
  14. return new Vector2(v.x, v.z);
  15. }
  16. public static Vector2[] YZ(this Vector3[] v)
  17. {
  18. Vector2[] new2DArray = new Vector2[v.Length];
  19. for (int i = 0; i < v.Length; i++)
  20. {
  21. new2DArray[i] = new Vector2(v[i].x, v[i].z);
  22. }
  23. return new2DArray;
  24. }
  25. #endregion
  26. #region Float
  27. public static float Remap(this float value, float from1, float to1, float from2, float to2)
  28. {
  29. return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
  30. }
  31. #endregion
  32. #region String
  33. public static string ToCamelCase(this string camelCaseString)
  34. {
  35. return System.Text.RegularExpressions.Regex.Replace(camelCaseString, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ").Trim();
  36. }
  37. #endregion
  38. #region GameObject
  39. public static void SetComponentRecursively<T>(this GameObject gameObject, bool tf) where T : Component
  40. {
  41. T[] comps = gameObject.GetComponentsInChildren<T>();
  42. foreach (T comp in comps)
  43. {
  44. try
  45. {
  46. System.Reflection.PropertyInfo pi = (typeof(T)).GetProperty("enabled");
  47. if (null != pi && pi.CanWrite)
  48. {
  49. pi.SetValue(comp, tf, null);
  50. }
  51. else
  52. {
  53. Console.WriteLine("BLABLA");
  54. Debug.Log("Property does not exist, or cannot write");
  55. }
  56. }
  57. catch (NullReferenceException e)
  58. {
  59. Debug.Log("The property does not exist in MyClass." + e.Message);
  60. }
  61. }
  62. }
  63. #endregion
  64. #region List
  65. public static void CastList<T>(this List<T> targetList)
  66. {
  67. targetList = targetList.Cast<T>().ToList();
  68. }
  69. #endregion
  70. #region Enums and flags
  71. public static bool Has<T>(this System.Enum type, T value)
  72. {
  73. try
  74. {
  75. return (((int)(object)type & (int)(object)value) == (int)(object)value);
  76. }
  77. catch
  78. {
  79. return false;
  80. }
  81. }
  82. public static bool Is<T>(this System.Enum type, T value)
  83. {
  84. try
  85. {
  86. return (int)(object)type == (int)(object)value;
  87. }
  88. catch
  89. {
  90. return false;
  91. }
  92. }
  93. public static T Add<T>(this System.Enum type, T value)
  94. {
  95. try
  96. {
  97. return (T)(object)(((int)(object)type | (int)(object)value));
  98. }
  99. catch (Exception ex)
  100. {
  101. throw new ArgumentException(
  102. string.Format(
  103. "Could not append value from enumerated type '{0}'.",
  104. typeof(T).Name
  105. ), ex);
  106. }
  107. }
  108. public static T Remove<T>(this System.Enum type, T value)
  109. {
  110. try
  111. {
  112. return (T)(object)(((int)(object)type & ~(int)(object)value));
  113. }
  114. catch (Exception ex)
  115. {
  116. throw new ArgumentException(
  117. string.Format(
  118. "Could not remove value from enumerated type '{0}'.",
  119. typeof(T).Name
  120. ), ex);
  121. }
  122. }
  123. #endregion
  124. #region Color
  125. public static Color ModifiedAlpha(this Color color, float alpha)
  126. {
  127. Color modifiedColor = color;
  128. modifiedColor.a = alpha;
  129. return modifiedColor;
  130. }
  131. #endregion
  132. }
  133. }
  134. #endif