一、调用Lua基本类型

  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 CSCallLuaBasicType : MonoBehaviour {
  11. LuaEnv luaEnv = new LuaEnv();
  12. void Start () {
  13. luaEnv.DoString("require 'BasicLua'");
  14. int a = luaEnv.Global.Get<int>("a");
  15. //int a;
  16. //luaEnv.Global.Get("a", out a);
  17. float b = luaEnv.Global.Get<float>("b");
  18. string c = luaEnv.Global.Get<string>("c");
  19. bool d = luaEnv.Global.Get<bool>("d");
  20. string n = luaEnv.Global.GetInPath<string>("e.f.name");
  21. Debug.Log(string.Format("a :{0}, b :{1}, c :{2}, d :{3}, name :{4}", a, b, c, d, n));
  22. }
  23. void Update () {
  24. if(luaEnv != null)
  25. {
  26. luaEnv.Tick();
  27. }
  28. }
  29. void OnDestroy()
  30. {
  31. luaEnv.Dispose();
  32. }
  33. }
  34. }

BasicLua.lua.txt

  1. a = 1
  2. b = 1.5
  3. c = 'hello world'
  4. d = true
  5. e = {
  6. ["f"] = { ["name"] = "shenjun" },
  7. "unity"
  8. }
  9. --f = { 2, 3 }

?