diff --git a/Source/Engine/Level/Actor.cpp b/Source/Engine/Level/Actor.cpp index f7ccb388f..73ef072b3 100644 --- a/Source/Engine/Level/Actor.cpp +++ b/Source/Engine/Level/Actor.cpp @@ -498,10 +498,21 @@ Script* Actor::GetScript(int32 index) const Script* Actor::GetScript(const MClass* type) const { CHECK_RETURN(type, nullptr); - for (auto script : Scripts) + if (type->IsInterface()) { - if (script->GetClass()->IsSubClassOf(type)) - return script; + for (auto script : Scripts) + { + if (script->GetClass()->HasInterface(type)) + return script; + } + } + else + { + for (auto script : Scripts) + { + if (script->GetClass()->IsSubClassOf(type)) + return script; + } } return nullptr; } @@ -509,9 +520,18 @@ Script* Actor::GetScript(const MClass* type) const Array Actor::GetScripts(const MClass* type) const { Array result; - for (auto script : Scripts) - if (script->GetClass()->IsSubClassOf(type)) - result.Add(script); + if (type->IsInterface()) + { + for (auto script : Scripts) + if (script->GetClass()->HasInterface(type)) + result.Add(script); + } + else + { + for (auto script : Scripts) + if (script->GetClass()->IsSubClassOf(type)) + result.Add(script); + } return result; } diff --git a/Source/Engine/Level/Actor.cs b/Source/Engine/Level/Actor.cs index 2a8d8039a..bd005d415 100644 --- a/Source/Engine/Level/Actor.cs +++ b/Source/Engine/Level/Actor.cs @@ -222,7 +222,7 @@ namespace FlaxEngine /// /// Type of the script to search for. Includes any scripts derived from the type. /// The script or null if failed to find. - public T GetScript() where T : Script + public T GetScript() where T : class { return GetScript(typeof(T)) as T; } @@ -233,7 +233,7 @@ namespace FlaxEngine /// Type of the script to search for. Includes any scripts derived from the type. /// The returned script, valid only if method returns true. /// True if found a script of that type or false if failed to find. - public bool TryGetScript(out T script) where T : Script + public bool TryGetScript(out T script) where T : class { script = GetScript(typeof(T)) as T; return script != null; @@ -244,7 +244,7 @@ namespace FlaxEngine /// /// Type of the object. /// Script instance if found, null otherwise. - public T FindScript() where T : Script + public T FindScript() where T : class { return FindScript(typeof(T)) as T; } @@ -290,7 +290,7 @@ namespace FlaxEngine /// /// Type of the scripts to search for. Includes any scripts derived from the type. /// All scripts matching the specified type. - public T[] GetScripts() where T : Script + public T[] GetScripts() where T : class { var count = ScriptsCount; var length = 0; diff --git a/Source/Engine/Scripting/ManagedCLR/MClass.cpp b/Source/Engine/Scripting/ManagedCLR/MClass.cpp index a443a4c19..35ef42680 100644 --- a/Source/Engine/Scripting/ManagedCLR/MClass.cpp +++ b/Source/Engine/Scripting/ManagedCLR/MClass.cpp @@ -124,6 +124,15 @@ bool MClass::IsSubClassOf(const MonoClass* monoClass) const } #endif +bool MClass::HasInterface(const MClass* klass) const +{ +#if USE_MONO + return klass && mono_class_is_assignable_from(klass->GetNative(), _monoClass) != 0; +#else + return false; +#endif +} + bool MClass::IsInstanceOfType(MObject* object) const { if (object == nullptr)