Unity XLua 热更框架

Unity XLua 热更框架

一.框架设计图

在这里插入图片描述

二.自定义映射脚本

using System;
using UnityEngine;
using XLua;

[System.Serializable]
public class Injection
{
    public string name;
    public GameObject value;
}

[System.Serializable]
public class InjectionLuaScript
{
    public string name;
    public LuaBehaviour value;
}

[CSharpCallLua]
public delegate void CsCallLuaAction(params object[] args);

[CSharpCallLua]
public delegate object[] CsCallLuaActionReturn(params object[] args);

[LuaCallCSharp]
public class LuaBehaviour : MonoBehaviour
{
    private Action<GameObject> _luaAwake;
    private Action<GameObject> _luaStart;
    private Action<GameObject> _luaEnable;
    private Action<GameObject> _luaUpdate;
    private Action<GameObject> _luaFixUpdate;
    private Action<GameObject> _luaLateUpdate;
    private Action<GameObject> _luaDisable;
    private Action<GameObject> _luaDestroy;
    private LuaTable _scriptEnv;

    [SerializeField] private Injection[] injections;
    [SerializeField] private InjectionLuaScript[] otherScripts;

    private void Awake()
    {
        var globalEnv = LuaMgr.GetInstance().LuaEnv;
        _scriptEnv = globalEnv.NewTable();
        var meta = globalEnv.NewTable();
        meta.Set("__index", globalEnv.Global);
        _scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        _scriptEnv.Set("self", this);

        if (injections != null && injections.Length > 0)
        {
            foreach (var injection in injections)
            {
                _scriptEnv.Set("g_" + injection.name, injection.value);
            }
        }

        if (otherScripts != null && otherScripts.Length > 0)
        {
            foreach (var otherScript in otherScripts)
            {
                _scriptEnv.Set("s_" + otherScript.name, otherScript.value);
            }
        }

        var prefabName = this.name;
        if (prefabName.Contains("(Clone)"))
        {
            prefabName = prefabName.Split(new[] {"(Clone)"}, StringSplitOptions.RemoveEmptyEntries)[0];
        }

        _luaAwake = _scriptEnv.GetInPath<Action<GameObject>>(prefabName + ".Awake");
        _luaStart = _scriptEnv.GetInPath<Action<GameObject>>(prefabName + ".Start");
        _luaEnable = _scriptEnv.GetInPath<Action<GameObject>>(prefabName + ".OnEnable");
        _luaUpdate = _scriptEnv.GetInPath<Action<GameObject>>(prefabName + ".Update");
        _luaFixUpdate = _scriptEnv.GetInPath<Action<GameObject>>(prefabName + ".FixedUpdate");
        _luaLateUpdate = _scriptEnv.GetInPath<Action<GameObject>>(prefabName + ".LateUpdate");
        _luaDisable = _scriptEnv.GetInPath<Action<GameObject>>(prefabName + ".OnDisable");
        _luaDestroy = _scriptEnv.GetInPath<Action<GameObject>>(prefabName + ".OnDestroy");
        
        _luaAwake?.Invoke(gameObject);
    }
    public void CallLuaFunction(string funcName, params object[] args)
    {
        var call = _scriptEnv.Get<CsCallLuaAction>(funcName);
        call?.Invoke(args);
    }
    public void CallLuaFunction(string funcName)
    {
        var call = _scriptEnv.Get<CsCallLuaAction>(funcName);
        call?.Invoke(null);
    }
    public object[] CallLuaFunctionReturn(string funcName, params object[] args)
    {
        var call = _scriptEnv.Get<CsCallLuaActionReturn>(funcName);
        return call?.Invoke(args);
    }
    public object[] CallLuaFunctionReturn(string funcName)
    {
        var call = _scriptEnv.Get<CsCallLuaActionReturn>(funcName);
        return call?.Invoke(null);
    }
    private void Start()
    {
        _luaStart?.Invoke(gameObject);
    }
    private void OnEnable()
    {
        _luaEnable?.Invoke(gameObject);
    }
    private void Update()
    {
        _luaUpdate?.Invoke(gameObject);
    }
    private void FixedUpdate()
    {
        _luaFixUpdate?.Invoke(gameObject);
    }
    private void LateUpdate()
    {
        _luaLateUpdate?.Invoke(gameObject);
    }
    private void OnDisable()
    {
        _luaDisable?.Invoke(gameObject);
    }
    private void OnDestroy()
    {
        _luaDestroy?.Invoke(gameObject);
        _luaAwake = null;
        _luaStart = null;
        _luaEnable = null;
        _luaUpdate = null;
        _luaFixUpdate = null;
        _luaLateUpdate = null;
        _luaDisable = null;
        _luaDestroy = null;
        _scriptEnv.Dispose();
        _scriptEnv = null;
        injections = null;
        otherScripts = null;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值