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

在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("a");
        string b = lua.Global.Get("b");
        bool c = lua.Global.Get("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();
    }
}

Unity XLua(二)C#访问Lua脚本中的值+表_第1张图片

在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");
        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();
    }
}

Unity XLua(二)C#访问Lua脚本中的值+表_第2张图片

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

方式二:

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("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();
    }
}

Unity XLua(二)C#访问Lua脚本中的值+表_第3张图片

此访问为引用类型访问,允许对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 dic = lua.Global.Get>("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();
    }
}

Unity XLua(二)C#访问Lua脚本中的值+表_第4张图片

方式四:

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("person");
        Debug.Log(luaTable.Get("name"));
        Debug.Log(luaTable.Get("age"));
        luaTable.Set("name", "王五");
        luaTable.Set("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();
    }
}

Unity XLua(二)C#访问Lua脚本中的值+表_第5张图片

补充:表中函数元素

你可能感兴趣的:(Unity,Lua)