在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();
}
}
补充:表中函数元素