Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Find common root for GameObject and Transforms (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson authored May 20, 2019
1 parent 2ee7481 commit 18e345e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,39 @@ public static void SetRenderingActive(this GameObject gameObject, bool isActive,
}
}
}

/// <summary>
/// Given 2 GameObjects, return a common root GameObject (or null).
/// </summary>
/// <param name="g1">GameObject to compare</param>
/// <param name="g2">GameObject to compare</param>
public static GameObject FindCommonRoot(this GameObject g1, GameObject g2)
{
if (g1 == null || g2 == null)
{
return null;
}

var t1 = g1.transform;

while (t1 != null)
{
var t2 = g2.transform;

while (t2 != null)
{
if (t1 == t2)
{
return t1.gameObject;
}

t2 = t2.parent;
}

t1 = t1.parent;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static IEnumerable<Transform> EnumerateHierarchyCore(this Transform root
/// Calculates the bounds of all the colliders attached to this GameObject and all it's children
/// </summary>
/// <param name="transform">Transform of root GameObject the colliders are attached to </param>
/// <returns>The total bounds of all colliders attached to this GameObject.
/// <returns>The total bounds of all colliders attached to this GameObject.
/// If no colliders attached, returns a bounds of center and extents 0</returns>
public static Bounds GetColliderBounds(this Transform transform)
{
Expand Down Expand Up @@ -255,5 +255,35 @@ public static bool TryGetDepth(Transform target, Transform parent, ref int depth

return false;
}

/// <summary>
/// Given 2 Transforms, return a common root Transform (or null).
/// </summary>
/// <param name="t1">Transform to compare</param>
/// <param name="t2">Transform to compare</param>
public static Transform FindCommonRoot(this Transform t1, Transform t2)
{
if (t1 == null || t2 == null)
{
return null;
}

while (t1 != null)
{
while (t2 != null)
{
if (t1 == t2)
{
return t1;
}

t2 = t2.parent;
}

t1 = t1.parent;
}

return null;
}
}
}

0 comments on commit 18e345e

Please sign in to comment.