TransformCacheExtensions.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal static class TransformCacheExtensions
  7. {
  8. internal static bool IsDescendant<T>(this T transform, T ancestor) where T : TransformCache
  9. {
  10. if (ancestor != null)
  11. {
  12. var parent = transform.parent;
  13. while (parent != null)
  14. {
  15. if (parent == ancestor)
  16. return true;
  17. parent = parent.parent;
  18. }
  19. }
  20. return false;
  21. }
  22. internal static bool IsDescendant<T>(this T transform, T[] ancestors) where T : TransformCache
  23. {
  24. return ancestors.FirstOrDefault( t => transform.IsDescendant<T>(t) ) != null;
  25. }
  26. internal static T[] FindRoots<T>(this T[] transforms) where T : TransformCache
  27. {
  28. return transforms.Where(t => t.IsDescendant(transforms) == false).ToArray();
  29. }
  30. internal static T FindRoot<T>(this T transform, T[] transforms) where T : TransformCache
  31. {
  32. var roots = transforms.FindRoots<T>();
  33. return roots.FirstOrDefault( r => transform == r || IsDescendant<T>(transform, r) );
  34. }
  35. }
  36. }