TestABLoader.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using LuaInterface;
  6. using System;
  7. //click Lua/Build lua bundle
  8. public class TestABLoader : MonoBehaviour
  9. {
  10. int bundleCount = int.MaxValue;
  11. string tips = null;
  12. IEnumerator CoLoadBundle(string name, string path)
  13. {
  14. using (WWW www = new WWW(path))
  15. {
  16. if (www == null)
  17. {
  18. Debugger.LogError(name + " bundle not exists");
  19. yield break;
  20. }
  21. yield return www;
  22. if (www.error != null)
  23. {
  24. Debugger.LogError(string.Format("Read {0} failed: {1}", path, www.error));
  25. yield break;
  26. }
  27. --bundleCount;
  28. LuaFileUtils.Instance.AddSearchBundle(name, www.assetBundle);
  29. www.Dispose();
  30. }
  31. }
  32. IEnumerator LoadFinished()
  33. {
  34. while (bundleCount > 0)
  35. {
  36. yield return null;
  37. }
  38. OnBundleLoad();
  39. }
  40. public IEnumerator LoadBundles()
  41. {
  42. string streamingPath = Application.streamingAssetsPath.Replace('\\', '/');
  43. #if UNITY_5
  44. #if UNITY_ANDROID && !UNITY_EDITOR
  45. string main = streamingPath + "/" + LuaConst.osDir + "/" + LuaConst.osDir;
  46. #else
  47. string main = "file:///" + streamingPath + "/" + LuaConst.osDir + "/" + LuaConst.osDir;
  48. #endif
  49. WWW www = new WWW(main);
  50. yield return www;
  51. AssetBundleManifest manifest = (AssetBundleManifest)www.assetBundle.LoadAsset("AssetBundleManifest");
  52. List<string> list = new List<string>(manifest.GetAllAssetBundles());
  53. #else
  54. //此处应该配表获取
  55. List<string> list = new List<string>() { "lua.unity3d", "lua_cjson.unity3d", "lua_system.unity3d", "lua_unityengine.unity3d", "lua_protobuf.unity3d", "lua_misc.unity3d", "lua_socket.unity3d", "lua_system_reflection.unity3d" };
  56. #endif
  57. bundleCount = list.Count;
  58. for (int i = 0; i < list.Count; i++)
  59. {
  60. string str = list[i];
  61. #if UNITY_ANDROID && !UNITY_EDITOR
  62. string path = streamingPath + "/" + LuaConst.osDir + "/" + str;
  63. #else
  64. string path = "file:///" + streamingPath + "/" + LuaConst.osDir + "/" + str;
  65. #endif
  66. string name = Path.GetFileNameWithoutExtension(str);
  67. StartCoroutine(CoLoadBundle(name, path));
  68. }
  69. yield return StartCoroutine(LoadFinished());
  70. }
  71. void Awake()
  72. {
  73. #if UNITY_5
  74. Application.logMessageReceived += ShowTips;
  75. #else
  76. Application.RegisterLogCallback(ShowTips);
  77. #endif
  78. LuaFileUtils file = new LuaFileUtils();
  79. file.beZip = true;
  80. StartCoroutine(LoadBundles());
  81. }
  82. void ShowTips(string msg, string stackTrace, LogType type)
  83. {
  84. tips += msg;
  85. tips += "\r\n";
  86. }
  87. void OnGUI()
  88. {
  89. GUI.Label(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 150, 400, 300), tips);
  90. }
  91. void OnApplicationQuit()
  92. {
  93. #if UNITY_5
  94. Application.logMessageReceived -= ShowTips;
  95. #else
  96. Application.RegisterLogCallback(null);
  97. #endif
  98. }
  99. void OnBundleLoad()
  100. {
  101. LuaState state = new LuaState();
  102. state.Start();
  103. state.DoString("print('hello tolua#:'..tostring(Vector3.zero))");
  104. state.DoFile("Main.lua");
  105. LuaFunction func = state.GetFunction("Main");
  106. func.Call();
  107. func.Dispose();
  108. state.Dispose();
  109. state = null;
  110. }
  111. }

?