|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using CIGEnums;
- using UnityEngine;
-
- namespace CIG3.ExtensionMethods
- {
- public static class ExtensionMethods
- {
- public static void Shuffle<T>(this IList<T> list)
- {
- int i = list.Count;
- while (i > 1)
- {
- i--;
- int index = UnityEngine.Random.Range(0, i + 1);
- T value = list[index];
- list[index] = list[i];
- list[i] = value;
- }
- }
-
- public static bool IsLand(this SurfaceType type)
- {
- return type > (SurfaceType)0 && type != SurfaceType.Water && type != SurfaceType.Beach;
- }
-
- public static T GetValue<T>(this Dictionary<string, object> values, string key, T defaultValue)
- {
- object obj;
- if (values.TryGetValue(key, out obj) && obj is T)
- {
- return (T)((object)obj);
- }
- return defaultValue;
- }
-
- public static string GetScenePath(this GameObject obj)
- {
- if (obj == null)
- {
- return "(null)";
- }
- Transform transform = obj.transform;
- string text = string.Empty;
- while (transform != null)
- {
- text = "/" + transform.name + text;
- transform = transform.parent;
- }
- return text;
- }
-
- public static T[] GetComponentsInAllChildren<T>(this GameObject obj)
- {
- List<T> list = new List<T>();
- ExtensionMethods.CollectComponents<T>(obj.transform, list);
- return list.ToArray();
- }
-
- private static void CollectComponents<T>(Transform transform, List<T> result)
- {
- T component = transform.GetComponent<T>();
- if (!object.ReferenceEquals(component, null))
- {
- result.Add(component);
- }
- IEnumerator enumerator = transform.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- Transform transform2 = (Transform)obj;
- ExtensionMethods.CollectComponents<T>(transform2, result);
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- }
-
- public static void DontDestroyObjectOnLoad(this UnityEngine.Object target, DontDestroyObjectOnLoadMode mode)
- {
- if (target is GameObject || target is Component)
- {
- Transform transform = null;
- if (target is GameObject)
- {
- transform = ((GameObject)target).transform;
- }
- else if (target is Component)
- {
- transform = ((Component)target).transform;
- }
- if (transform != null)
- {
- if (mode == DontDestroyObjectOnLoadMode.DetachFromParent)
- {
- transform.SetParent(null);
- }
- else if (mode == DontDestroyObjectOnLoadMode.DontDestroyTopParent)
- {
- Transform transform2 = transform;
- while (transform2.parent != null)
- {
- transform2 = transform2.parent;
- }
- UnityEngine.Object.DontDestroyOnLoad(transform2);
- return;
- }
- }
- }
- UnityEngine.Object.DontDestroyOnLoad(target);
- }
- }
- }
|