1.在场景里面创建一个Camera游戏对象添加一个离子特效
创建.TXT文件拖入到工程里
GameObject = UnityEngine.GameObject
ParticleSystem = UnityEngine.ParticleSystem
local obj =
GameObject('obj',typeof(UnityEngine.Camera))
par = obj:AddComponent(typeof(ParticleSystem))
创建C#脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;//引用lua命名空间
public class ParseLuaScript : MonoBehaviour {
//
public TextAsset luaText;
//lua的运行环境
LuaState luaState;
void Start () {
luaState = new LuaState ();
luaState.Start ();
//让luaState能够是识别unity引擎中的3D游戏对象相关类
LuaBinder.Bind (luaState);
luaState.DoString (luaText.text);
}
void Update () {
}
}
创建一个空物体.添加脚本.运行,删除原来的主摄像机
2.创建一个Cube,添加刚体组件:
创建txt文件:
--获取想用的组件
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
创建脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;
public class CreatCubeByLuaScr : MonoBehaviour {
public TextAsset luaText;
LuaState lua;
public GameObject cube;
//定义一个lua函数的字段
LuaFunction StartFunc;
LuaFunction UptateFunc;
void Start () {
lua = new LuaState ();
lua.Start ();
LuaBinder.Bind (lua);
//给lua文件中使用的预设体赋值
lua ["prefab"] = cube;
lua.DoString (luaText.text);
//获取Lua代码中的OnStart方法
StartFunc = lua.GetFunction ("OnStart");
//调用Lua中的代码执行
StartFunc.Call ();
}
void Update () {
}
}
新建空物体:
创建cube预设体
执行:
3.让Cube动起来
修改上面的Txt文件:
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
修改C#脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;
public class CreatCubeByLuaScr : MonoBehaviour {
public TextAsset luaText;
LuaState lua;
public GameObject cube;
//定义一个lua函数的字段
LuaFunction StartFunc;
LuaFunction UptateFunc;
void Start () {
lua = new LuaState ();
lua.Start ();
LuaBinder.Bind (lua);
//给lua文件中使用的预设体赋值
lua ["prefab"] = cube;
lua.DoString (luaText.text);
//获取Lua代码中的OnStart方法
StartFunc = lua.GetFunction ("OnStart");
//获取Lua代码中的OnUptate方法
UptateFunc = lua.GetFunction("OnUpdate");
//调用Lua中的代码执行
StartFunc.Call ();
}
void Update () {
UptateFunc.Call ();
}
}