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