SLuaEnv.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }
  18. return _luaState;
  19. }
  20. private set
  21. {
  22. Debug.Assert(false, "Can't set the Instance");
  23. }
  24. }
  25. private static TextAsset GetTextAsset(string fullLuaScriptPath)
  26. {
  27. #if UNITY_EDITOR
  28. string resFinalPath = "Assets/" + SFrameworkDefR.ResourceDir + "/" + fullLuaScriptPath;
  29. TextAsset luaTextAssets = UnityEditor.AssetDatabase.LoadAssetAtPath(resFinalPath, typeof(UnityEngine.Object)) as TextAsset;
  30. #else
  31. TextAsset luaTextAssets = SFramework.SResourceManagerR.LoadResource(fullLuaScriptPath) as TextAsset;
  32. #endif
  33. return luaTextAssets;
  34. }
  35. private static byte[] CustomLoader(ref string luaScriptName)
  36. {
  37. //Debug.Log("CustomLoader Work Once. ---> " + luaScriptName);
  38. string fullLuaScriptPath = "LuaScripts/" + luaScriptName + ".txt";
  39. //Debug.Log("FullLuaScriptPath -> " + fullLuaScriptPath);
  40. //TextAsset luaTextAssets = SFramework.SResourceManagerR.LoadResource(fullLuaScriptPath) as TextAsset;
  41. TextAsset luaTextAssets = GetTextAsset(fullLuaScriptPath);
  42. //Debug.Log("luaTextAssets -> " + luaTextAssets.text);
  43. Debug.Log(string.Format(
  44. "SFramework.SLuaEnv.CustomLoader -> luaScriptName: {0} \nluaTextAssets: {2}",
  45. luaScriptName, fullLuaScriptPath, luaTextAssets.text
  46. ));
  47. return luaTextAssets.bytes;
  48. }
  49. }
  50. }