在Lua中访问C#(基于xLua)

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

public class Lua2CSharp : MonoBehaviour
{
    private void Start()
    {
        LuaEnv luaenv = new LuaEnv();
        luaenv.DoString("require 'Lua2C#'");
        luaenv.Dispose();
    }
}

下面是Lua脚本

CS.UnityEngine.GameObject("new by Lua")--创建游戏物体
print(CS.UnityEngine.Time.deltaTime)--输出deltaTime
CS.UnityEngine.Time.timeScale=0.5
local gO=CS.UnityEngine.GameObject--对于重复调用的变量可以先声明一个局部变量,节省性能
local camera = gO.Find("Main Camera")--查找摄像机
camera.name="zxw by Lua"--修改名称
local light = gO.Find("Directional Light")--查找灯光
light.name="zxws by Lua"--修改名称
local cC=camera:GetComponent("Camera")--调用成员方法(根据某个对象所调用)使用冒号语法糖来传递第一个对象
gO.Destroy(cC)--销毁

猜你喜欢

转载自blog.csdn.net/qq_43461641/article/details/85344884