调用Lua Table类型

TableLua.lua.txt

  1. student = {
  2. name = "xm", age = 18, Sex = "man",
  3. 80, 90, 95,
  4. getSex = function(self)
  5. return "人妖"
  6. end,
  7. totalScore = function(self, a, b)
  8. return a + b;
  9. end
  10. }

一、映射到class和struct

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using XLua;
  8. namespace shenjun
  9. {
  10. public class TableToClass : MonoBehaviour {
  11. LuaEnv luaEnv = new LuaEnv();
  12. void Start () {
  13. luaEnv.DoString("require 'TableLua'");
  14. // 对应public字段,table的属性可以多于或少于class的字段,没有对应的使用该类型的默认值
  15. Student xm = luaEnv.Global.Get<Student>("student");
  16. Debug.Log(xm);
  17. }
  18. void Update () {
  19. if(luaEnv != null)
  20. {
  21. luaEnv.Tick();
  22. }
  23. }
  24. private void OnDestroy()
  25. {
  26. luaEnv.Dispose();
  27. }
  28. class Student
  29. {
  30. public string name;
  31. public int age;
  32. public string Sex { get; set; } // 无法对应table中的键
  33. public override string ToString()
  34. {
  35. return string.Format("name : {0}, age : {1}, sex : {2}", name, age, Sex);
  36. }
  37. //public string get_Sex()
  38. //{
  39. // return "";
  40. //}
  41. }
  42. }
  43. }
  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using XLua;
  8. namespace shenjun
  9. {
  10. public class TableToStruct : MonoBehaviour
  11. {
  12. LuaEnv luaEnv = new LuaEnv();
  13. void Start()
  14. {
  15. luaEnv.DoString("require 'TableLua'");
  16. // 对应public字段,table的属性可以多于或少于class的字段,没有对应的使用该类型的默认值
  17. Student xm = luaEnv.Global.Get<Student>("student");
  18. Debug.Log(xm);
  19. }
  20. void Update()
  21. {
  22. if (luaEnv != null)
  23. {
  24. luaEnv.Tick();
  25. }
  26. }
  27. private void OnDestroy()
  28. {
  29. luaEnv.Dispose();
  30. }
  31. /*
  32. * xLua复杂值类型(struct)的默认传递方式是引用传递,这种方式要求先对值类型boxing,传递给lua,
  33. * lua使用后释放该引用。由于值类型每次boxing将产生一个新对象,当lua侧使用完毕释放该对象的引用时,
  34. * 则产生一次gc。为此,xLua实现了一套struct的gc优化方案,您只要通过简单的配置,
  35. * 则可以实现满足条件的struct传递到lua侧无gc。
  36. *
  37. * struct需要满足什么条件?
  38. * 1、struct允许嵌套其它struct,但它以及它嵌套的struct只能包含这几种基本类型:
  39. * byte、sbyte、short、ushort、int、uint、long、ulong、float、double;
  40. * 例如UnityEngine定义的大多数值类型:Vector系列,Quaternion,Color。。。均满足条件,
  41. * 或者用户自定义的一些struct
  42. * 2、该struct配置了GCOptimize属性(对于常用的UnityEngine的几个struct,
  43. * Vector系列,Quaternion,Color。。。均已经配置了该属性),这个属性可以通过配置文件或者C# Attribute实现;
  44. * 3、使用到该struct的地方,需要添加到生成代码列表;
  45. */
  46. [GCOptimize]
  47. [LuaCallCSharp]
  48. struct Student
  49. {
  50. public string name;
  51. public int age;
  52. public string Sex { get; set; } // 无法对应table中的键
  53. public override string ToString()
  54. {
  55. return string.Format("name : {0}, age : {1}, sex : {2}", name, age, Sex);
  56. }
  57. }
  58. //[LuaCallCSharp]
  59. //public static class StructConfig
  60. //{
  61. // public static List<System.Type> LuaCallCSharp
  62. // {
  63. // get
  64. // {
  65. // return new List<System.Type>()
  66. // {
  67. // typeof(Student)
  68. // };
  69. // }
  70. // }
  71. //}
  72. }
  73. }

二、映射到Interface

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using XLua;
  8. namespace shenjun
  9. {
  10. public class TableToInterface : MonoBehaviour {
  11. LuaEnv luaEnv = new LuaEnv();
  12. void Start () {
  13. luaEnv.DoString("require 'TableLua'");
  14. // 引用类型映射 代码生成器会生成一个实例
  15. IStudent iTable = luaEnv.Global.Get<IStudent>("student");
  16. // 修改table中某键的值
  17. luaEnv.Global.SetInPath<int>("student.age", 20);
  18. string _name = iTable.name;
  19. int age = iTable.age;
  20. string sex = iTable.Sex;
  21. Debug.Log(string.Format("name :{0}, age :{1}, sex :{2}", _name, age, sex));
  22. // 引用类型映射
  23. iTable.name = "xz";
  24. Debug.Log(luaEnv.Global.GetInPath<string>("student.name"));
  25. int result = iTable.totalScore(100, 200);
  26. Debug.Log(result);
  27. }
  28. void Update () {
  29. if(luaEnv != null)
  30. {
  31. luaEnv.Tick();
  32. }
  33. }
  34. private void OnDestroy()
  35. {
  36. luaEnv.Dispose();
  37. }
  38. [CSharpCallLua]
  39. interface IStudent
  40. {
  41. string name { get; set; }
  42. int age { get; set; }
  43. string Sex { get; set; }
  44. //string get_Sex();
  45. int totalScore(int a, int b);
  46. }
  47. }
  48. }

