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 ;
}
}