Add Unity-style GetComponents scripting API

This commit is contained in:
2026-04-03 14:51:52 +08:00
parent 5225faff1d
commit 0f51f553c8
8 changed files with 240 additions and 10 deletions

View File

@@ -101,6 +101,7 @@ set(XCENGINE_GAME_SCRIPT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/GameScripts/BuiltinComponentProbe.cs
${CMAKE_CURRENT_SOURCE_DIR}/GameScripts/AddComponentProbe.cs
${CMAKE_CURRENT_SOURCE_DIR}/GameScripts/FieldMetadataProbe.cs
${CMAKE_CURRENT_SOURCE_DIR}/GameScripts/GetComponentsProbe.cs
${CMAKE_CURRENT_SOURCE_DIR}/GameScripts/ScriptComponentApiProbe.cs
${CMAKE_CURRENT_SOURCE_DIR}/GameScripts/RuntimeGameObjectProbe.cs
${CMAKE_CURRENT_SOURCE_DIR}/GameScripts/HierarchyProbe.cs

View File

@@ -0,0 +1,49 @@
using System;
using XCEngine;
namespace Gameplay
{
public sealed class GetComponentsProbe : MonoBehaviour
{
public int ObservedTransformCount;
public int ObservedCameraCount;
public int ObservedCameraCountViaGameObject;
public int ObservedMarkerCount;
public int ObservedMeshRendererCount;
public bool ObservedAllMarkersNonNull;
public bool ObservedFirstMarkerMatchesGetComponent;
public bool ObservedMarkerInstancesAreDistinct;
public bool ObservedTransformBoundToHost;
public string ObservedFirstMarkerHostName = string.Empty;
public void Start()
{
Transform[] transforms = GetComponents<Transform>();
Camera[] cameras = GetComponents<Camera>();
Camera[] camerasViaGameObject = gameObject.GetComponents<Camera>();
ObjectApiMarkerProbe[] markers = GetComponents<ObjectApiMarkerProbe>();
MeshRenderer[] meshRenderers = GetComponents<MeshRenderer>();
ObjectApiMarkerProbe firstMarker = GetComponent<ObjectApiMarkerProbe>();
ObservedTransformCount = transforms.Length;
ObservedCameraCount = cameras.Length;
ObservedCameraCountViaGameObject = camerasViaGameObject.Length;
ObservedMarkerCount = markers.Length;
ObservedMeshRendererCount = meshRenderers.Length;
ObservedAllMarkersNonNull = Array.TrueForAll(markers, marker => marker != null);
ObservedFirstMarkerMatchesGetComponent = firstMarker != null
&& markers.Length > 0
&& ReferenceEquals(markers[0], firstMarker);
ObservedMarkerInstancesAreDistinct = markers.Length >= 2
&& !ReferenceEquals(markers[0], markers[1]);
ObservedTransformBoundToHost = transforms.Length == 1
&& transforms[0] != null
&& transforms[0].GameObjectUUID == GameObjectUUID;
if (markers.Length > 0 && markers[0] != null)
{
ObservedFirstMarkerHostName = markers[0].gameObject.name;
}
}
}
}

View File

@@ -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>();

View File

@@ -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;

View File

@@ -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);