LuaManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 
  2. using UnityEngine;
  3. using System;
  4. using System.Collections.Generic;
  5. #if USE_LUA
  6. using LuaInterface;
  7. #endif
  8. public class LuaManager
  9. {
  10. public const string c_LuaConfigName = "LuaConfig";
  11. public const string c_LuaLibraryListKey = "LuaLibList";
  12. public const string c_LuaListKey = "LuaList";
  13. #if USE_LUA
  14. private static LuaState s_state = new LuaState();
  15. public static LuaState LuaState
  16. {
  17. get { return s_state; }
  18. }
  19. public static bool s_isUpdate = false;
  20. /// <summary>
  21. /// 这里仅仅初始化LuaState,热更新结束后调用StartLua正式启动Lua
  22. /// </summary>
  23. public static void Init()
  24. {
  25. try
  26. {
  27. s_state.Start();
  28. //LuaBinder.Bind(s_state);
  29. ApplicationManager.s_OnApplicationUpdate += Update;
  30. }
  31. catch (Exception e)
  32. {
  33. Debug.LogError("Lua Init Execption " + e.ToString());
  34. }
  35. }
  36. /// <summary>
  37. /// 加载全部Lua文件
  38. /// </summary>
  39. public static void LoadLua()
  40. {
  41. //Debug.Log("LoadLua");
  42. try
  43. {
  44. Dictionary<string,SingleField> data = ConfigManager.GetData(c_LuaConfigName);
  45. //先取出所有库文件执行
  46. string[] luaLibList = data[c_LuaLibraryListKey].GetStringArray();
  47. for (int i = 0; i < luaLibList.Length; i++)
  48. {
  49. DoLuaFile(luaLibList[i]);
  50. }
  51. //再取出所有的Lua文件并执行
  52. string[] luaList = data[c_LuaListKey].GetStringArray();
  53. for (int i = 0; i < luaList.Length; i++)
  54. {
  55. DoLuaFile(luaList[i]);
  56. }
  57. }
  58. catch (Exception e)
  59. {
  60. Debug.LogError("Lua Start Execption " + e.ToString());
  61. }
  62. }
  63. /// <summary>
  64. /// 启动Lua
  65. /// </summary>
  66. public static void LaunchLua()
  67. {
  68. //Debug.Log("LaunchLua");
  69. try
  70. {
  71. s_state.GetFunction("Main").Call();
  72. s_isUpdate = true;
  73. s_updateFunction = s_state.GetFunction("LuaUpdate");
  74. }
  75. catch (Exception e)
  76. {
  77. Debug.LogError("Lua Lunch Execption " + e.ToString());
  78. }
  79. }
  80. public static object[] CallFunction(string functionName,params object[] objs)
  81. {
  82. var function = LuaState.GetFunction(functionName);
  83. if(function != null)
  84. {
  85. return function.Call(objs);
  86. }
  87. else
  88. {
  89. throw new Exception("找不到Lua函数 ->" + functionName + "<-");
  90. }
  91. }
  92. static LuaFunction s_updateFunction;
  93. static void Update()
  94. {
  95. if(s_isUpdate)
  96. {
  97. s_updateFunction.Call(Time.deltaTime * 1000);
  98. }
  99. }
  100. public static void DoLuaFile(string fileName)
  101. {
  102. string content = AssetsPoolManager.ReadTextFile(fileName);
  103. s_state.DoString(content, fileName);
  104. }
  105. #endif
  106. }