xLua——自定义加载器

作者:追风剑情 发布于:2017-7-17 16:17 分类:Lua

以下示例代码来自xLua demo

  1. using UnityEngine;
  2. using System.Collections;
  3. using XLua;
  4.  
  5. public class CustomLoader : MonoBehaviour {
  6. LuaEnv luaenv = null;
  7. // Use this for initialization
  8. void Start()
  9. {
  10. luaenv = new LuaEnv();
  11. //添加自定义加载器
  12. luaenv.AddLoader((ref string filename) => {
  13. if (filename == "InMemory")
  14. {
  15. //返回一个xLua对象{ccc = 9999}
  16. string script = "return {ccc = 9999}";
  17. return System.Text.Encoding.UTF8.GetBytes(script);
  18. }
  19. return null;
  20. });
  21. //require('InMemory') 加载文件InMemory,调用这句后会执行自定义加载器。
  22. luaenv.DoString("print('InMemory.ccc=', require('InMemory').ccc)");
  23. }
  24.  
  25. // Update is called once per frame
  26. void Update()
  27. {
  28. if (luaenv != null)
  29. {
  30. luaenv.Tick();
  31. }
  32. }
  33.  
  34. void OnDestroy()
  35. {
  36. luaenv.Dispose();
  37. }
  38. }

运行测试

11111.png

标签: xLua

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号