复杂转换

ComplexConvert.lua.txt

  1. local studentInfo = {
  2. name = "zs",
  3. age = 18,
  4. scores = {
  5. 90, 80, 100
  6. }
  7. }
  8. local students = {}
  9. students[1] = studentInfo;
  10. students[1].name = "xm"
  11. students[2] = { name = "ls", age = 20 }
  12. students[2].scores = { 90, 50, 100 }
  13. -- 创建Unity1711对象
  14. local unity1711 = CS.shenjun.Unity1711()
  15. unity1711:Add(studentInfo)
  16. unity1711:Add(students[1])
  17. unity1711:Add(students[2])
  18. unity1711:ShowAll()
  19. -- 获取所有成绩都及格的学生数据
  20. print("show pass ======================")
  21. local pass = unity1711:GetPass()
  22. for i=1, pass.Length do
  23. CS.UnityEngine.Debug.Log(pass[i-1])
  24. end
  25. -- 获取所有某成绩不及格的学生数据
  26. print("show fail ======================")
  27. local fail = unity1711:GetFail()
  28. for i=1, fail.Length do
  29. CS.UnityEngine.Debug.Log(fail[i-1])
  30. end

ComplexConvert.cs

  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 ComplexConvert : MonoBehaviour
  12. {
  13. void Start()
  14. {
  15. LuaEnv luaEnv = new LuaEnv();
  16. luaEnv.DoString("require 'ComplexConvert'");
  17. luaEnv.Dispose();
  18. }
  19. void Update()
  20. {
  21. }
  22. }
  23. public class Unity1711
  24. {
  25. public List<StudentInfo> studentsInfo = new List<StudentInfo>();
  26. public void Add(StudentInfo s)
  27. {
  28. studentsInfo.Add(s);
  29. }
  30. public void ShowAll()
  31. {
  32. foreach (var item in studentsInfo)
  33. {
  34. Debug.Log(item);
  35. }
  36. }
  37. // 获取所有成绩都及格的学生数据
  38. public StudentInfo[] GetPass()
  39. {
  40. var result = studentsInfo.Where(
  41. n =>
  42. {
  43. return n.scores.All(m => m > 60);
  44. }
  45. );
  46. return result.ToArray();
  47. }
  48. // 获取所有某成绩不及格的学生数据
  49. public StudentInfo[] GetFail()
  50. {
  51. var result = studentsInfo.Where(
  52. n =>
  53. {
  54. return n.scores.Any(m => m < 60);
  55. }
  56. );
  57. return result.ToArray();
  58. }
  59. }
  60. [XLua.LuaCallCSharp]
  61. public struct StudentInfo
  62. {
  63. public string name;
  64. public int age;
  65. public int[] scores;
  66. public override string ToString()
  67. {
  68. //var score = from n in scores select n + "";
  69. var score = scores.Select(n => n + "");
  70. string scoreStr = string.Join("#", score.ToArray());
  71. return string.Format("[name:{0}, age:{1}, score:{2}]", name, age, scoreStr);
  72. }
  73. }
  74. }

?