lua语言(四)之Unity3D实践

3 篇文章 0 订阅
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 ();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值