当前位置:网站首页>Unity xlua monoproxy mono proxy class

Unity xlua monoproxy mono proxy class

2022-07-05 11:26:00 Handsome_ shuai_

Unity Xlua Mono proxy class (MonoProxy)

  • Xlua Used for mapping Unity And declare periodic functions
  • Use UniRx Can achieve update Multiple bindings of , It can be bound when necessary Update To improve performance
public class MonoProxy : MonoBehaviour
{
    
    public LuaTable luaTable;
    private Action<LuaTable> luaStart;
    private Action<LuaTable> luaOnDestroy;

    public LuaTable BindScript(string moduleName, string scriptPath)
    {
    
        Main.Instance.LuaEnv.DoString($"require('{
      scriptPath}')");
        string[] arr = scriptPath.Split('/');
        luaTable = Main.Instance.LuaEnv.Global.Get<LuaTable>(arr[arr.Length - 1]);
        luaTable.Set("MonoProxy", this);
        Action<LuaTable> luaAwake = luaTable.Get<Action<LuaTable>>("Awake");
        luaAwake?.Invoke(luaTable);
        luaTable.Get("Start", out luaStart);
        luaTable.Get("OnDestroy", out luaOnDestroy);
        return luaTable;
    }

    public void BindUpdate(Action action)
    {
    
        Observable.EveryUpdate().Subscribe(_ =>
        {
    
            action?.Invoke();
        }).AddTo(this);
    }

    private void Start()
    {
    
        luaStart?.Invoke(luaTable);
    }

    private void OnDestroy()
    {
    
        luaOnDestroy?.Invoke(luaTable);
    }
}
原网站

版权声明
本文为[Handsome_ shuai_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051123384693.html