三、映射到Dictionary和List

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using XLua;
  8. namespace shenjun
  9. {
  10. public class TableToDicOrList : MonoBehaviour {
  11. LuaEnv luaEnv = new LuaEnv();
  12. void Start () {
  13. luaEnv.DoString("require 'TableLua'");
  14. // 值拷贝 只能获取匹配的
  15. Dictionary<string, string> dic = new Dictionary<string, string>();
  16. dic = luaEnv.Global.Get<Dictionary<string, string>>("student");
  17. foreach (var item in dic.Keys)
  18. {
  19. Debug.Log("key :" + item + ", value :" + dic[item]);
  20. }
  21. List<int> list = new List<int>();
  22. list = luaEnv.Global.Get<List<int>>("student");
  23. foreach (var item in list)
  24. {
  25. Debug.Log(item);
  26. }
  27. }
  28. void Update () {
  29. }
  30. private void OnDestroy()
  31. {
  32. luaEnv.Dispose();
  33. }
  34. }
  35. }

四、映射到LuaTable类型

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityEngine;
  8. using XLua;
  9. namespace shenjun
  10. {
  11. public class TableToLuaTable : MonoBehaviour {
  12. LuaEnv luaEnv = new LuaEnv();
  13. void Start () {
  14. luaEnv.DoString("require 'TableLua'");
  15. LuaTable table = luaEnv.Global.Get<LuaTable>("student");
  16. string _name = table.Get<string>("name");
  17. int age = table.Get<int>("age");
  18. string sex = table.Get<string>("Sex");
  19. Debug.Log(string.Format("name :{0}, age :{1}, sex :{2}", _name, age, sex));
  20. Debug.Log("Length:" + table.Length); // 3
  21. List<int> scores = luaEnv.Global.Get<List<int>>("student");
  22. Debug.Log("Total Scores :" + scores.Aggregate((a, b) => a + b));
  23. var list = table.GetKeys();
  24. foreach (var item in list)
  25. {
  26. Debug.Log(item);
  27. }
  28. int csharpScore = table.Get<int, int>(1);
  29. int unityScore = table.Get<int, int>(2);
  30. int shaderScore = table.Get<int, int>(3);
  31. Debug.Log("csharpScore : " + csharpScore + ", unityScore : " + unityScore + ", shaderScore : " + shaderScore);
  32. LuaFunction func = table.Get<LuaFunction>("totalScore");
  33. object[] results = func.Call(table, 100, 200);
  34. Debug.Log(results[0]);
  35. }
  36. void Update () {
  37. }
  38. private void OnDestroy()
  39. {
  40. luaEnv.Dispose();
  41. }
  42. }
  43. }
  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityEngine;
  8. using XLua;
  9. namespace shenjun
  10. {
  11. public class TableToLuaTable2 : MonoBehaviour {
  12. [CSharpCallLua]
  13. public delegate int AddDel(LuaTable self, int a, int b);
  14. [CSharpCallLua]
  15. public delegate string GetSexDel();
  16. public TextAsset luaText;
  17. LuaEnv luaEnv = new LuaEnv();
  18. LuaTable luaTableGlobal;
  19. LuaTable luaTableLocal;
  20. GetSexDel luaGetSex;
  21. AddDel luaAdd;
  22. void Start () {
  23. #region 全局table元表
  24. luaTableGlobal = luaEnv.NewTable();
  25. // 设置检索元表 为全局
  26. LuaTable meta = luaEnv.NewTable();
  27. meta.Set("__index", luaEnv.Global);
  28. luaTableGlobal.SetMetaTable(meta);
  29. meta.Dispose();
  30. luaEnv.DoString(luaText.text, "LuaBehaviour", luaTableGlobal);
  31. string _name = luaTableGlobal.GetInPath<string>("student.name");
  32. int age = luaTableGlobal.GetInPath<int>("student.age");
  33. string sex = luaTableGlobal.GetInPath<string>("student.Sex");
  34. Debug.Log(string.Format("1 name :{0}, age :{1}, sex :{2}", _name, age, sex));
  35. luaGetSex = luaTableGlobal.GetInPath<GetSexDel>("student.getSex");
  36. if (luaGetSex != null)
  37. Debug.Log("1 : " + luaGetSex()) ;
  38. luaAdd = luaTableGlobal.GetInPath<AddDel>("student.totalScore");
  39. if (luaAdd != null)
  40. Debug.Log("1 : " + luaAdd(luaEnv.NewTable() ,100, 200));
  41. #endregion
  42. #region 局部table元表
  43. luaTableLocal = luaEnv.NewTable();
  44. luaEnv.DoString(luaText.text);
  45. LuaTable metaContent = luaEnv.Global.Get<LuaTable>("student");
  46. meta = luaEnv.NewTable();
  47. meta.Set("__index", metaContent);
  48. luaTableLocal.SetMetaTable(meta);
  49. meta.Dispose();
  50. _name = luaTableLocal.Get<string>("name");
  51. age = luaTableLocal.Get<int>("age");
  52. sex = luaTableLocal.Get<string>("Sex");
  53. Debug.Log(string.Format("2 name :{0}, age :{1}, sex :{2}", _name, age, sex));
  54. luaGetSex = luaTableLocal.Get<GetSexDel>("getSex");
  55. if(luaGetSex != null)
  56. {
  57. Debug.Log("2 : " + luaGetSex());
  58. }
  59. luaAdd = luaTableLocal.Get<AddDel>("totalScore");
  60. if (luaAdd != null)
  61. Debug.Log("2 : " + luaAdd(luaEnv.NewTable(), 500, 600));
  62. #endregion
  63. }
  64. void Update () {
  65. }
  66. private void OnDestroy()
  67. {
  68. luaGetSex = null;
  69. luaAdd = null;
  70. luaEnv.Dispose();
  71. }
  72. }
  73. }

?