diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index 04918f24b..219893012 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -1326,12 +1326,8 @@ Actor* Level::FindActor(const MClass* type) CHECK_RETURN(type, nullptr); Actor* result = nullptr; ScopeLock lock(ScenesLock); - for (int32 i = 0; result == nullptr && i < Scenes.Count(); i++) - { result = Scenes[i]->FindActor(type); - } - return result; } @@ -1340,25 +1336,57 @@ Script* Level::FindScript(const MClass* type) CHECK_RETURN(type, nullptr); Script* result = nullptr; ScopeLock lock(ScenesLock); - for (int32 i = 0; result == nullptr && i < Scenes.Count(); i++) - { result = Scenes[i]->FindScript(type); + return result; +} + +namespace +{ + void GetActors(const MClass* type, Actor* actor, Array& result) + { + if (actor->GetClass()->IsSubClassOf(type)) + result.Add(actor); + for (auto child : actor->Children) + GetActors(type, child, result); } + void GetScripts(const MClass* type, Actor* actor, Array& result) + { + for (auto script : actor->Scripts) + if (script->GetClass()->IsSubClassOf(type)) + result.Add(script); + for (auto child : actor->Children) + GetScripts(type, child, result); + } +} + +Array Level::GetActors(const MClass* type) +{ + Array result; + CHECK_RETURN(type, result); + ScopeLock lock(ScenesLock); + for (int32 i = 0; i < Scenes.Count(); i++) + ::GetActors(type, Scenes[i], result); + return result; +} + +Array Level::GetScripts(const MClass* type) +{ + Array result; + CHECK_RETURN(type, result); + ScopeLock lock(ScenesLock); + for (int32 i = 0; i < Scenes.Count(); i++) + ::GetScripts(type, Scenes[i], result); return result; } Scene* Level::FindScene(const Guid& id) { ScopeLock lock(ScenesLock); - for (int32 i = 0; i < Scenes.Count(); i++) - { if (Scenes[i]->GetID() == id) return Scenes[i]; - } - return nullptr; } diff --git a/Source/Engine/Level/Level.cs b/Source/Engine/Level/Level.cs index 6232d2e54..1eafe08d5 100644 --- a/Source/Engine/Level/Level.cs +++ b/Source/Engine/Level/Level.cs @@ -77,5 +77,33 @@ namespace FlaxEngine { return FindActor(id) as T; } + + /// + /// Finds all the scripts of the given type in all the loaded scenes. + /// + /// Type of the object. + /// Found scripts list. + public static T[] GetScripts() where T : Script + { + var scripts = GetScripts(typeof(T)); + var result = new T[scripts.Length]; + for (int i = 0; i < scripts.Length; i++) + result[i] = scripts[i] as T; + return result; + } + + /// + /// Finds all the actors of the given type in all the loaded scenes. + /// + /// Type of the object. + /// Found actors list. + public static T[] GetActors() where T : Actor + { + var actors = GetActors(typeof(T)); + var result = new T[actors.Length]; + for (int i = 0; i < actors.Length; i++) + result[i] = actors[i] as T; + return result; + } } } diff --git a/Source/Engine/Level/Level.h b/Source/Engine/Level/Level.h index 0a10b58d2..5e274978a 100644 --- a/Source/Engine/Level/Level.h +++ b/Source/Engine/Level/Level.h @@ -377,6 +377,20 @@ public: return (T*)FindScript(T::GetStaticClass()); } + /// + /// Finds all the actors of the given type in all the loaded scenes. + /// + /// Type of the actor to search for. Includes any actors derived from the type. + /// Found actors list. + API_FUNCTION() static Array GetActors(const MClass* type); + + /// + /// Finds all the scripts of the given type in all the loaded scenes. + /// + /// Type of the script to search for. Includes any scripts derived from the type. + /// Found scripts list. + API_FUNCTION() static Array GetScripts(const MClass* type); + /// /// Tries to find scene with given ID. ///