lua中c#变量传入和lua之间的回调

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' 相当于加载一个新lua脚本,在DoString里面会导致scriptEnv失效

你可能感兴趣的:(lua中c#变量传入和lua之间的回调)