Mervill / Unity3D-NLua

Everything you need to get started using Lua in Unity3D. Indie & Pro compatible.
Boost Software License 1.0
191 stars 51 forks source link

Error: NotSupportedException - MonoPInvokeCallback (il2cpp) #25

Open Shark-vil opened 4 years ago

Shark-vil commented 4 years ago

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!");
    }
wmanning-cra commented 2 years ago

I'm getting this error as well.

federicodangelosand commented 6 months ago

Did anyone have any luck getting this to work?

BrianOstrander commented 1 month ago

I'm having this issue as well, I'm a bit confused since I've heard of other people using this library with il2cpp without problems...