flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

get_dataPath is not allowed to be called from a ScriptableObject constructor

Open pablott opened this issue 3 years ago • 4 comments

Describe the bug I followed the steps to install the script but on compile I get the following error in Unity's console:

UnityException: get_dataPath is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject 'Build'.
See "Script Serialization" page in the Unity Manual for further details.
Build..cctor () (at Assets/FlutterUnityIntegration/Editor/Build.cs:12)
Rethrow as TypeInitializationException: The type initializer for 'Build' threw an exception.

To Reproduce Steps to reproduce the behavior:

  1. Install script on Unity 2020.3.19f1
  2. Open console and read log
  3. Flutter export to iOS/Android is present in the menu but does nothing

Expected behavior The script should compile. The menu options should work.

Unity (please complete the following information):

  • macOS 12.1, M1
  • 2020.3.19f1
  • Note: I tried installing the script in latest LTS (2020.3.26f1) and it never shows in the menu.

Additional context Defining these properties as writable statics works:

public class Build : EditorWindow
{
    // Define and set later on OnEnable
    static string ProjectPath = "";
    static string apkPath = "";
    static string androidExportPath = "";
    static string iosExportPath = "";
    static string iosExportPluginPath = "";
    // ...

And then assigning values in OnEnable() (note: error is still displayed in the console):

    private void OnEnable()
    {
        // Assign value OnEnable to avoid error
        // UnityException: get_dataPath is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject 'Build'.
        ProjectPath = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
        apkPath = Path.Combine(ProjectPath, "Builds/" + Application.productName + ".apk");
        androidExportPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../android/unityLibrary"));
        iosExportPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../ios/UnityLibrary"));
        iosExportPluginPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../ios_xcode/UnityLibrary"));

        pluginMode = EditorPrefs.GetBool(persistentKey, false);
    }

Now it exports to ../../ios/unity/ apparently correct.

I also had to change AcceptExternalModificationsToPlayer to AllowDebugging. I may open a different issue for that.

pablott avatar Jan 20 '22 13:01 pablott

If you need anything else, please let me know. Thanks.

pablott avatar Jan 20 '22 13:01 pablott

@juicycleff We are very interested in using this in our project. We want to make an Android/iOS app with a Unity component, so this could be useful. I'm having quite a few issues getting it to run though, latest LTS does not even load the script.

pablott avatar Jan 21 '22 20:01 pablott

Build-fixed.cs.zip

pablott avatar Jan 21 '22 20:01 pablott

I forgot to mention that you have to open the plugin window first (Flutter-> Settings) or else the export will not work. I think the OnEnable part is not initialized if you don't do this.

This is the error:

ArgumentException: The 'locationPathName' parameter for BuildPipeline.BuildPlayer should not be null or empty.
UnityEditor.BuildPipeline.BuildPlayer (System.String[] scenes, System.String locationPathName, System.String assetBundleManifestPath, UnityEditor.BuildTargetGroup buildTargetGroup, UnityEditor.BuildTarget target, UnityEditor.BuildOptions options, System.String[] extraScriptingDefines) (at /Users/bokken/buildslave/unity/build/Editor/Mono/BuildPipeline.bindings.cs:329)
UnityEditor.BuildPipeline.BuildPlayer (UnityEditor.BuildPlayerOptions buildPlayerOptions) (at /Users/bokken/buildslave/unity/build/Editor/Mono/BuildPipeline.bindings.cs:295)
UnityEditor.BuildPipeline.BuildPlayer (System.String[] levels, System.String locationPathName, UnityEditor.BuildTarget target, UnityEditor.BuildOptions options) (at /Users/bokken/buildslave/unity/build/Editor/Mono/BuildPipeline.bindings.cs:290)
Build.BuildIOS (System.String path) (at Assets/FlutterUnityIntegration/Editor/Build.cs:160)
Build.DoBuildIOS () (at Assets/FlutterUnityIntegration/Editor/Build.cs:102)

pablott avatar Jan 25 '22 12:01 pablott

Closing this as outdated. This looks specific to older unitypackages, probably in the 4.x range.

timbotimbo avatar May 17 '23 14:05 timbotimbo