Unity3D LuaComponent(基于ulua)

Posted MrBlue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D LuaComponent(基于ulua)相关的知识,希望对你有一定的参考价值。

LuaComponent可以支持配一个需要执行在这个gameObject上的lua脚本,并且每个gameObject上的lua都是一个实例

 

using UnityEngine;
using LuaInterface;
using System.Collections.Generic;

//Lua组件 public class LuaComponent : MonoBehaviour
{
    //lua环境,需要在使用前给其赋值
    public static LuaState s_luaState; 

    //函数名字定义
    protected static class  FuncName
    {
        public static readonly string Awake = "Awake";
        public static readonly string OnEnable = "OnEnable";
        public static readonly string Start = "Start";
        public static readonly string Update = "Update";
        public static readonly string OnDisable = "OnDisable";
        public static readonly string OnDestroy = "OnDestroy";
    };

    //lua路径,不用填缀名,可以是bundle
    [Tooltip("script path")]
    public string LuaPath;

    //预存函数提高效率
    protected Dictionary<string, LuaFunction> mDictFunc = new Dictionary<string, LuaFunction>();

    //lua表,当gameObject销毁时要释放
    private LuaTable mSelfTable = null;

    //初始化函数,可以被重写,已添加其他
    protected virtual bool Init()
    {
        if (string.IsNullOrEmpty(LuaPath))
        {
            return false;
        }

        object[] luaRet = s_luaState.DoFile(LuaPath);
        if (luaRet == null || luaRet.Length < 1)
        {
            Debug.LogError("Lua must return a table " + LuaPath);
            return false;
        }

        mSelfTable = luaRet[0] as LuaTable;
        if (null == mSelfTable)
        {
            Debug.LogError("null == luaTable  " + LuaPath);
            return false;
        }

        AddFunc(FuncName.Awake);
        AddFunc(FuncName.OnEnable);
        AddFunc(FuncName.Start);
        AddFunc(FuncName.Update);
        AddFunc(FuncName.OnDisable);
        AddFunc(FuncName.OnDestroy);

        return true;
    }

    //保存函数
    protected bool AddFunc(string name)
    {
        var func = mSelfTable.GetLuaFunction(name);
        if (null == func)
        {
            return false;
        }
        mDictFunc.Add(name, func);
        return true;
    }

    //调用函数
    protected void CallLuaFunction(string name, params object[] args)
    {
        LuaFunction func = null;
        if (mDictFunc.TryGetValue(name, out func))
        {
            func.BeginPCall();
            foreach (var o in args)
            {
                func.Push(o);
            }
            func.PCall();
            func.EndPCall();
        }
    }

    void Awake()
    {
        Init();
        CallLuaFunction(FuncName.Awake,mSelfTable,gameObject);
    }

    void OnEnable()
    {
        CallLuaFunction(FuncName.OnEnable, mSelfTable, gameObject);
    }

    void Start()
    {
        CallLuaFunction(FuncName.Start, mSelfTable, gameObject);
    }

    void Update()
    {
        CallLuaFunction(FuncName.Update, mSelfTable, gameObject);
    }

    void OnDisable()
    {
        CallLuaFunction(FuncName.OnDisable, mSelfTable, gameObject);
    }

    void OnDestroy()
    {
        CallLuaFunction(FuncName.OnDestroy, mSelfTable, gameObject);

        //记得释放资源
        foreach (var pair in  mDictFunc)
        {
            pair.Value.Dispose();
        }
        mDictFunc.Clear();
        if (null != mSelfTable)
        {
            mSelfTable.Dispose();
            mSelfTable = null;
        }
    }

}

 

lua脚本形如,记得最后一定要return 这个表 而且每个变量都得是local的

local Player = {}

local transform = nil;

local characterController = nil;
local moveDirection = Vector3.zero;

function Player:Awake(gameObject)
    print("Awake");

    transform = gameObject.transform;
    characterController = gameObject:GetComponent(CharacterController);
end

function Player:Start( gameObject )  
    print("Start")  
    
    --gameObject.transform.localPosition = Vector3.New(200,100);
end

function Player:OnDestroy( gameObject )  
    print("OnDestroy")  
end

function Player:Update(gameObject)
    if (characterController.isGrounded) then        
        moveDirection = Vector3.New(Input.GetAxis("Horizontal"), 0, 0);
        moveDirection = transform:TransformDirection(moveDirection);
        moveDirection = moveDirection * 6;
    end    
        -- Apply gravity
        moveDirection.y =moveDirection.y- 20 * Time.deltaTime;
        characterController:Move(moveDirection * Time.deltaTime);
        
end

return Player;

 

以上是关于Unity3D LuaComponent(基于ulua)的主要内容,如果未能解决你的问题,请参考以下文章

在Unity3D中基于订阅者模式实现事件机制

Thinking in Unity3D:基于物理着色(PBS)的材质系统

基于鼠标的瞄准 Unity3d

Unity3D:导入关键帧动画(不是基于骨骼的动画)

Unity3D LuaBundleLoader(基于cslua)

基于Unity3D的体素沙盒游戏设计与实现(上)