Extensions.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using UnityEngine;
  4. namespace HQFPSWeapons
  5. {
  6. public static class Extensions
  7. {
  8. public static string DoUnityLikeNameFormat(this string str)
  9. {
  10. if(string.IsNullOrEmpty(str))
  11. return string.Empty;
  12. if(str.Length > 2 && str[0] == 'm' && str[1] == '_')
  13. str = str.Remove(0, 2);
  14. if(str.Length > 1 && str[0] == '_')
  15. str = str.Remove(0);
  16. StringBuilder newText = new StringBuilder(str.Length * 2);
  17. newText.Append(str[0]);
  18. for(int i = 1; i < str.Length; i++)
  19. {
  20. bool lastIsUpper = char.IsUpper(str[i - 1]);
  21. bool lastIsSpace = str[i - 1] == ' ';
  22. bool lastIsDigit = char.IsDigit(str[i - 1]);
  23. if(char.IsUpper(str[i]) && !lastIsUpper && !lastIsSpace)
  24. newText.Append(' ');
  25. if(char.IsDigit(str[i]) && !lastIsDigit && !lastIsUpper && !lastIsSpace)
  26. newText.Append(' ');
  27. newText.Append(str[i]);
  28. }
  29. return newText.ToString();
  30. }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. public static Transform FindDeepChild(this Transform parent, string childName)
  35. {
  36. var result = parent.Find(childName);
  37. if(result)
  38. return result;
  39. for(int i = 0;i < parent.childCount;i ++)
  40. {
  41. result = parent.GetChild(i).FindDeepChild(childName);
  42. if(result)
  43. return result;
  44. }
  45. return null;
  46. }
  47. /// <summary>
  48. /// Checks if the index is inside the list's bounds.
  49. /// </summary>
  50. public static bool IndexIsValid<T>(this List<T> list, int index)
  51. {
  52. return index >= 0 && index < list.Count;
  53. }
  54. public static List<T> CopyOther<T>(this List<T> list, List<T> toCopy)
  55. {
  56. if (toCopy == null || toCopy.Count == 0)
  57. return null;
  58. list = new List<T>();
  59. for (int i = 0; i < toCopy.Count; i++)
  60. list.Add(toCopy[i]);
  61. return list;
  62. }
  63. public static bool IsInRangeLimitsExcluded(this float f, float l1, float l2)
  64. {
  65. return f > l1 && f < l2;
  66. }
  67. public static bool IsInRangeLimitsIncluded(this float f, float l1, float l2)
  68. {
  69. return f >= l1 && f <= l2;
  70. }
  71. }
  72. }