直接举个例子
首先直接在场景里边新建一个LuaMgr的空物体
然后挂上一个脚本using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
public class HotFixScript : MonoBehaviour
{
private LuaEnv lua;
// Start is called before the first frame update
void Awake()
{
lua = new LuaEnv();
lua.AddLoader(MyLoader);
lua.DoString(@“require ‘game’”);
}
private byte[] MyLoader(ref string fileluaname)
{
string absPath = Application.dataPath+@"\LuaText\" + fileluaname + ".lua";
Debug.Log("abspath"+absPath);
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
}
// Update is called once per frame
private void OnDestroy()
{
lua.Dispose();
}
private void OnDisable()
{
lua.DoString(@"require 'gameDispose'");
}
}
game.lua和gameDispose.lua脚本如下
GameObject=CS.UnityEngine.GameObject
Input=CS.UnityEngine.Input
PlayerMove=CS.PlayerMove
Vector3 =CS.UnityEngine.Vector3
Time=CS.UnityEngine.Time
Quaternion=CS.UnityEngine.Quaternion
Destroy=CS.UnityEngine.GameObject.Destroy
--LUA实现人物移动
xlua.private_accessible(PlayerMove)
xlua.hotfix(PlayerMove,'Update',function(self)
-- local moveSpeed= self.moveSpeed
--local rotateSpeed=self.rotateSpeed
-- print('Time'..Time.deltaTime)
-- print('moveSpeed'..':'..self.moveSpeed)
-- print('rotateSpeed'..':'..self.rotateSpeed)
-- print('vinput'..self.vInput)
-- print('hInput'..self.hInput)
self.vInput=Input.GetAxis('Vertical') * (self.moveSpeed)
self.hInput=Input.GetAxis('Horizontal') * (self.rotateSpeed)
--self.transform:Translate(Vector3.forward * self.moveSpeed * self.vInput * Time.deltaTime)
-- self.transform:Rotate(Vector3.up * self.hInput * self.rotateSpeed * Time.deltaTime)
end)
xlua.hotfix(PlayerMove,'Start',function (self)
self.Rigidbody=self.transform:GetComponent('Rigidbody')
end)
xlua.hotfix(PlayerMove,'FixedUpdate',function (self)
local rotation=Vector3.up*self.hInput
local angleRot=Quaternion.Euler(rotation* Time.fixedDeltaTime)
self.Rigidbody:MovePosition(self.transform.position+self.transform.forward*self.vInput* Time.fixedDeltaTime)
self.Rigidbody:MoveRotation(self.Rigidbody.rotation*angleRot);
end)
--实现人物摄像机跟随
CameraMove=CS.CameraMove
xlua.private_accessible(CameraMove)
xlua.hotfix(CameraMove,'Start',function (self)
self.camoffset=Vector3(0,1.2,-2.6)
self.target=GameObject.Find('Player').transform
print("cam开始执行")
end)
xlua.hotfix(CameraMove,'LateUpdate',function (self)
self.transform.position=self.target.TransformPoint(self.target,self.camoffset)
self.transform:LookAt(self.target)
end)
--物体被捡起来的脚本
ItemScript=CS.ItemScript
xlua.private_accessible(ItemScript)
xlua.hotfix(ItemScript,'OnCollisionEnter',function (self,collision)
print("hello")
if collision.gameObject.name=='Player' then
Destroy(self.gameObject)
print('luaItemPicked')
end
end)
--敌人发现自己的脚本
EnemyScript=CS.EnemyScript
xlua.private_accessible(EnemyScript)
xlua.hotfix(EnemyScript,'OnTriggerEnter',function(self, collider)
if collider.name=='Player' then
print("lua敌人发现了自己")
end
end)
xlua.hotfix(EnemyScript,'OnTriggerExit',function(self,collider)
if collider.name=='Player' then
print("lua我已经走出敌人范围之外了")
end
end)
gameDispose.lua脚本如下
PlayerMove=CS.PlayerMove
xlua.hotfix(PlayerMove,'Update',nil)
xlua.hotfix(PlayerMove,'Start',nil)
xlua.hotfix(PlayerMove,'FixedUpdate',nil)
CameraMove=CS.CameraMove
xlua.hotfix(CameraMove,'Start',nil)
xlua.hotfix(CameraMove,'LateUpdate',nil)
ItemScript=CS.ItemScript
xlua.hotfix(ItemScript,'OnCollisionEnter',nil)
EnemyScript=CS.EnemyScript
xlua.hotfix(EnemyScript,'OnTriggerEnter',nil)
xlua.hotfix(EnemyScript,'OnTriggerExit',nil)
然后上边对应的PlayerMove,CameraMove,ItemScript分别对应继承mono的脚本
实际上lua并不是很难,最主要用好table,metatable之间的区别 .和:之间的区别