TestCJson.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. using LuaInterface;
  4. public class TestCJson : LuaClient
  5. {
  6. string script = @"
  7. local json = require 'cjson'
  8. function Test(str)
  9. local data = json.decode(str)
  10. print(data.glossary.title)
  11. s = json.encode(data)
  12. print(s)
  13. end
  14. ";
  15. protected override LuaFileUtils InitLoader()
  16. {
  17. return new LuaResLoader();
  18. }
  19. protected override void OpenLibs()
  20. {
  21. base.OpenLibs();
  22. OpenCJson();
  23. }
  24. protected override void OnLoadFinished()
  25. {
  26. base.OnLoadFinished();
  27. TextAsset text = (TextAsset)Resources.Load("jsonexample", typeof(TextAsset));
  28. string str = text.ToString();
  29. luaState.DoString(script);
  30. LuaFunction func = luaState.GetFunction("Test");
  31. func.BeginPCall();
  32. func.Push(str);
  33. func.PCall();
  34. func.EndPCall();
  35. func.Dispose();
  36. }
  37. //屏蔽,例子不需要运行
  38. protected override void CallMain() { }
  39. }

?