SLuaEnv.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SFramework
  5. {
  6. public static class SLuaEnv
  7. {
  8. private static XLua.LuaEnv _luaState = null;
  9. public static XLua.LuaEnv Instance
  10. {
  11. get
  12. {
  13. if (null == _luaState)
  14. {
  15. _luaState = new XLua.LuaEnv();
  16. _luaState.AddLoader(SLuaEnv.CustomLoader);
  17. _luaState.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);
  18. _luaState.AddBuildin("lpeg", XLua.LuaDLL.Lua.LoadLpeg);
  19. _luaState.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf);
  20. _luaState.AddBuildin("ffi", XLua.LuaDLL.Lua.LoadFFI);
  21. }
  22. return _luaState;
  23. }
  24. private set
  25. {
  26. Debug.Assert(false, "Can't set the Instance");
  27. }
  28. }
  29. private static TextAsset GetTextAsset(string fullLuaScriptPath)
  30. {
  31. #if UNITY_EDITOR
  32. string resFinalPath = "Assets/" + SFrameworkDefR.ResourceDir + "/" + fullLuaScriptPath;
  33. TextAsset luaTextAssets = UnityEditor.AssetDatabase.LoadAssetAtPath(resFinalPath, typeof(UnityEngine.Object)) as TextAsset;
  34. #else
  35. TextAsset luaTextAssets = SFramework.SResourceManagerR.LoadResource(fullLuaScriptPath) as TextAsset;
  36. #endif
  37. return luaTextAssets;
  38. }
  39. private static byte[] CustomLoader(ref string luaScriptName)
  40. {
  41. //Debug.Log("CustomLoader Work Once. ---> " + luaScriptName);
  42. string fullLuaScriptPath = "LuaScripts/" + luaScriptName + ".txt";
  43. //Debug.Log("FullLuaScriptPath -> " + fullLuaScriptPath);
  44. //TextAsset luaTextAssets = SFramework.SResourceManagerR.LoadResource(fullLuaScriptPath) as TextAsset;
  45. TextAsset luaTextAssets = GetTextAsset(fullLuaScriptPath);
  46. //Debug.Log("luaTextAssets -> " + luaTextAssets.text);
  47. Debug.Log(string.Format(
  48. "SFramework.SLuaEnv.CustomLoader -> luaScriptName: {0} \nluaTextAssets: {2}",
  49. luaScriptName, fullLuaScriptPath, luaTextAssets.text
  50. ));
  51. return luaTextAssets.bytes;
  52. }
  53. }
  54. }