forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjectExtensions.cs
More file actions
77 lines (68 loc) · 2.91 KB
/
Copy pathGameObjectExtensions.cs
File metadata and controls
77 lines (68 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// Extension methods for Unity's GameObject class
/// </summary>
public static class GameObjectExtensions
{
[Obsolete("Use the more extensive TransformExtensions.GetFullPath instead.")]
public static string GetFullPath(this GameObject go)
{
return go.transform.GetFullPath("/", "");
}
/// <summary>
/// Set the layer to the given object and the full hierarchy below it.
/// </summary>
/// <param name="root">Start point of the traverse</param>
/// <param name="layer">The layer to apply</param>
public static void SetLayerRecursively(this GameObject root, int layer)
{
if (root == null)
{
throw new ArgumentNullException("root", "Root transform can't be null.");
}
foreach (var child in root.transform.EnumerateHierarchy())
{
child.gameObject.layer = layer;
}
}
/// <summary>
/// Set the layer to the given object and the full hierarchy below it and cache the previous layers in the out parameter.
/// </summary>
/// <param name="root">Start point of the traverse</param>
/// <param name="layer">The layer to apply</param>
/// <param name="cache">The previously set layer for each object</param>
public static void SetLayerRecursively(this GameObject root, int layer, out Dictionary<GameObject, int> cache)
{
if (root == null) { throw new ArgumentNullException("root"); }
cache = new Dictionary<GameObject, int>();
foreach (var child in root.transform.EnumerateHierarchy())
{
cache[child.gameObject] = child.gameObject.layer;
child.gameObject.layer = layer;
}
}
/// <summary>
/// Reapplies previously cached hierarchy layers
/// </summary>
/// <param name="root">Start point of the traverse</param>
/// <param name="cache">The previously set layer for each object</param>
public static void ApplyLayerCacheRecursively(this GameObject root, Dictionary<GameObject, int> cache)
{
if (root == null) { throw new ArgumentNullException("root"); }
if (cache == null) { throw new ArgumentNullException("cache"); }
foreach (var child in root.transform.EnumerateHierarchy())
{
int layer;
if (!cache.TryGetValue(child.gameObject, out layer)) { continue; }
child.gameObject.layer = layer;
cache.Remove(child.gameObject);
}
}
}
}