Mathijs-Bakker / Extenject

Dependency Injection Framework for Unity Game Engine
MIT License
488 stars 91 forks source link

ReflectionBaking NullReferenceException #59

Open joaokucera opened 1 year ago

joaokucera commented 1 year ago

Describe the bug

NullReferenceException: Object reference not set to an instance of an object
Zenject.ReflectionBaking.ReflectionBakingInternalUtil.TryForceUnityFullCompile () (at Packages/com.gamehouse.yggdrasil.client.zenject/OptionalExtras/ReflectionBaking/Unity/ReflectionBakingInternalUtil.cs:72)
Zenject.ReflectionBaking.ZenjectReflectionBakingSettingsEditor.OnInspectorGUI () (at Packages/com.gamehouse.yggdrasil.client.zenject/OptionalExtras/ReflectionBaking/Unity/ZenjectReflectionBakingSettingsEditor.cs:116)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Editor/Inspector/InspectorElement.cs:636)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /Users/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)

To Reproduce I have added ZenjectReflectionBakingSettings and pressed the checkbox "Is Enabled In Editor".

Expected behavior No exception and recompile Unity.

Extenject and Unity info (please complete the following information):

Additional context dirtyAllScriptsMethod variables is null in the method below:

public static void TryForceUnityFullCompile()
{
    Type compInterface = typeof(UnityEditor.Editor).Assembly.GetType(
        "UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface");

    if (compInterface != null)
    {
        var dirtyAllScriptsMethod = compInterface.GetMethod(
            "DirtyAllScripts", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

        dirtyAllScriptsMethod.Invoke(null, null);
    }

    UnityEditor.AssetDatabase.Refresh();
}

Also, I have many asmdef files in my project, would it be an issue?

WhippetsAintDogs commented 9 months ago

Prior to Unity 2019.3, the UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface.DirtyAllScripts method existed and was used through reflection to force a recompilation. But now there is a public "preferred" way to do it without using reflection which is UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation.