Unity XLua(二)C#访问Lua脚本中的值+表

20 篇文章 4 订阅

在C#脚本中访问Lua脚本中的值

 Lua脚本

a = 24
b = 'ok'
c = true

C#访问

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

public class Test : MonoBehaviour
{
    LuaEnv lua;
	void Start ()
    {
        lua = new LuaEnv();

        //加载Lua
        lua.AddLoader(LoadLua);
        lua.DoString("require 'TestLua'");

        //访问Lua脚本中的值
        int a = lua.Global.Get<int>("a");
        string b = lua.Global.Get<string>("b");
        bool c = lua.Global.Get<bool>("c");

        Debug.Log(a);
        Debug.Log(b);
        Debug.Log(c);
    }

    byte[] LoadLua(ref string file)
    {
        Debug.Log(file);

        string slua = File.ReadAllText(Application.dataPath + "/Lua/" + file + ".lua");
        byte[] bytes = new UTF8Encoding().GetBytes(slua);

        return bytes;
    }

    void OnDestroy()
    {
        lua.Dispose();
    }
}

在C#脚本中访问Lua脚本中的table

 Lua脚本

person = {name = '张三',age = 25}

C#访问

方式一:

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

public class Person
{
    public string name;
    public int age;
}

public class Test : MonoBehaviour
{
    LuaEnv lua;
	void Start ()
    {
        lua = new LuaEnv();

        //加载Lua
        lua.AddLoader(LoadLua);
        lua.DoString("require 'TestLua'");

        //访问Lua脚本中的table
        Person person = lua.Global.Get<Person>("person");
        Debug.Log(person.name);
        Debug.Log(person.age);
        person.name = "王五";
        person.age = 26;
        lua.DoString("print(person.name)");
        lua.DoString("print(person.age)");
    }

    byte[] LoadLua(ref string file)
    {
        Debug.Log(file);

        string slua = File.ReadAllText(Application.dataPath + "/Lua/" + file + ".lua");
        byte[] bytes = new UTF8Encoding().GetBytes(slua);

        return bytes;
    }

    void OnDestroy()
    {
        lua.Dispose();
    }
}

此访问为值类型访问,只允许访问,不允许赋值。

方式二:

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

[CSharpCallLua]
interface IPerson
{
    string name { get; set; }
    int age { get; set; }
}

public class Test : MonoBehaviour
{
    LuaEnv lua;
	void Start ()
    {
        lua = new LuaEnv();

        //加载Lua
        lua.AddLoader(LoadLua);
        lua.DoString("require 'TestLua'");

        //访问Lua脚本中的table
        IPerson iperson = lua.Global.Get<IPerson>("person");
        Debug.Log(iperson.name);
        Debug.Log(iperson.age);
        iperson.name = "王五";
        iperson.age = 26;
        lua.DoString("print(person.name)");
        lua.DoString("print(person.age)");
    }

    byte[] LoadLua(ref string file)
    {
        Debug.Log(file);

        string slua = File.ReadAllText(Application.dataPath + "/Lua/" + file + ".lua");
        byte[] bytes = new UTF8Encoding().GetBytes(slua);

        return bytes;
    }

    void OnDestroy()
    {
        lua.Dispose();
    }
}

此访问为引用类型访问,允许对Lua脚本中table的值进行修改。

方式三:

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

public class Test : MonoBehaviour
{
    LuaEnv lua;
    void Start()
    {
        lua = new LuaEnv();

        //加载Lua
        lua.AddLoader(LoadLua);
        lua.DoString("require 'TestLua'");

        //访问Lua脚本中的table
        Dictionary<string, object> dic = lua.Global.Get<Dictionary<string, object>>("person");
        Debug.Log(dic["name"]);
        Debug.Log(dic["age"]);
        dic["name"] = "王五";
        dic["age"] = 26;
        lua.DoString("print(person.name)");
        lua.DoString("print(person.age)");
    }

    byte[] LoadLua(ref string file)
    {
        Debug.Log(file);

        string slua = File.ReadAllText(Application.dataPath + "/Lua/" + file + ".lua");
        byte[] bytes = new UTF8Encoding().GetBytes(slua);

        return bytes;
    }

    void OnDestroy()
    {
        lua.Dispose();
    }
}

方式四:

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

public class Test : MonoBehaviour
{
    LuaEnv lua;
    void Start()
    {
        lua = new LuaEnv();

        //加载Lua
        lua.AddLoader(LoadLua);
        lua.DoString("require 'TestLua'");

        //访问Lua脚本中的table
        LuaTable luaTable = lua.Global.Get<LuaTable>("person");
        Debug.Log(luaTable.Get<string>("name"));
        Debug.Log(luaTable.Get<int>("age"));
        luaTable.Set<string, string>("name", "王五");
        luaTable.Set<string, int>("age", 26);
        lua.DoString("print(person.name)");
        lua.DoString("print(person.age)");
    }

    byte[] LoadLua(ref string file)
    {
        Debug.Log(file);

        string slua = File.ReadAllText(Application.dataPath + "/Lua/" + file + ".lua");
        byte[] bytes = new UTF8Encoding().GetBytes(slua);

        return bytes;
    }

    void OnDestroy()
    {
        lua.Dispose();
    }
}

补充:表中函数元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值