Add Unity-style GetComponents scripting API
This commit is contained in:
@@ -34,6 +34,11 @@ namespace XCEngine
|
||||
return GameObject.GetComponent<T>();
|
||||
}
|
||||
|
||||
public T[] GetComponents<T>() where T : Component
|
||||
{
|
||||
return GameObject.GetComponents<T>();
|
||||
}
|
||||
|
||||
public T GetComponentInChildren<T>() where T : Component
|
||||
{
|
||||
return GameObject.GetComponentInChildren<T>();
|
||||
|
||||
@@ -67,6 +67,28 @@ namespace XCEngine
|
||||
return InternalCalls.GameObject_GetComponent(UUID, typeof(T)) as T;
|
||||
}
|
||||
|
||||
public T[] GetComponents<T>() where T : Component
|
||||
{
|
||||
Component[] components = InternalCalls.GameObject_GetComponents(UUID, typeof(T));
|
||||
if (components == null || components.Length == 0)
|
||||
{
|
||||
return System.Array.Empty<T>();
|
||||
}
|
||||
|
||||
if (components is T[] typedComponents)
|
||||
{
|
||||
return typedComponents;
|
||||
}
|
||||
|
||||
T[] result = new T[components.Length];
|
||||
for (int index = 0; index < components.Length; ++index)
|
||||
{
|
||||
result[index] = components[index] as T;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public T GetComponentInChildren<T>() where T : Component
|
||||
{
|
||||
return InternalCalls.GameObject_GetComponentInChildren(UUID, typeof(T)) as T;
|
||||
|
||||
@@ -86,6 +86,9 @@ namespace XCEngine
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern Component GameObject_GetComponent(ulong gameObjectUUID, Type componentType);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern Component[] GameObject_GetComponents(ulong gameObjectUUID, Type componentType);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern Component GameObject_GetComponentInChildren(ulong gameObjectUUID, Type componentType);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user