lua中c#變量傳入和lua之間的回調(diào)

C#

using UnityEngine;
using System.Collections;
using XLua;
using System;
using System.IO;

public class star_coroutine : MonoBehaviour 
{
    //public static star_coroutine Instance
    //{
    //    get;
    //    private set;
    //}
    public TextAsset luaScript;
    LuaEnv lua = null;
    private LuaTable scriptEnv;
    public Injection[] injections;

    private Action luaStart;
    // Use this for initialization

    void Awake() 
    {
        lua = new LuaEnv();
        scriptEnv = lua.NewTable();
        LuaTable meta = lua.NewTable();
        meta.Set("__index", lua.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();
        scriptEnv.Set("self", this);
        scriptEnv.Set("offset", transform.Find("offset"));
        //lua.DoString("require 'uiset'");
        lua.DoString(luaScript.text, "star_coroutine", scriptEnv);
        scriptEnv.Get("Start", out luaStart);

    }
    void Start () {
        if (luaStart != null)
        {
            luaStart();
        }
    }

    void Update() 
    {
        if (lua != null) 
        {
            lua.Tick();
        }
    }


    void OnDestroy() 
    {       
        //lua.Dispose();
        scriptEnv.Dispose();
    }
}

Lua

lua_Tool

return
{
UIeven = function(tr,call)
    if tr ~= nil and call ~= nil then
        call(tr)
        print("tr is" .. tr.name)
    end
end
}

uiset

local UIevent=(require 'lua_Tool').UIeven
local CallBack=function (tr)
    tr.gameObject:SetActive(false)
    print("tr is false"..tr.name)
end
 
print("offset.name"..offset.name) 

function Start()
    UIevent(self.transform,CallBack)   
end

注意的是在DoString中使用require 'uiset' 相當(dāng)于加載一個(gè)新lua腳本,在DoString里面會(huì)導(dǎo)致scriptEnv失效

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容