forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentExtensions.cs
More file actions
138 lines (126 loc) · 6.23 KB
/
ComponentExtensions.cs
File metadata and controls
138 lines (126 loc) · 6.23 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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>
/// Extensions methods for the Unity Component class.
/// This also includes some component-related extensions for the GameObjet class.
/// </summary>
public static class ComponentExtensions
{
/// <summary>
/// Ensure that a component of type <typeparamref name="T"/> exists on the game object.
/// If it doesn't exist, creates it.
/// </summary>
/// <typeparam name="T">Type of the component.</typeparam>
/// <param name="gameObject">Game object on which component should be.</param>
/// <returns>The component that was retrieved or created.</returns>
public static T EnsureComponent<T>(this GameObject gameObject) where T : Component
{
T foundComponent = gameObject.GetComponent<T>();
if (foundComponent == null)
{
return gameObject.AddComponent<T>();
}
return foundComponent;
}
/// <summary>
/// Ensure that a component of type <typeparamref name="T"/> exists on the game object.
/// If it doesn't exist, creates it.
/// </summary>
/// <typeparam name="T">Type of the component.</typeparam>
/// <param name="component">A component on the game object for which a component of type <typeparamref name="T"/> should exist.</param>
/// <returns>The component that was retrieved or created.</returns>
public static T EnsureComponent<T>(this Component component) where T : Component
{
return EnsureComponent<T>(component.gameObject);
}
/// <summary>
/// Apply the specified delegate to all objects in the hierarchy under a specified game object.
/// </summary>
/// <param name="root">Root game object of the hierarchy.</param>
/// <param name="action">Delegate to apply.</param>
public static void ApplyToHierarchy(this GameObject root, Action<GameObject> action)
{
action(root);
foreach (var item in root.GetComponentsInChildren<Transform>())
{
action(item.gameObject);
}
}
/// <summary>
/// Find the first component of type <typeparamref name="T"/> in the ancestors of the specified game object.
/// </summary>
/// <typeparam name="T">Type of component to find.</typeparam>
/// <param name="gameObject">Game object for which ancestors must be considered.</param>
/// <param name="includeSelf">Indicates whether the specified game object should be included.</param>
/// <returns>The component of type <typeparamref name="T"/>. Null if it none was found.</returns>
public static T FindAncestorComponent<T>(this GameObject gameObject, bool includeSelf = true) where T : Component
{
return gameObject.transform.FindAncestorComponent<T>(includeSelf);
}
/// <summary>
/// Find the first component of type <typeparamref name="T"/> in the ancestors of the game object of the specified component.
/// </summary>
/// <typeparam name="T">Type of component to find.</typeparam>
/// <param name="component">Component for which its game object's ancestors must be considered.</param>
/// <param name="includeSelf">Indicates whether the specified game object should be included.</param>
/// <returns>The component of type <typeparamref name="T"/>. Null if it none was found.</returns>
public static T FindAncestorComponent<T>(this Component component, bool includeSelf = true) where T : Component
{
return component.transform.FindAncestorComponent<T>(includeSelf);
}
/// <summary>
/// Find the first component of type <typeparamref name="T"/> in the ancestors of the specified transform.
/// </summary>
/// <typeparam name="T">Type of component to find.</typeparam>
/// <param name="startTransform">Transform for which ancestors must be considered.</param>
/// <param name="includeSelf">Indicates whether the specified transform should be included.</param>
/// <returns>The component of type <typeparamref name="T"/>. Null if it none was found.</returns>
public static T FindAncestorComponent<T>(this Transform startTransform, bool includeSelf = true) where T : Component
{
foreach (Transform transform in startTransform.EnumerateAncestors(includeSelf))
{
T component = transform.GetComponent<T>();
if (component != null)
{
return component;
}
}
return null;
}
/// <summary>
/// Enumerates the ancestors of the specified transform.
/// </summary>
/// <param name="startTransform">Transform for which ancestors must be returned.</param>
/// <param name="includeSelf">Indicates whether the specified transform should be included.</param>
/// <returns>An enumeration of all ancestor transforms of the specified start transform.</returns>
public static IEnumerable<Transform> EnumerateAncestors(this Transform startTransform, bool includeSelf)
{
if (!includeSelf)
{
startTransform = startTransform.parent;
}
for (Transform transform = startTransform; transform != null; transform = transform.parent)
{
yield return transform;
}
}
/// <summary>
/// Perform an action on every component of type T that is on this GameObject
/// </summary>
/// <typeparam name="T">Component Type</typeparam>
/// <param name="g">this gameObject</param>
/// <param name="action">Action to perform.</param>
public static void ForEachComponent<T>(this GameObject g, Action<T> action)
{
foreach (T i in g.GetComponents<T>())
{
action(i);
}
}
}
}