柚子快報激活碼778899分享:Lua調(diào)用C#類
柚子快報激活碼778899分享:Lua調(diào)用C#類
先創(chuàng)建一個Main腳本作為主入口,掛載到攝像機上
public class Main : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
LuaMgr.GetInstance().Init();
LuaMgr.GetInstance().DoLuaFile("Main");
}
// Update is called once per frame
void Update()
{
}
}
編寫Lua腳本
Main.lua
print("OK")
--也會執(zhí)行重定向
--require("Test")
require("L1_CallClass")
L1_CallClass.lua
--Lua中使用C#類
--CS.命名空間.類名
--Unity的類(GameObject Transform等
--在CS.UnityEngine
--默認調(diào)用無參構(gòu)造
local obj1 = CS.UnityEngine.GameObject()
local obj2 = CS.UnityEngine.GameObject("命名")
--節(jié)約性能
GameObject = CS.UnityEngine.GameObject
local obj3 = GameObject("節(jié)約性能")
--靜態(tài)方法直接.使用
local obj4 = GameObject.Find("命名")
print(obj4.transform.position)
--使用成員方法,一定要加:
Vector3 = CS.UnityEngine.Vector3
obj4.transform:Translate(Vector3.right)
print(obj4.transform.position)
--自定義類
local t = CS.Test1()
t:Speak("說話")
local t2 = CS.Holens.Test2()
t2:Speak("說話")
--繼承Mono的類 不能直接New
local obj5 = GameObject("加腳本測試")
--xLua提供了一個重要方法 typeof方法
obj5:AddComponent(typeof(CS.LuaCallC))
自定義類
//自定義類
public class Test1
{
public void Speak(string str)
{
Debug.Log("Test1"+str);
}
}
namespace Holens
{
public class Test2
{
public void Speak(string str)
{
Debug.Log("Test2" + str);
}
}
}
public class LuaCallC : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
?
運行結(jié)果
柚子快報激活碼778899分享:Lua調(diào)用C#類
好文鏈接
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。