您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

124 行
2.9 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CIGEnums;
  5. using UnityEngine;
  6. namespace CIG3.ExtensionMethods
  7. {
  8. public static class ExtensionMethods
  9. {
  10. public static void Shuffle<T>(this IList<T> list)
  11. {
  12. int i = list.Count;
  13. while (i > 1)
  14. {
  15. i--;
  16. int index = UnityEngine.Random.Range(0, i + 1);
  17. T value = list[index];
  18. list[index] = list[i];
  19. list[i] = value;
  20. }
  21. }
  22. public static bool IsLand(this SurfaceType type)
  23. {
  24. return type > (SurfaceType)0 && type != SurfaceType.Water && type != SurfaceType.Beach;
  25. }
  26. public static T GetValue<T>(this Dictionary<string, object> values, string key, T defaultValue)
  27. {
  28. object obj;
  29. if (values.TryGetValue(key, out obj) && obj is T)
  30. {
  31. return (T)((object)obj);
  32. }
  33. return defaultValue;
  34. }
  35. public static string GetScenePath(this GameObject obj)
  36. {
  37. if (obj == null)
  38. {
  39. return "(null)";
  40. }
  41. Transform transform = obj.transform;
  42. string text = string.Empty;
  43. while (transform != null)
  44. {
  45. text = "/" + transform.name + text;
  46. transform = transform.parent;
  47. }
  48. return text;
  49. }
  50. public static T[] GetComponentsInAllChildren<T>(this GameObject obj)
  51. {
  52. List<T> list = new List<T>();
  53. ExtensionMethods.CollectComponents<T>(obj.transform, list);
  54. return list.ToArray();
  55. }
  56. private static void CollectComponents<T>(Transform transform, List<T> result)
  57. {
  58. T component = transform.GetComponent<T>();
  59. if (!object.ReferenceEquals(component, null))
  60. {
  61. result.Add(component);
  62. }
  63. IEnumerator enumerator = transform.GetEnumerator();
  64. try
  65. {
  66. while (enumerator.MoveNext())
  67. {
  68. object obj = enumerator.Current;
  69. Transform transform2 = (Transform)obj;
  70. ExtensionMethods.CollectComponents<T>(transform2, result);
  71. }
  72. }
  73. finally
  74. {
  75. IDisposable disposable;
  76. if ((disposable = (enumerator as IDisposable)) != null)
  77. {
  78. disposable.Dispose();
  79. }
  80. }
  81. }
  82. public static void DontDestroyObjectOnLoad(this UnityEngine.Object target, DontDestroyObjectOnLoadMode mode)
  83. {
  84. if (target is GameObject || target is Component)
  85. {
  86. Transform transform = null;
  87. if (target is GameObject)
  88. {
  89. transform = ((GameObject)target).transform;
  90. }
  91. else if (target is Component)
  92. {
  93. transform = ((Component)target).transform;
  94. }
  95. if (transform != null)
  96. {
  97. if (mode == DontDestroyObjectOnLoadMode.DetachFromParent)
  98. {
  99. transform.SetParent(null);
  100. }
  101. else if (mode == DontDestroyObjectOnLoadMode.DontDestroyTopParent)
  102. {
  103. Transform transform2 = transform;
  104. while (transform2.parent != null)
  105. {
  106. transform2 = transform2.parent;
  107. }
  108. UnityEngine.Object.DontDestroyOnLoad(transform2);
  109. return;
  110. }
  111. }
  112. }
  113. UnityEngine.Object.DontDestroyOnLoad(target);
  114. }
  115. }
  116. }