Add FindActor/FindActors with Tag to Level
This commit is contained in:
@@ -755,6 +755,59 @@ int32 Level::GetLayerIndex(const StringView& layer)
|
||||
return result;
|
||||
}
|
||||
|
||||
Actor* FindActorRecursive(Actor* node, const Tag& tag)
|
||||
{
|
||||
if (node->HasTag(tag))
|
||||
return node;
|
||||
Actor* result = nullptr;
|
||||
for (Actor* child : node->Children)
|
||||
{
|
||||
result = FindActorRecursive(child, tag);
|
||||
if (result)
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Actor* Level::FindActor(const Tag& tag, Actor* root)
|
||||
{
|
||||
PROFILE_CPU();
|
||||
if (root)
|
||||
return FindActorRecursive(root, tag);
|
||||
Actor* result = nullptr;
|
||||
for (Scene* scene : Scenes)
|
||||
{
|
||||
result = FindActorRecursive(scene, tag);
|
||||
if (result)
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void FindActorRecursive(Actor* node, const Tag& tag, Array<Actor*>& result)
|
||||
{
|
||||
if (node->HasTag(tag))
|
||||
result.Add(node);
|
||||
for (Actor* child : node->Children)
|
||||
FindActorRecursive(child, tag, result);
|
||||
}
|
||||
|
||||
Array<Actor*> Level::FindActors(const Tag& tag, Actor* root)
|
||||
{
|
||||
PROFILE_CPU();
|
||||
Array<Actor*> result;
|
||||
if (root)
|
||||
{
|
||||
FindActorRecursive(root, tag);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Scene* scene : Scenes)
|
||||
FindActorRecursive(scene, tag);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void Level::callActorEvent(ActorEventType eventType, Actor* a, Actor* b)
|
||||
{
|
||||
PROFILE_CPU();
|
||||
|
||||
@@ -16,6 +16,7 @@ class Engine;
|
||||
struct RenderView;
|
||||
struct RenderContext;
|
||||
struct RenderContextBatch;
|
||||
struct Tag;
|
||||
|
||||
/// <summary>
|
||||
/// The scene manager that contains the loaded scenes collection and spawns/deleted actors.
|
||||
@@ -461,6 +462,23 @@ public:
|
||||
/// </summary>
|
||||
API_FUNCTION() static int32 GetLayerIndex(const StringView& layer);
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Tries to find the actor with the given tag (returns the first one found).
|
||||
/// </summary>
|
||||
/// <param name="tag">The tag of the actor to search for.</param>
|
||||
/// <param name="root">The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes.</param>
|
||||
/// <returns>Found actor or null.</returns>
|
||||
API_FUNCTION() static Actor* FindActor(const Tag& tag, Actor* root = nullptr);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to find the actors with the given tag (returns all found).
|
||||
/// </summary>
|
||||
/// <param name="tag">The tag of the actor to search for.</param>
|
||||
/// <param name="root">The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes.</param>
|
||||
/// <returns>Found actors or empty if none.</returns>
|
||||
API_FUNCTION() static Array<Actor*> FindActors(const Tag& tag, Actor* root = nullptr);
|
||||
|
||||
private:
|
||||
// Actor API
|
||||
enum class ActorEventType
|
||||
|
||||
@@ -166,7 +166,7 @@ namespace FlaxEngine
|
||||
{
|
||||
if (list == null)
|
||||
return false;
|
||||
foreach (Tag tag in list)
|
||||
foreach (Tag tag in tags)
|
||||
{
|
||||
if (HasTag(list, tag))
|
||||
return true;
|
||||
@@ -184,7 +184,7 @@ namespace FlaxEngine
|
||||
{
|
||||
if (list == null)
|
||||
return false;
|
||||
foreach (Tag tag in list)
|
||||
foreach (Tag tag in tags)
|
||||
{
|
||||
if (HasTagExact(list, tag))
|
||||
return true;
|
||||
@@ -204,7 +204,7 @@ namespace FlaxEngine
|
||||
return true;
|
||||
if (list == null || list.Length == 0)
|
||||
return false;
|
||||
foreach (Tag tag in list)
|
||||
foreach (Tag tag in tags)
|
||||
{
|
||||
if (!HasTag(list, tag))
|
||||
return false;
|
||||
@@ -224,7 +224,7 @@ namespace FlaxEngine
|
||||
return true;
|
||||
if (list == null || list.Length == 0)
|
||||
return false;
|
||||
foreach (Tag tag in list)
|
||||
foreach (Tag tag in tags)
|
||||
{
|
||||
if (!HasTagExact(list, tag))
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user