C#和Lua的交互

1.C#调用Lua

        1.1C#调用Lua文件中的全局变量

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

/*
*创建者:
*创建时间:
*描述:XLua管理器
*版本:
*/
public class XLuaManager 
{
    public static LuaEnv le;//Lua环境变量(官方建议全局)
    private XLuaManager(){ }
    private static XLuaManager instance;
    public static XLuaManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new XLuaManager();
                Init();//调用XLua管理器时进行初始化
            }
            return instance;
        }
    }
    static void Init()//Lua环境初始化+文件加载器
    {
        le=new LuaEnv();
        le.AddLoader(FileLoad);//添加文件加载器
    }
    static byte[] FileLoad(ref string fileName)
    {
        string str = Application.dataPath + "/Lua/" + fileName + ".lua";
        return File.ReadAllBytes(str);
    }
    public void Do(string fileName)//请求文件
    {
        le.DoString(fileName);
    }
    public void Dispose()//释放资源
    {
        le.Dispose();
    }
}

 //Lua文件         //C#调用Lua脚本

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

/*
*创建者:
*创建时间:
*描述:C#调用Lua文件中的全局变量
*版本:
*/
public class NewBehaviourScript : MonoBehaviour
{
    
    // Start is called before the first frame update
    void Start()
    {
        XLuaManager.Instance.DoString("require('Test')");//调用时初始化并打开文件
        int a = XLuaManager.le.Global.Get<int>("a");//通过泛型指定接收的变量类型
        print(a);
    }

}

         1.2C#访问Lua文件中的函数

         //Lua文件        

          //C#调用Lua脚本

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

/*
*创建者:
*创建时间:
*描述:C#调用Lua文件中的方法
*版本:
*/
public class NewBehaviourScript : MonoBehaviour
{
    public delegate void Func();//声明与Lua文件中具有相同返回值类型和参数列表的委托
    // Start is called before the first frame update
    void Start()
    {
        XLuaManager.Instance.DoString("require('Test')");
        Func Eat = XLuaManager.le.Global.Get<Func>("Eat");将Lua文件中的方法注册进委托再调用
        Eat();
        
        //适用于多返回值的方法
        LuaFunction lf = le.Global.Get<LuaFunction>("Sum");
        object[] objs = lf.Call(1,2);
        foreach (var item in objs)
        {
            print(item);
        }
    }

}

         1.3C#访问Lua文件中的表

                映射到接口

                 //Lua文件   

        

                   //C#调用Lua脚本

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

/*
*创建者:
*创建时间:
*描述:C#访问Lua文件中的表
*版本:
*/

[CSharpCallLua]
public class NewBehaviourScript : MonoBehaviour
{
    
    // Start is called before the first frame update
    void Start()
    {
        XLuaManager.Instance.DoString("require('Test')");
        Example example = XLuaManager.le.Global.Get<Example>("Test1");
        print(example.name);
    }
}
[CSharpCallLua]
public interface Example//映射到接口(创建与Lua文件相同键名的属性和方法)
{
    [CSharpCallLua]
    public delegate void Func();
    [CSharpCallLua]
    public int id { get; set; }
    [CSharpCallLua]
    public string name { get; set; }
    [CSharpCallLua]
    public Func Example1 { get; set; }
}

                 1.4遍历Lua文件中表的所有数据

                                1.4.1 创建顶级对象集合接收Lua文件中表的所有数据

        

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

/*
*创建者:
*创建时间:
*描述:C#访问Lua文件中的表
*版本:
*/

[CSharpCallLua]
public class NewBehaviourScript : MonoBehaviour
{
    
    // Start is called before the first frame update
    void Start()
    {
        XLuaManager.Instance.DoString("require('Test')");
        Example example = XLuaManager.le.Global.Get<Example>("Test1");
         //遍历Lua文件中类(表)的所有数据
        List<object> list = le.Global.Get<List<object>>("Test1");
        foreach (var item in list)
        {
            print(item);
        }
    }
}
[CSharpCallLua]
public interface Example//映射到接口(创建与Lua文件相同键名的属性和方法)
{
    [CSharpCallLua]
    public delegate void Func();
    [CSharpCallLua]
    public int id { get; set; }
    [CSharpCallLua]
    public string name { get; set; }
    [CSharpCallLua]
    public Func Example1 { get; set; }
}

                 1.4.2 创建字典接收Lua文件中表的所有数据

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

/*
*创建者:
*创建时间:
*描述:C#访问Lua文件中的表
*版本:
*/

[CSharpCallLua]
public class NewBehaviourScript : MonoBehaviour
{
    
    // Start is called before the first frame update
    void Start()
    {
        XLuaManager.Instance.DoString("require('Test')");
        Example example = XLuaManager.le.Global.Get<Example>("Test1");
         //遍历Lua文件中类(表)的所有数据
        Dictionary<object, object> dic = le.Global.Get<Dictionary<object, object>>("Test1");
        foreach (var item in dic)
        {
            print(item.Key+"\t"+item.Value);
        }
    }
}
[CSharpCallLua]
public interface Example//映射到接口(创建与Lua文件相同键名的属性和方法)
{
    [CSharpCallLua]
    public delegate void Func();
    [CSharpCallLua]
    public int id { get; set; }
    [CSharpCallLua]
    public string name { get; set; }
    [CSharpCallLua]
    public Func Example1 { get; set; }
}

        1.5 C#遍历Lua文件中所有数据

              //Lua文件  

 

        //C#脚本调用Lua

        

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

/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public class HomeWork_3_31 : MonoBehaviour
{
    LuaEnv le;
    // Start is called before the first frame update
    void Start()
    {
        le = new LuaEnv();
        le.AddLoader(FileLoad);
        le.DoString("require('HomeWork_3_31TableLua')");
        HomeWork hw = le.Global.Get<HomeWork>("HomeWork");//Lua文件使用io.open调用其他lua
//文件,需要通过映射该lua文件间接调用其他lua文件
        hw.Init();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    byte[] FileLoad(ref string fileName)
    {
        string str = Application.dataPath + "/Lua/" + fileName + ".lua";
        return File.ReadAllBytes(str);
    }
    private void OnDestroy()
    {
        //le.Dispose();
    }
}
[CSharpCallLua]
public interface HomeWork //映射到接口
{
    [CSharpCallLua]
    public delegate void func1();
    [CSharpCallLua]
    public delegate void func2(string s);

    [CSharpCallLua]
    public func1 Init();
    [CSharpCallLua]
    public func1 Register();
    [CSharpCallLua]
    public func1 Login();
    [CSharpCallLua]
    public func2 Split(string s);

}

2.Lua调用C#

        2.1 工程配置:Edit=>ProjectSettings=>Player=>OtherSettings=>ScriptCompliation

        2.2Lua调用C#

 //Lua文件(对该方法进行覆写)

 //C#调用Lua文件,执行热更新

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

/*
*创建者:
*创建时间:
*描述:Lua调用C#
*版本:
*/

[Hotfix]
public class NewBehaviourScript : MonoBehaviour
{   
    // Start is called before the first frame update
    void Start()
    {
        XLuaManager.Instance.DoString("require('Test')");  //执行Lua文件
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值