AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
TransformExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections.Generic;
6 using System.Text;
7 using UnityEngine;
8 
9 namespace HoloToolkit.Unity
10 {
11  public static class TransformExtensions
12  {
20  public static string GetFullPath(this Transform transform, string delimiter = ".", string prefix = "/")
21  {
22  StringBuilder stringBuilder = new StringBuilder();
23  GetFullPath(stringBuilder, transform, delimiter, prefix);
24  return stringBuilder.ToString();
25  }
26 
27  private static void GetFullPath(StringBuilder stringBuilder, Transform transform, string delimiter, string prefix)
28  {
29  if (transform.parent == null)
30  {
31  stringBuilder.Append(prefix);
32  }
33  else
34  {
35  GetFullPath(stringBuilder, transform.parent, delimiter, prefix);
36  stringBuilder.Append(delimiter);
37  }
38  stringBuilder.Append(transform.name);
39  }
40 
45  public static IEnumerable<Transform> EnumerateHierarchy(this Transform root)
46  {
47  if (root == null) { throw new ArgumentNullException("root"); }
48  return root.EnumerateHierarchyCore(new List<Transform>(0));
49  }
50 
56  public static IEnumerable<Transform> EnumerateHierarchy(this Transform root, ICollection<Transform> ignore)
57  {
58  if (root == null) { throw new ArgumentNullException("root"); }
59  if (ignore == null)
60  {
61  throw new ArgumentNullException("ignore", "Ignore collection can't be null, use EnumerateHierarchy(root) instead.");
62  }
63  return root.EnumerateHierarchyCore(ignore);
64  }
65 
71  private static IEnumerable<Transform> EnumerateHierarchyCore(this Transform root, ICollection<Transform> ignore)
72  {
73  var transformQueue = new Queue<Transform>();
74  transformQueue.Enqueue(root);
75 
76  while (transformQueue.Count > 0)
77  {
78  var parentTransform = transformQueue.Dequeue();
79 
80  if (!parentTransform || ignore.Contains(parentTransform)) { continue; }
81 
82  for (var i = 0; i < parentTransform.childCount; i++)
83  {
84  transformQueue.Enqueue(parentTransform.GetChild(i));
85  }
86 
87  yield return parentTransform;
88  }
89  }
90 
97  public static Bounds GetColliderBounds(this Transform transform)
98  {
99  Collider[] colliders = transform.GetComponentsInChildren<Collider>();
100  if (colliders.Length == 0) { return new Bounds(); }
101 
102  Bounds bounds = colliders[0].bounds;
103  for (int i = 1; i < colliders.Length; i++)
104  {
105  bounds.Encapsulate(colliders[i].bounds);
106  }
107  return bounds;
108  }
109 
114  public static bool IsParentOrChildOf(this Transform transform1, Transform transform2)
115  {
116  return transform1.IsChildOf(transform2) || transform2.IsChildOf(transform1);
117  }
118  }
119 }
static IEnumerable< Transform > EnumerateHierarchy(this Transform root)
Enumerates all children in the hierarchy starting at the root object.
static bool IsParentOrChildOf(this Transform transform1, Transform transform2)
Checks if the provided transforms are child/parent related.
static Bounds GetColliderBounds(this Transform transform)
Calculates the bounds of all the colliders attached to this GameObject and all it&#39;s children ...
static string GetFullPath(this Transform transform, string delimiter=".", string prefix="/")
An extension method that will get you the full path to an object.
static IEnumerable< Transform > EnumerateHierarchy(this Transform root, ICollection< Transform > ignore)
Enumerates all children in the hierarchy starting at the root object except for the branches in ignor...