找回密码
 立即注册
查看: 338|回复: 0

Unity XLua 热更框架

[复制链接]
发表于 2023-4-11 23:26 | 显示全部楼层 |阅读模式
Unity XLua 热更框架

一.框架设计图



二.自定义映射脚本
  1. using System;using UnityEngine;using XLua;[System.Serializable]publicclassInjection{publicstring name;publicGameObjectvalue;}[System.Serializable]publicclassInjectionLuaScript{publicstring name;publicLuaBehaviourvalue;}[CSharpCallLua]publicdelegatevoidCsCallLuaAction(paramsobject[] args);[CSharpCallLua]publicdelegateobject[]CsCallLuaActionReturn(paramsobject[] args);[LuaCallCSharp]publicclassLuaBehaviour: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;privateLuaTable _scriptEnv;[SerializeField]private Injection[] injections;[SerializeField]private InjectionLuaScript[] otherScripts;privatevoidAwake(){var globalEnv = LuaMgr.GetInstance().LuaEnv;
  2.         _scriptEnv = globalEnv.NewTable();var meta = globalEnv.NewTable();
  3.         meta.Set("__index", globalEnv.Global);
  4.         _scriptEnv.SetMetaTable(meta);
  5.         meta.Dispose();
  6.         _scriptEnv.Set("self",this);if(injections !=null&& injections.Length >0){foreach(var injection in injections){
  7.                 _scriptEnv.Set("g_"+ injection.name, injection.value);}}if(otherScripts !=null&& otherScripts.Length >0){foreach(var otherScript in otherScripts){
  8.                 _scriptEnv.Set("s_"+ otherScript.name, otherScript.value);}}var prefabName =this.name;if(prefabName.Contains("(Clone)")){
  9.             prefabName = prefabName.Split(new[]{"(Clone)"}, StringSplitOptions.RemoveEmptyEntries)[0];}
  10.         _luaAwake = _scriptEnv.GetInPath<Action<GameObject>>(prefabName +".Awake");
  11.         _luaStart = _scriptEnv.GetInPath<Action<GameObject>>(prefabName +".Start");
  12.         _luaEnable = _scriptEnv.GetInPath<Action<GameObject>>(prefabName +".OnEnable");
  13.         _luaUpdate = _scriptEnv.GetInPath<Action<GameObject>>(prefabName +".Update");
  14.         _luaFixUpdate = _scriptEnv.GetInPath<Action<GameObject>>(prefabName +".FixedUpdate");
  15.         _luaLateUpdate = _scriptEnv.GetInPath<Action<GameObject>>(prefabName +".LateUpdate");
  16.         _luaDisable = _scriptEnv.GetInPath<Action<GameObject>>(prefabName +".OnDisable");
  17.         _luaDestroy = _scriptEnv.GetInPath<Action<GameObject>>(prefabName +".OnDestroy");
  18.         
  19.         _luaAwake?.Invoke(gameObject);}publicvoidCallLuaFunction(string funcName,paramsobject[] args){var call = _scriptEnv.Get<CsCallLuaAction>(funcName);
  20.         call?.Invoke(args);}publicvoidCallLuaFunction(string funcName){var call = _scriptEnv.Get<CsCallLuaAction>(funcName);
  21.         call?.Invoke(null);}publicobject[]CallLuaFunctionReturn(string funcName,paramsobject[] args){var call = _scriptEnv.Get<CsCallLuaActionReturn>(funcName);return call?.Invoke(args);}publicobject[]CallLuaFunctionReturn(string funcName){var call = _scriptEnv.Get<CsCallLuaActionReturn>(funcName);return call?.Invoke(null);}privatevoidStart(){
  22.         _luaStart?.Invoke(gameObject);}privatevoidOnEnable(){
  23.         _luaEnable?.Invoke(gameObject);}privatevoidUpdate(){
  24.         _luaUpdate?.Invoke(gameObject);}privatevoidFixedUpdate(){
  25.         _luaFixUpdate?.Invoke(gameObject);}privatevoidLateUpdate(){
  26.         _luaLateUpdate?.Invoke(gameObject);}privatevoidOnDisable(){
  27.         _luaDisable?.Invoke(gameObject);}privatevoidOnDestroy(){
  28.         _luaDestroy?.Invoke(gameObject);
  29.         _luaAwake =null;
  30.         _luaStart =null;
  31.         _luaEnable =null;
  32.         _luaUpdate =null;
  33.         _luaFixUpdate =null;
  34.         _luaLateUpdate =null;
  35.         _luaDisable =null;
  36.         _luaDestroy =null;
  37.         _scriptEnv.Dispose();
  38.         _scriptEnv =null;
  39.         injections =null;
  40.         otherScripts =null;}}
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-23 08:00 , Processed in 0.102383 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表