-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathFindObjects.cs
More file actions
129 lines (113 loc) · 5.29 KB
/
Copy pathFindObjects.cs
File metadata and controls
129 lines (113 loc) · 5.29 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
namespace Unity.Netcode
{
/// <summary>
/// Helper class to handle the variations of FindObjectsByType.
/// </summary>
/// <remarks>
/// It is intentional that we do not include the UnityEngine namespace in order to avoid
/// over-complicated define wrapping between versions that do or don't support FindObjectsSortMode.
/// </remarks>
internal static class FindObjects
{
/// <summary>
/// Replaces <see cref="Object.FindObjectsByType"/> to have one place where these changes are applied.
/// </summary>
/// <typeparam name="T">The type of object to find. Must be a reference type derived from <see cref="Object"/></typeparam>
/// <param name="includeInactive">When true, inactive objects will be included.</param>
/// <param name="orderByIdentifier">When true, the array returned will be sorted by identifier.</param>
/// <returns>Results as an <see cref="Array"/> of type T</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] ByType<T>(bool includeInactive = false, bool orderByIdentifier = false) where T : Object
{
var inactive = includeInactive ? UnityEngine.FindObjectsInactive.Include : UnityEngine.FindObjectsInactive.Exclude;
#if NGO_FINDOBJECTS_NOSORTING
var results = Object.FindObjectsByType<T>(inactive);
#if !NGO_FINDOBJECTS_UNORDERED_IDS
if (orderByIdentifier)
{
Array.Sort(results, (a, b) => a.GetEntityId().CompareTo(b.GetEntityId()));
}
#endif
#else
var results = Object.FindObjectsByType<T>(inactive, orderByIdentifier ? UnityEngine.FindObjectsSortMode.InstanceID : UnityEngine.FindObjectsSortMode.None);
#endif
return results;
}
/// <summary>
/// Returns an enumerator that enumerates over all the components of a given type in a scene.
/// </summary>
/// <param name="scene">The scene to use for searching</param>
/// <param name="includeInactive">When true, inactive objects will be included.</param>
/// <typeparam name="T">Type of <see cref="Component"/> to get from the scene</typeparam>
/// <returns>a generator that yields successive NetworkObjects in the current scene</returns>
public static IEnumerable<T> FromSceneByType<T>(Scene scene, bool includeInactive) where T : UnityEngine.Component
{
return new ObjectsInSceneEnumerator<T>(scene, includeInactive);
}
/// <summary>
/// An Enumerator that enumerates over each component of type <see cref="T"/> in the given scene.
/// </summary>
/// <typeparam name="T">Type of <see cref="Component"/> to get from the scene</typeparam>
private struct ObjectsInSceneEnumerator<T> : IEnumerable<T>, IEnumerator<T> where T : UnityEngine.Component
{
private readonly UnityEngine.GameObject[] m_RootObjects;
private int m_RootIndex;
private T[] m_CurrentChildObjects;
private int m_CurrentChildIndex;
private readonly bool m_IncludeInactive;
internal ObjectsInSceneEnumerator(Scene scene, bool includeInactive)
{
m_IncludeInactive = includeInactive;
// If the scene is invalid, use an empty array.
m_RootObjects = scene.IsValid() ? scene.GetRootGameObjects() : new UnityEngine.GameObject[0];
m_RootIndex = 0;
m_CurrentChildObjects = null;
m_CurrentChildIndex = 0;
Current = null;
}
public void Dispose() { }
public bool MoveNext()
{
while (m_CurrentChildObjects == null && m_RootIndex < m_RootObjects.Length)
{
m_CurrentChildObjects = m_RootObjects[m_RootIndex].GetComponentsInChildren<T>(m_IncludeInactive);
m_RootIndex++;
if (m_CurrentChildObjects.Length == 0)
{
m_CurrentChildObjects = null;
}
}
if (m_CurrentChildObjects != null && m_CurrentChildIndex < m_CurrentChildObjects.Length)
{
Current = m_CurrentChildObjects[m_CurrentChildIndex];
m_CurrentChildIndex++;
if (m_CurrentChildIndex >= m_CurrentChildObjects.Length)
{
m_CurrentChildIndex = 0;
m_CurrentChildObjects = null;
}
return true;
}
Current = null;
return false;
}
public void Reset()
{
m_RootIndex = 0;
m_CurrentChildObjects = null;
m_CurrentChildIndex = 0;
Current = null;
}
object IEnumerator.Current => Current;
public T Current { get; private set; }
public IEnumerator<T> GetEnumerator() => this;
IEnumerator IEnumerable.GetEnumerator() => this;
}
}
}