1. 程式人生 > 其它 >Xlua學習筆記一——Lua對映c#生命週期函式

Xlua學習筆記一——Lua對映c#生命週期函式

技術標籤:XLualuaunity

思路非常簡單,在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