Hierarchy.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace SRF
  6. {
  7. public class Hierarchy
  8. {
  9. [Obsolete("Use static Get() instead")]
  10. public Transform this[string key]
  11. {
  12. get
  13. {
  14. return Hierarchy.Get(key);
  15. }
  16. }
  17. public static Transform Get(string key)
  18. {
  19. Transform transform;
  20. if (Hierarchy.Cache.TryGetValue(key, out transform))
  21. {
  22. return transform;
  23. }
  24. GameObject gameObject = GameObject.Find(key);
  25. if (gameObject)
  26. {
  27. transform = gameObject.transform;
  28. Hierarchy.Cache.Add(key, transform);
  29. return transform;
  30. }
  31. string[] array = key.Split(Hierarchy.Seperator, StringSplitOptions.RemoveEmptyEntries);
  32. transform = new GameObject(array.Last<string>()).transform;
  33. Hierarchy.Cache.Add(key, transform);
  34. if (array.Length == 1)
  35. {
  36. return transform;
  37. }
  38. transform.parent = Hierarchy.Get(string.Join("/", array, 0, array.Length - 1));
  39. return transform;
  40. }
  41. private static readonly char[] Seperator = new char[]
  42. {
  43. '/'
  44. };
  45. private static readonly Dictionary<string, Transform> Cache = new Dictionary<string, Transform>();
  46. }
  47. }