Lua中new C#对象
-
1.C#脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
public class _005_LuaCallCSharp : MonoBehaviour {
private LuaEnv env;
void Start ()
{
env = new LuaEnv();
env.DoString("require 'LuaCallCSharp'");
}
private void Update()
{
if(env!=null)
{
env.Tick();
}
}
private void OnDestroy()
{
env.Dispose();
}
}
-
2、Lua脚本
--new C#对象
local newGameObj = CS.UnityEngine.GameObject()
--创建名称为helloworld的物体
local newGameObj2 = CS.UnityEngine.GameObject('helloworld')
--创建一个Sphere(球体)
local newSphere=CS.UnityEngine.GameObject.CreatePrimitive(0)
print(newGameObj)
print(newGameObj2)
print(newSphere)
输出结果:
img.jpg
注意:
1、在XLua中调用所有C#相关的都放到CS下,包括构造函数,静态成员属性、方法。
2、Lua里头没有new关键字
如果有多个构造函数呢?xlua支持重载,比如你要调用GameObject的带一个string参数的构造函数,这么写:
local newGameObj2 = CS.UnityEngine.GameObject('helloworld')
3、相关的方法名与类名,空间名不能出现错误。