Unity ToLua框架整理(一) C#和lua函数调用

记录下 方便自己用,代码已加注释,就不多说了,应该能看懂

1.C#调用Lua代码

Lua代码
lua代码放在tolua自带的lua目录,建议下个LuaBuilder配合VS2017使用
在这里插入图片描述

local GameObject = UnityEngine.GameObject	
local Input = UnityEngine.Input
local Vector3=UnityEngine.Vector3
local Transform=UnityEngine.Transform
local obj
--输出测试
function LDebug(args)
    print("LTest Coming!!!!")
end
--lua函数传参测试
function LInputValue( param)
    print('[lua中调用:]InputValue方法传入参数:'..tostring( param))
end
--unityAPI测试
function LFindCube(args)
    obj=GameObject.Find("Sphere")
	print(obj.name)
end
--添加标签设置
function LAddRig(args)
	obj:AddComponent(typeof(Rigidbody))
end
--物品移动测试
function LCubeMove(args)
    obj.transform:Translate(Vector3(2,2,2))
end
--控制物体测试
function LSphereCtrl()
	local h = Input.GetAxis("Horizontal")
	local v = Input.GetAxis("Vertical")
	obj.transform:Translate(Vector3(h,0,v))
end
--控制物体传参测试
function LObjCtrl(go)
	local hor = Input.GetAxis("Horizontal")
	local ver = Input.GetAxis("Vertical")
	go.transform:Translate(Vector3(hor,0,ver))
end


C#代码
这个随便放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;
public class TestToLua : MonoBehaviour
{
    public GameObject cube;
    private LuaState lua = null;
    //创建一个存放Lua类里面函数的载体
    private LuaFunction luaFunc = null;		
    private void Awake()
    {
        lua = new LuaState();
        lua.Start();
        //用来把Wrap.lua里的类和相应的Wrap类
        LuaBinder.Bind(lua);
        //引用读取lua文件
        lua.DoFile("LTest.lua");
    }
    void Start()
    {
        //输出测试
        CallFunc("LDebug");	
        //函数传参测试
        UseParameter();
        //找到物体
        CallFunc("LFindCube");
        //物体移动
        CallFunc("LCubeMove");
    }

    private void Update()
    {
        //虚拟轴控制移动
        SphereMoveCtrl();
        //虚拟轴控制移动 传参
        ObjCtrlParameter("LObjCtrl",cube);
    }
  
    void CallFunc(string func)
    {
        //此处的func因该为lua脚本中的table类的名字,而不是lua本文件名的名字
        luaFunc = lua.GetFunction(func);
        //开始调用Lua脚本里面的函数
        luaFunc.Call();
    }
    /// <summary>
    /// 移动不传参
    /// </summary>
    void SphereMoveCtrl()
    {
        LuaFunction valueFunc = lua.GetFunction("LSphereCtrl");
        valueFunc.Call();
    }
    /// <summary>
    /// 移动传参
    /// </summary>
    /// <param name="str"></param>
    /// <param name="obj"></param>
    void ObjCtrlParameter(string str,GameObject obj)
    {
        LuaFunction valueFunc = lua.GetFunction(str);
        valueFunc.Call(obj);
    }
    /// <summary>
    /// 传参
    /// </summary>
    void UseParameter()
    {
        LuaFunction valueFunc = lua.GetFunction("LInputValue");
        valueFunc.BeginPCall();
        valueFunc.Push("--push方法从C#中传入参数--");
        valueFunc.PCall();
        valueFunc.EndPCall();
        valueFunc.Call("--直接Call方法从C#传入参数--");
    }
}


2.Lua调用C#方法、参数

C#代码
类记得在CustomSetting里注册,否则报错不能用
在这里插入图片描述

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

public class UseLua : MonoBehaviour
{
    public GameObject cube;
    public GameObject sphere;
    public void CDebugger()
    {
        Debug.Log("C#  coming!!!!");
    }
    public void CFindObj()
    {
        cube = GameObject.Find("Cube");
        sphere = GameObject.Find("Sphere");
    }
    public void CSphereMove()
    {
        sphere.transform.Translate(new Vector3(2, 2, 2));
    }
    public void LSphereCtrl()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        sphere.transform.Translate(new Vector3(h,0,v));
        Debug.Log("LSphereCtrl comming!!!");
    }
    public void CObjCtrl(GameObject tmpobj)
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        tmpobj.transform.Translate(new Vector3(h,0,v));
    }
}

Lua代码

--构造自己的类
LLuaUseC={}
LLuaUseC.__index=LLuaUseC
local this=LLuaUseC
function  LLuaUseC.New()
     local  self = {}
      setmetatable(self , {__index= LLuaUseC})
      return  self
end 
--更新方法
function LLuaUseC.Update(args)
	print("Lua Update!!!!")
	--Lua Update 更新不传参测试
	this.useLua:LSphereCtrl()
	--Lua Update更新传参测试
	this.useLua:CObjCtrl(this.useLua.cube)
end
function LLuaUseC:UseC(args)
	--首先找到一个物体
	this.tmpObj=GameObject.Find("Cube")
	--添加要调用C#方法的脚本
	this.useLua=this.tmpObj:AddComponent(typeof(UseLua))
	--输出测试
	this.useLua:CDebugger()
	--找到物体测试
	this.useLua:CFindObj()
	--物体动一下
	this.useLua:CSphereMove()
	--Lua自己的Update方法  
	--unscaled false 采用deltaTime计时,true 采用 unscaledDeltaTime计时
	--function Timer.New(func, duration, loop, unscaled)
	--  传的是要更新的方法  -1是无限循环    具体自己看ToLua的脚本
	local timer=FrameTimer.New(this.Update,-1,-1)
	--开始Update
	timer:Start()
end
--启动
LLuaUseC:UseC()

lua调C# C#调lua都能实现下面这个效果,纯lua写,简单得多,就不写了
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值