作者:a58224227 | 来源:互联网 | 2023-08-29 10:55
创建一个CubeGameObjectUnityEngine.GameObjectInputUnityEngine.InputTimeUnityEngine.Time
//创建一个Cube
GameObject = UnityEngine.GameObject
Input = UnityEngine.Input
Time = UnityEngine.Time
Transform = UnityEngine.Transform
Rigidbody = UnityEngine.Rigidbody
cube = nil
transform = nil
rig = nil
function OnStart()
cube = GameObject.Instantiate(prefab)
rig = cube:AddComponent(typeof(Rigidbody))
rig.useGravity = false
transform = cube:GetComponent(typeof(Transform))
transform.position = Vector3(0,1,-5)
end
function OnUpdate()
x,y = 0,0
x = Input.GetAxis("Horizontal")
y = Input.GetAxis("Vertical")
transform:Rotate(Vector3(0,x,0)*Time.deltaTime*100)
transform.position = transform.position + transform.forward*y*Time.deltaTime*10
end
将脚本写好后拖到Unity中
在C#脚本中
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface ;
public class CreatCubeByLuaScr : MonoBehaviour {
public TextAsset luaTex;
LuaState luaState;
public GameObject cube;
//定义一个lua函数字段
LuaFunction StarFunc;
void Start () {
luaState = new LuaState ();
luaState.Start ();
LuaBinder.Bind (luaState);
//给lua文件中使用的预设体赋值
luaState ["prefab"] = cube;
luaState.DoString (luaTex.text);
//获取lua代码中的方法
StarFunc = luaState.GetFunction ("OnStart");
//调用Lua中的代码执行
StarFunc.Call ();
}
void Update () {
UpdateFunc.Call ();
}
}
挂上c#脚本:将lua文件拖到Lua Text中