Unity3D-NLua icon indicating copy to clipboard operation
Unity3D-NLua copied to clipboard

Error: NotSupportedException - MonoPInvokeCallback (il2cpp)

Open Shark-vil opened this issue 5 years ago • 3 comments

When I try to compile my script in "il2cpp", I get this error:

System.NotSupportedException: To marshal a managed method, please add an attribute named 'MonoPInvokeCallback' to the method definition. The method we're attempting to marshal is: NLua.MetaFunctions::CollectObject
  at KeraLua.Lua.LuaPushStdCallCFunction (System.IntPtr luaState, KeraLua.LuaNativeFunction fn) [0x00000] in <00000000000000000000000000000000>:0 
  at NLua.ObjectTranslator.CreateBaseClassMetatable (KeraLua.LuaState luaState) [0x00000] in <00000000000000000000000000000000>:0 
  at NLua.ObjectTranslator..ctor (NLua.Lua interpreter, KeraLua.LuaState luaState) [0x00000] in <00000000000000000000000000000000>:0 
  at NLua.Lua.Init () [0x00000] in <00000000000000000000000000000000>:0 
  at NLua.Lua..ctor () [0x00000] in <00000000000000000000000000000000>:0 
  at InitAddons.InitLuaFile (System.IO.FileInfo fileInfo) [0x00000] in <00000000000000000000000000000000>:0 
  at InitAddons.FindLuaOnDir (System.String dirPath) [0x00000] in <00000000000000000000000000000000>:0 
  at InitAddons.FindAddons () [0x00000] in <00000000000000000000000000000000>:0 
  at InitAddons.Awake () [0x00000] in <00000000000000000000000000000000>:0 

How can it be solved? It appears when I try to create an object "Lua luaState = new Lua ();"

Code fragment (this procedure is called after two others which are called in "Awake"):

    private void InitLuaFile(FileInfo fileInfo)
    {
        string filePath = fileInfo.FullName;
        Lua luaState;
        try
        {
            /* ERROR LINE */
            luaState = new Lua();
        }
        catch(Exception ex)
        {
            uLog.LogError("Error create object  \"Lua\":\n" + ex);
            return;
        }

        luaState.LoadCLRPackage();

        string luaText = File.ReadAllText(filePath);

        Scene scene = SceneManager.GetActiveScene();
        luaState["UnitySceneName"] = scene.name;
        luaState["UnitySceneIndex"] = scene.buildIndex;

        luaState.DoString(luaText);

        luaFiles.Add(new LuaFile {
            state = luaState,
            path = filePath
        });

        int count = luaFiles.Count - 1;
        luaFunctionReplaceOrCreate("FixedUpdate", ref _fixed_update, luaFiles[count]);
        luaFunctionReplaceOrCreate("Update", ref _update, luaFiles[count]);
        luaFunctionReplaceOrCreate("LateUpdate", ref _late_update, luaFiles[count]);

        if (directoryWatcher.Find(x => x == fileInfo.DirectoryName) == null)
        {
            FileSystemWatcher watcher = new FileSystemWatcher(fileInfo.DirectoryName, "*.lua");
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Changed += ActionChangeLuaFile;
            watcher.EnableRaisingEvents = true;
        }
        else
            uLog.LogWarning("The folder is already listening!");
    }

Shark-vil avatar Oct 27 '19 18:10 Shark-vil

I'm getting this error as well.

wmanning-cra avatar May 31 '22 21:05 wmanning-cra