|
Unity XLua 热更框架
一.框架设计图
二.自定义映射脚本
- 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;
- _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);}publicvoidCallLuaFunction(string funcName,paramsobject[] args){var call = _scriptEnv.Get<CsCallLuaAction>(funcName);
- call?.Invoke(args);}publicvoidCallLuaFunction(string funcName){var call = _scriptEnv.Get<CsCallLuaAction>(funcName);
- 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(){
- _luaStart?.Invoke(gameObject);}privatevoidOnEnable(){
- _luaEnable?.Invoke(gameObject);}privatevoidUpdate(){
- _luaUpdate?.Invoke(gameObject);}privatevoidFixedUpdate(){
- _luaFixUpdate?.Invoke(gameObject);}privatevoidLateUpdate(){
- _luaLateUpdate?.Invoke(gameObject);}privatevoidOnDisable(){
- _luaDisable?.Invoke(gameObject);}privatevoidOnDestroy(){
- _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;}}
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|