diff --git a/Source/Engine/Engine/NativeInterop.Unmanaged.cs b/Source/Engine/Engine/NativeInterop.Unmanaged.cs index ae55f1ed7..bc2af6d61 100644 --- a/Source/Engine/Engine/NativeInterop.Unmanaged.cs +++ b/Source/Engine/Engine/NativeInterop.Unmanaged.cs @@ -908,12 +908,7 @@ namespace FlaxEngine.Interop while (unloading) System.Threading.Thread.Sleep(1); -#if FLAX_EDITOR - var isCollectible = true; -#else - var isCollectible = false; -#endif - scriptingAssemblyLoadContext = new AssemblyLoadContext("Flax", isCollectible); + InitScriptingAssemblyLoadContext(); DelegateHelpers.InitMethods(); } diff --git a/Source/Engine/Engine/NativeInterop.cs b/Source/Engine/Engine/NativeInterop.cs index 38af2e8fe..39adc59ab 100644 --- a/Source/Engine/Engine/NativeInterop.cs +++ b/Source/Engine/Engine/NativeInterop.cs @@ -78,6 +78,19 @@ namespace FlaxEngine.Interop return nativeLibrary; } + private static void InitScriptingAssemblyLoadContext() + { +#if FLAX_EDITOR + var isCollectible = true; +#else + var isCollectible = false; +#endif + scriptingAssemblyLoadContext = new AssemblyLoadContext("Flax", isCollectible); +#if FLAX_EDITOR + scriptingAssemblyLoadContext.Resolving += ScriptingAssemblyLoadContext_Resolving; +#endif + } + [UnmanagedCallersOnly] internal static unsafe void Init() { @@ -89,15 +102,21 @@ namespace FlaxEngine.Interop System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; -#if FLAX_EDITOR - var isCollectible = true; -#else - var isCollectible = false; -#endif - scriptingAssemblyLoadContext = new AssemblyLoadContext("Flax", isCollectible); + InitScriptingAssemblyLoadContext(); DelegateHelpers.InitMethods(); } + private static Assembly? ScriptingAssemblyLoadContext_Resolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName) + { + // FIXME: There should be a better way to resolve the path to EditorTargetPath where the dependencies are stored + string editorTargetPath = Path.GetDirectoryName(nativeLibraryPaths.Keys.First(x => x != "FlaxEngine")); + + var assemblyPath = Path.Combine(editorTargetPath, assemblyName.Name + ".dll"); + if (File.Exists(assemblyPath)) + return assemblyLoadContext.LoadFromAssemblyPath(assemblyPath); + return null; + } + [UnmanagedCallersOnly] internal static unsafe void Exit() {