Xlua学习笔记一——Lua映射c#生命周期函数
作者:互联网
思路非常简单,在c#脚本中每个生命周期函数中执行一个事件,该事件用于在lua中调用,然后再写一个添加或者删除事件的函数即可。
之后如果lua中逻辑要通过生命周期函数实现,在物体上添加该脚本组件,然后添加对应周期函数事件就行了。
直接上代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class LuaMonoMgr : MonoBehaviour
{
private UnityAction start;
private UnityAction update;
private UnityAction fixedUpdate;
private UnityAction lateUpdate;
private UnityAction onEnable;
private UnityAction onDisable;
private UnityAction onDestroy;
private void Start()
{
if (start != null)
start();
}
private void Update()
{
if (update != null)
update();
}
private void FixedUpdate()
{
if (fixedUpdate != null)
fixedUpdate();
}
private void LateUpdate()
{
if (lateUpdate != null)
lateUpdate();
}
private void OnEnable()
{
if (onEnable != null)
onEnable();
}
private void OnDisable()
{
if (onDisable != null)
onDisable();
}
private void OnDestroy()
{
if (onDestroy != null)
onDestroy();
start = null;
update = null;
fixedUpdate = null;
lateUpdate = null;
onEnable = null;
onDisable = null;
onDestroy = null;
}
public void AddOrRemoveListener(UnityAction fun , E_LifeFun_Type type, bool isAdd = true)
{
switch (type)
{
case E_LifeFun_Type.Start:
if (isAdd)
start += fun;
else
start -= fun;
break;
case E_LifeFun_Type.Update:
if (isAdd)
update += fun;
else
update -= fun;
break;
case E_LifeFun_Type.LateUpdate:
if (isAdd)
lateUpdate += fun;
else
lateUpdate -= fun;
break;
case E_LifeFun_Type.FixedUpdate:
if (isAdd)
fixedUpdate += fun;
else
fixedUpdate -= fun;
break;
case E_LifeFun_Type.OnEnable:
if (isAdd)
onEnable += fun;
else
onEnable -= fun;
break;
case E_LifeFun_Type.OnDisable:
if (isAdd)
onDisable += fun;
else
onDisable -= fun;
break;
case E_LifeFun_Type.OnDestroy:
if (isAdd)
onDestroy += fun;
else
onDestroy -= fun;
break;
}
}
}
public enum E_LifeFun_Type
{
Start,
Update,
LateUpdate,
FixedUpdate,
OnEnable,
OnDisable,
OnDestroy
}
调用逻辑:
LuaMonoMgr = CS.LuaMonoMgr
E_LifeFun_Type = CS.E_LifeFun_Type
GameObject = CS.UnityEngine.GameObject
luaMgr = GameObject.Find("Main Camera"):AddComponent(typeof(LuaMonoMgr))
luaMgr:AddOrRemoveListener(self.Start,E_LifeFun_Type.Start)
function Camera.Start()
--body
end
标签:c#,void,private,Lua,LifeFun,Xlua,fun,null,Type 来源: https://blog.csdn.net/qq_39029746/article/details/113606842