zifa2003293 发表于 2021-8-13 11:24

XLUA与(Unity中)C#所有类型交互实例大全--1

在一开始,我们想来创建一个C#代码,作为与Lua交互使用,就命名为NewTest好了
using System.Collections;using System.Collections.Generic;using UnityEngine;using XLua;public class NewTest : MonoBehaviour {      void Start () {            }                  public void Update () {        }}打好hotfix和LuaCallCSharp的标签,方便一会儿使用
再创建一个Lua脚本,在C#中用自定义Loader加载创建好的Lua脚本(怎么加载很多教程都有,我这里就不说了,可以执行百度)
1.访问/创建C#中的数组
我们在C#中的NewTest中创建一个名为ArrayTest的方法,在Update中调用方便Debug,并在创建一个GameObject的数组在Start中初始化
using System.Collections;using System.Collections.Generic;using UnityEngine;using XLua;public class NewTest : MonoBehaviour {    public GameObject[] go1;    void Start () {       go1 = new GameObject { new GameObject("111"), new GameObject("222"), new GameObject("333") };    }                  public void Update () {      ArrayTest();    }      public void ArrayTest()//数组测试    {      print(go1.name);      //在Lua我们在创建一个对象数组TODO...    }}接着是Lua代码
local UnityEngine=CS.UnityEnginelocal System=CS.Systemxlua.hotfix(CS.NewTest,"ArrayTest",function(self)        print(self.go1.name)--访问C#中的GameObject数组        --自己创建一个GameObject数组        local c_GameObject={}        c_GameObject=UnityEngine.GameObject("新的");        print("我是在Lua中创建的",c_GameObject)end)就这么简单就完成了,在Lua中创建数组其实就是创建表,表几乎能装下C#中的任何类型。
2.访问/创建泛型
我们再到C#代码中增加如下需要使用的东西
using System.Collections;using System.Collections.Generic;using UnityEngine;using XLua;public class NewTest : MonoBehaviour{    public GameObject[] go1;    public List<int> bb;    void Start()    {      go1 = new GameObject { new GameObject("111"), new GameObject("222"), new GameObject("333") };      bb = new List<int>() { 1, 2 };    }        public void Update()    {      GenericTest();            }    public void ArrayTest()//数组测试    {      print(go1.name);      //在Lua我们在创建一个对象数组TODO...    }    public void GenericTest()//泛型测试    {      AA<int> aa = new AA<int>();                aa.ShowTpyeName();      print(bb+" "+ aa.GetType());      //我们在Lua中创建一个泛型    }}public class AA<T>{    public void ShowTpyeName()    {      Debug.Log(typeof(T).Name);    }}接着我们在添加Lua代码
xlua.hotfix(CS.NewTest,"GenericTest",function(self)        --创建AA的泛型类对象要先用GetType()获取泛型类的名称,放到下面的字符串中        --不过此方法笨重代价过大,也暂时找不到什么好方法了        local aa=CS.System.Activator.CreateInstance(CS.System.Type.GetType("AA`1"))        aa:ShowTpyeName();--调用泛型类中的方法        --访问泛型类        for i=1,self.bb.Count do--访问List                print(i)        endend)访问List比较容易,就像数组操作一样,当创建List就不那么容易了,毕竟Lua没有泛型,实现还需要反射代价昂贵,也比较笨重
3.可变参数函数的调用
这种比较简单,老规矩想看C#代码
using System.Collections;using System.Collections.Generic;using UnityEngine;using XLua;public class NewTest : MonoBehaviour{    public GameObject[] go1;    public List<int> bb;    void Start()    {      go1 = new GameObject { new GameObject("111"), new GameObject("222"), new GameObject("333") };      bb = new List<int>() { 1, 2 };    }        public void Update()    {      VariableParams("111", "222");            }    public void ArrayTest()//数组测试    {      print(go1.name);      //在Lua我们在创建一个对象数组TODO...    }    public void GenericTest()//泛型测试    {      AA<int> aa = new AA<int>();                aa.ShowTpyeName();      print(bb+" "+ aa.GetType());      //我们在Lua中创建一个泛型    }    public void VariableParams(params string[] aa)//不定参数测试    {      print(aa + " "+aa);    }}public class AA<T>{    public void ShowTpyeName()    {      Debug.Log(typeof(T).Name);    }}再来看Lua的
xlua.hotfix(CS.NewTest,"Update",function(self)        self:VariableParams("111","222")end)类型还有很多很多,今天先更这三种的,过两天继续更新,希望可以帮助到大家
页: [1]
查看完整版本: XLUA与(Unity中)C#所有类型交互实例大全--1