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;