forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformExtensions.cs
More file actions
90 lines (80 loc) · 3.8 KB
/
Copy pathTransformExtensions.cs
File metadata and controls
90 lines (80 loc) · 3.8 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
78
79
80
81
82
83
84
85
86
87
88
89
90
// 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 System.Text;
using UnityEngine;
namespace HoloToolkit.Unity
{
public static class TransformExtensions
{
/// <summary>
/// An extension method that will get you the full path to an object.
/// </summary>
/// <param name="transform">The transform you wish a full path to.</param>
/// <param name="delimiter">The delimiter with which each object is delimited in the string.</param>
/// <param name="prefix">Prefix with which the full path to the object should start.</param>
/// <returns>A delimited string that is the full path to the game object in the hierarchy.</returns>
public static string GetFullPath(this Transform transform, string delimiter = ".", string prefix = "/")
{
StringBuilder stringBuilder = new StringBuilder();
GetFullPath(stringBuilder, transform, delimiter, prefix);
return stringBuilder.ToString();
}
private static void GetFullPath(StringBuilder stringBuilder, Transform transform, string delimiter, string prefix)
{
if (transform.parent == null)
{
stringBuilder.Append(prefix);
} else
{
GetFullPath(stringBuilder, transform.parent, delimiter, prefix);
stringBuilder.Append(delimiter);
}
stringBuilder.Append(transform.name);
}
/// <summary>
/// Enumerates all children in the hierarchy starting at the root object.
/// </summary>
/// <param name="root">Start point of the traversion set</param>
public static IEnumerable<Transform> EnumerateHierarchy(this Transform root)
{
if (root == null) { throw new ArgumentNullException("root"); }
return root.EnumerateHierarchyCore(new List<Transform>(0));
}
/// <summary>
/// Enumerates all children in the hierarchy starting at the root object except for the branches in ignore.
/// </summary>
/// <param name="root">Start point of the traversion set</param>
/// <param name="ignore">Transforms and all its children to be ignored</param>
public static IEnumerable<Transform> EnumerateHierarchy(this Transform root, ICollection<Transform> ignore)
{
if (root == null) { throw new ArgumentNullException("root"); }
if (ignore == null)
{
throw new ArgumentNullException("ignore", "Ignore collection can't be null, use EnumerateHierarchy(root) instead.");
}
return root.EnumerateHierarchyCore(ignore);
}
/// <summary>
/// Enumerates all children in the hierarchy starting at the root object except for the branches in ignore.
/// </summary>
/// <param name="root">Start point of the traversion set</param>
/// <param name="ignore">Transforms and all its children to be ignored</param>
private static IEnumerable<Transform> EnumerateHierarchyCore(this Transform root, ICollection<Transform> ignore)
{
var transformQueue = new Queue<Transform>();
transformQueue.Enqueue(root);
while (transformQueue.Count > 0)
{
var parentTransform = transformQueue.Dequeue();
if (!parentTransform || ignore.Contains(parentTransform)) { continue; }
for (var i = 0; i < parentTransform.childCount; i++)
{
transformQueue.Enqueue(parentTransform.GetChild(i));
}
yield return parentTransform;
}
}
}
}