Skip to content
This repository was archived by the owner on Dec 24, 2020. It is now read-only.

Commit 6f0ca72

Browse files
committed
Add utility method for finding scripts by type
1 parent aff7173 commit 6f0ca72

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

FlaxEngine/API/Objects/Actor.Gen.cs

+21
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,24 @@ public Actor FindActor(Type type)
393393
#endif
394394
}
395395

396+
/// <summary>
397+
/// Tries to find the script of the given type in this actor tree (checks this actor and all children trees).
398+
/// </summary>
399+
/// <param name="type">The type of the script to find.</param>
400+
/// <returns>Script instance if found, null otherwise.</returns>
401+
#if UNIT_TEST_COMPILANT
402+
[Obsolete("Unit tests, don't support methods calls.")]
403+
#endif
404+
[UnmanagedCall]
405+
public Script FindScript(Type type)
406+
{
407+
#if UNIT_TEST_COMPILANT
408+
throw new NotImplementedException("Unit tests, don't support methods calls. Only properties can be get or set.");
409+
#else
410+
return Internal_FindScriptByType(unmanagedPtr, type);
411+
#endif
412+
}
413+
396414
/// <summary>
397415
/// Tries to find the actor with the given name in all the loaded scenes.
398416
/// </summary>
@@ -922,6 +940,9 @@ public static Guid[] TryGetSerializedObjectsIds(byte[] data)
922940
[MethodImpl(MethodImplOptions.InternalCall)]
923941
internal static extern Actor Internal_FindActorByType(IntPtr obj, Type type);
924942

943+
[MethodImpl(MethodImplOptions.InternalCall)]
944+
internal static extern Script Internal_FindScriptByType(IntPtr obj, Type type);
945+
925946
[MethodImpl(MethodImplOptions.InternalCall)]
926947
internal static extern Actor Internal_FindByName(string name);
927948

FlaxEngine/API/Objects/Actor.cs

+18
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,24 @@ public T FindActor<T>() where T : Actor
260260
#endif
261261
}
262262

263+
/// <summary>
264+
/// Tries to find the script of the given type in this script tree (checks this actor and all children trees).
265+
/// </summary>
266+
/// <typeparam name="T">The type of the script to find.</typeparam>
267+
/// <returns>Script instance if found, null otherwise.</returns>
268+
#if UNIT_TEST_COMPILANT
269+
[Obsolete("Unit tests, don't support methods calls.")]
270+
#endif
271+
[UnmanagedCall]
272+
public T FindScript<T>() where T : Script
273+
{
274+
#if UNIT_TEST_COMPILANT
275+
throw new NotImplementedException("Unit tests, don't support methods calls. Only properties can be get or set.");
276+
#else
277+
return Internal_FindScriptByType(unmanagedPtr, typeof(T)) as T;
278+
#endif
279+
}
280+
263281
/// <summary>
264282
/// Tries to find the actor of the given type in all the loaded scenes.
265283
/// </summary>

FlaxEngine/API/Objects/Script.Gen.cs

+21
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,24 @@ public void BreakPrefabLink()
115115
#endif
116116
}
117117

118+
/// <summary>
119+
/// Tries to find the script of the given type in all the loaded scenes.
120+
/// </summary>
121+
/// <param name="type">The type of the script to find.</param>
122+
/// <returns>Script instance if found, null otherwise.</returns>
123+
#if UNIT_TEST_COMPILANT
124+
[Obsolete("Unit tests, don't support methods calls.")]
125+
#endif
126+
[UnmanagedCall]
127+
public static Script Find(Type type)
128+
{
129+
#if UNIT_TEST_COMPILANT
130+
throw new NotImplementedException("Unit tests, don't support methods calls. Only properties can be get or set.");
131+
#else
132+
return Internal_FindByType(type);
133+
#endif
134+
}
135+
118136
#region Internal Calls
119137

120138
#if !UNIT_TEST_COMPILANT
@@ -147,6 +165,9 @@ public void BreakPrefabLink()
147165

148166
[MethodImpl(MethodImplOptions.InternalCall)]
149167
internal static extern void Internal_BreakPrefabLink(IntPtr obj);
168+
169+
[MethodImpl(MethodImplOptions.InternalCall)]
170+
internal static extern Script Internal_FindByType(Type type);
150171
#endif
151172

152173
#endregion

FlaxEngine/API/Objects/Script.cs

+18
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ public Transform LocalTransform
4444
set => Actor.LocalTransform = value;
4545
}
4646

47+
/// <summary>
48+
/// Tries to find the script of the given type in all the loaded scenes.
49+
/// </summary>
50+
/// <typeparam name="T">The type of the script to find.</typeparam>
51+
/// <returns>Script instance if found, null otherwise.</returns>
52+
#if UNIT_TEST_COMPILANT
53+
[Obsolete("Unit tests, don't support methods calls.")]
54+
#endif
55+
[UnmanagedCall]
56+
public static T Find<T>() where T : Script
57+
{
58+
#if UNIT_TEST_COMPILANT
59+
return null;
60+
#else
61+
return Internal_FindByType(typeof(T)) as T;
62+
#endif
63+
}
64+
4765
#region Internal Calls
4866

4967
#if !UNIT_TEST_COMPILANT

0 commit comments

Comments
 (0)