AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
GameObjectExtensions.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 UnityEngine;
7 
8 namespace HoloToolkit.Unity
9 {
13  public static class GameObjectExtensions
14  {
15  [Obsolete("Use the more extensive TransformExtensions.GetFullPath instead.")]
16  public static string GetFullPath(this GameObject go)
17  {
18  return go.transform.GetFullPath("/", "");
19  }
20 
26  public static void SetLayerRecursively(this GameObject root, int layer)
27  {
28  if (root == null)
29  {
30  throw new ArgumentNullException("root", "Root transform can't be null.");
31  }
32 
33  foreach (var child in root.transform.EnumerateHierarchy())
34  {
35  child.gameObject.layer = layer;
36  }
37  }
38 
45  public static void SetLayerRecursively(this GameObject root, int layer, out Dictionary<GameObject, int> cache)
46  {
47  if (root == null) { throw new ArgumentNullException("root"); }
48 
49  cache = new Dictionary<GameObject, int>();
50 
51  foreach (var child in root.transform.EnumerateHierarchy())
52  {
53  cache[child.gameObject] = child.gameObject.layer;
54  child.gameObject.layer = layer;
55  }
56  }
57 
63  public static void ApplyLayerCacheRecursively(this GameObject root, Dictionary<GameObject, int> cache)
64  {
65  if (root == null) { throw new ArgumentNullException("root"); }
66  if (cache == null) { throw new ArgumentNullException("cache"); }
67 
68  foreach (var child in root.transform.EnumerateHierarchy())
69  {
70  int layer;
71  if (!cache.TryGetValue(child.gameObject, out layer)) { continue; }
72  child.gameObject.layer = layer;
73  cache.Remove(child.gameObject);
74  }
75  }
76 
82  public static GameObject GetParentRoot(this GameObject child)
83  {
84  if (child.transform.parent == null)
85  {
86  return child;
87  }
88  else
89  {
90  return GetParentRoot(child.transform.parent.gameObject);
91  }
92  }
93  }
94 }
static void SetLayerRecursively(this GameObject root, int layer, out Dictionary< GameObject, int > cache)
Set the layer to the given object and the full hierarchy below it and cache the previous layers in th...
static void SetLayerRecursively(this GameObject root, int layer)
Set the layer to the given object and the full hierarchy below it.
static GameObject GetParentRoot(this GameObject child)
Gets the GameObject&#39;s root Parent object.
Extension methods for Unity&#39;s GameObject class
static string GetFullPath(this GameObject go)
static void ApplyLayerCacheRecursively(this GameObject root, Dictionary< GameObject, int > cache)
Reapplies previously cached hierarchy layers