SResourceManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SFramework
  5. {
  6. public class SResourceManager
  7. {
  8. private static AssetBundleManifest _mainAssetBundleManifest = null;
  9. private static Dictionary<string, AssetBundle> _loadedAssetBundle = new Dictionary<string, AssetBundle>();
  10. public static string TestCall(string value)
  11. {
  12. Debug.Log("SFramework.SResourceManager.TestCall -> " + value);
  13. return "TestCall Return Value.";
  14. }
  15. public static UnityEngine.Object LoadResource(string resourcePath)
  16. {
  17. UnityEngine.Object targetObject = null;
  18. resourcePath = resourcePath.ToLower();
  19. #pragma warning disable 0162
  20. // Editor Direct Read Res
  21. if (Application.isEditor && !SFrameworkDef.isLoadAssetsBundleOnEditor)
  22. {
  23. string resFinalPath = "";
  24. #if UNITY_EDITOR
  25. resFinalPath = "Assets/" + SFrameworkDef.ResourceDir + "/" + resourcePath;
  26. targetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(resFinalPath, typeof(UnityEngine.Object));
  27. Debug.Assert(targetObject != null);
  28. #endif
  29. return targetObject;
  30. }
  31. #pragma warning restore 0162
  32. // Editor or non-Editor Read AssetsBundle
  33. else
  34. {
  35. // Check Main AssetBundleManifest.
  36. if (_mainAssetBundleManifest == null)
  37. {
  38. LoadMainAssetBundleManifest();
  39. }
  40. string abExtName = SFrameworkDef.AssetsBundleExtName;
  41. targetObject = LoadOneAssetBundle(resourcePath + abExtName);
  42. // Disable it on 20181023 by SunXun. Because targeetObject == null. If the assetbundle.isStreamedSceneAssetBundle == true.
  43. // Debug.Assert(targetObject != null);
  44. return targetObject;
  45. }
  46. }
  47. private static void LoadMainAssetBundleManifest()
  48. {
  49. string mainManifestPath = "";
  50. string platformName = SPlatformInfo.GetBuildPlatformName();
  51. string persistentPath = Application.persistentDataPath + "/Bundles/" + platformName + "/" + platformName;
  52. string streamingPath = Application.streamingAssetsPath + "/Bundles/" + platformName + "/" + platformName;
  53. // Process Special Android Path.
  54. if (platformName == "Android" && !Application.isEditor)
  55. {
  56. streamingPath = Application.dataPath + "!assets/Bundles/" + platformName + "/" + platformName;
  57. mainManifestPath = streamingPath;
  58. }
  59. if (System.IO.File.Exists(streamingPath))
  60. mainManifestPath = streamingPath;
  61. if (System.IO.File.Exists(persistentPath))
  62. mainManifestPath = persistentPath;
  63. if (string.IsNullOrEmpty(mainManifestPath))
  64. {
  65. Debug.LogError(string.Format("LoadMainAssetBundleManifest Failed ! File:{0} Is Not Existed!", mainManifestPath));
  66. return;
  67. }
  68. Debug.Log("LoadMainAssetBundleManifest -> mainManifestPath -> " + mainManifestPath);
  69. AssetBundle manifestAB = AssetBundle.LoadFromFile(mainManifestPath);
  70. Debug.Assert(mainManifestPath != null);
  71. _mainAssetBundleManifest = manifestAB.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
  72. Debug.Assert(_mainAssetBundleManifest != null);
  73. Debug.Log("LoadMainAssetBundleManifest Success!");
  74. }
  75. private static UnityEngine.Object LoadOneAssetBundle(string assetBundleFullName)
  76. {
  77. Debug.Assert(_mainAssetBundleManifest != null);
  78. UnityEngine.Object targetObject;
  79. string resourcePath = "";
  80. string platformName = SPlatformInfo.GetBuildPlatformName();
  81. string persistentPath = Application.persistentDataPath + "/Bundles/" + platformName + "/" + assetBundleFullName;
  82. string streamingPath = Application.streamingAssetsPath + "/Bundles/" + platformName + "/" + assetBundleFullName;
  83. string assetName = GetAssetNameByAssetBundleFullName(assetBundleFullName);
  84. // Process Loaded Asset Bundle.
  85. if (_loadedAssetBundle.ContainsKey(assetBundleFullName))
  86. {
  87. AssetBundle loadedAB = _loadedAssetBundle[assetBundleFullName];
  88. Debug.Assert(loadedAB != null);
  89. targetObject = loadedAB.LoadAsset(assetName);
  90. Debug.Assert(targetObject != null);
  91. Debug.Log(string.Format("LoadOneAssetBundle -> Load By Cache AsetName:{0} From:{1}", assetName, assetBundleFullName));
  92. return targetObject;
  93. }
  94. // Process Special Android Path.
  95. if (platformName == "Android" && !Application.isEditor)
  96. {
  97. streamingPath = Application.dataPath + "!assets/Bundles/" + platformName + "/" + assetBundleFullName;
  98. resourcePath = streamingPath;
  99. }
  100. // For Editor Link.
  101. if (System.IO.File.Exists(streamingPath))
  102. resourcePath = streamingPath;
  103. if (System.IO.File.Exists(persistentPath))
  104. resourcePath = persistentPath;
  105. if (string.IsNullOrEmpty(resourcePath))
  106. {
  107. Debug.LogError(string.Format("LoadOneAssetBundle Failed ! File:{0} Is Not Existed!", assetBundleFullName));
  108. return null;
  109. }
  110. //Debug.Log("LoadOneAssetBundle -> resourcePath -> " + resourcePath);
  111. // Load All Dependencies
  112. string[] dependenciesArray = _mainAssetBundleManifest.GetAllDependencies(assetBundleFullName);
  113. for (var depIndex = 0; depIndex < dependenciesArray.Length; depIndex++)
  114. LoadOneAssetBundle(dependenciesArray[depIndex]);
  115. // Load AssetBundle
  116. AssetBundle resourceAB = AssetBundle.LoadFromFile(resourcePath);
  117. Debug.Assert(resourceAB != null);
  118. if (resourceAB.isStreamedSceneAssetBundle)
  119. {
  120. // isStreamedSceneAssetBundle Can't Be LoadAsset.
  121. // just can load like below.
  122. // string[] scenePath = resourceAB.GetAllScenePaths();
  123. // string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath[0]);
  124. // UnityEngine.SceneManagement.Scene targetScene = UnityEngine.SceneManagement.SceneManager.GetSceneByName(sceneName);
  125. // UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
  126. // targetObject = targetScene as UnityEngine.Object;
  127. targetObject = null;
  128. }
  129. else
  130. {
  131. targetObject = resourceAB.LoadAsset(assetName);
  132. Debug.Assert(targetObject != null);
  133. }
  134. _loadedAssetBundle.Add(assetBundleFullName, resourceAB);
  135. Debug.Log(string.Format("LoadOneAssetBundle -> Load By File AssetName:{0} From:{1}", assetName, assetBundleFullName));
  136. return targetObject;
  137. }
  138. private static string GetAssetNameByAssetBundleFullName(string assetBundleFullName)
  139. {
  140. string[] splitArray = assetBundleFullName.Split('/');
  141. string splitString = splitArray[splitArray.Length - 1];
  142. int extNameLengh = SFrameworkDef.AssetsBundleExtName.Length;
  143. string finalString = splitString.Substring(0, splitString.Length - extNameLengh);
  144. return finalString;
  145. }
  146. private static string GetAssetNameByAssetBundleFullNameWithExt(string assetBundleFullName)
  147. {
  148. string[] splitArray = assetBundleFullName.Split('/');
  149. string splitString = splitArray[splitArray.Length - 1];
  150. string finalString = splitString;
  151. return finalString;
  152. }
  153. public static SFramework.SVersionInfo GetLocalVersionInfo()
  154. {
  155. var platformName = SPlatformInfo.GetBuildPlatformName();
  156. string persistentPath = Application.persistentDataPath + "/Bundles/" + platformName + "/" + SFrameworkDef.VersionFileName;
  157. string streamingPath = Application.streamingAssetsPath + "/Bundles/" + platformName + "/" + SFrameworkDef.VersionFileName;
  158. string versionFilePath = "";
  159. bool canLoadFromFileDirectly = false;
  160. // Process Special Android Real Machine Path.
  161. if (platformName == "Android" && !Application.isEditor)
  162. {
  163. streamingPath = "jar:file://" + Application.dataPath + "!assets/Bundles/" + platformName + "/" + SFrameworkDef.VersionFileName;
  164. versionFilePath = streamingPath;
  165. canLoadFromFileDirectly = false;
  166. }
  167. // For Editor Link.
  168. if (System.IO.File.Exists(streamingPath))
  169. {
  170. versionFilePath = streamingPath;
  171. canLoadFromFileDirectly = true;
  172. }
  173. Debug.Log("persistentPath -> " + persistentPath);
  174. if (System.IO.File.Exists(persistentPath))
  175. {
  176. versionFilePath = persistentPath;
  177. canLoadFromFileDirectly = true;
  178. }
  179. if (string.IsNullOrEmpty(versionFilePath))
  180. {
  181. Debug.LogError(string.Format("GetLocalVersionInfo Failed ! File:{0} Is Not Existed!", versionFilePath));
  182. return null;
  183. }
  184. Debug.Log("GetLocalVersionInfo -> File Path -> " + versionFilePath);
  185. string versionInfo = "";
  186. if (canLoadFromFileDirectly)
  187. {
  188. versionInfo = System.IO.File.ReadAllText(versionFilePath);
  189. }
  190. else
  191. {
  192. UnityEngine.Networking.UnityWebRequest versionFileLoader =
  193. UnityEngine.Networking.UnityWebRequest.Get(versionFilePath);
  194. versionFileLoader.timeout = 3;
  195. versionFileLoader.SendWebRequest();
  196. while (!versionFileLoader.isDone)
  197. {
  198. // just wait.
  199. }
  200. //if (versionFileLoader.isNetworkError || versionFileLoader.isHttpError)
  201. if (versionFileLoader.result == UnityEngine.Networking.UnityWebRequest.Result.ConnectionError || versionFileLoader.result == UnityEngine.Networking.UnityWebRequest.Result.ProtocolError)
  202. {
  203. Debug.LogError("GetLocalVersionInfo -> versionFileLoader error ->" + versionFileLoader.error);
  204. return null;
  205. }
  206. versionInfo = versionFileLoader.downloadHandler.text;
  207. }
  208. SVersionInfo localVersionInfo = LitJson.JsonMapper.ToObject<SVersionInfo>(versionInfo);
  209. Debug.Assert(localVersionInfo != null);
  210. return localVersionInfo;
  211. }
  212. public static void TryDeleteAssetBundle(string assetBundleName)
  213. {
  214. // Here the assetBundleName include ext name , Like -> "prefabs/xxxx.prefab.s"
  215. string platformName = SPlatformInfo.GetBuildPlatformName();
  216. string filePath = Application.persistentDataPath + "/Bundles/" + platformName + "/" + assetBundleName;
  217. string fileManifesetPath = filePath + ".manifest";
  218. // Delete AssetBundle File.
  219. if (System.IO.File.Exists(filePath))
  220. {
  221. System.IO.File.Delete(filePath);
  222. Debug.Log(string.Format("TryDeleteAssetBundle -> Delete File:{0} Path:{1}", assetBundleName, filePath));
  223. }
  224. // Delete AssetBundle.manifest File.
  225. if (System.IO.File.Exists(fileManifesetPath))
  226. {
  227. System.IO.File.Delete(fileManifesetPath);
  228. Debug.Log(string.Format("TryDeleteAssetBundle -> Delete File:{0} Path:{1}", assetBundleName + ".manifest", filePath));
  229. }
  230. }
  231. public static void SaveAssetBundle(string assetBundleName, byte[] assetBundleData)
  232. {
  233. string platformName = SPlatformInfo.GetBuildPlatformName();
  234. string abFullPathWithName = Application.persistentDataPath + "/Bundles/" + platformName + "/" + assetBundleName;
  235. string abFullPath = SToolFunction.GetAssetBundleFullPath(abFullPathWithName);
  236. // Debug.Log("abFullPathWithName -> " + abFullPathWithName);
  237. // Debug.Log("abFullPath -> " + abFullPath);
  238. SToolFunction.MakeSurePathExisted(abFullPath);
  239. System.IO.File.WriteAllBytes(abFullPathWithName, assetBundleData);
  240. // Below is wrong. file size error.
  241. // System.IO.StreamWriter abWriter = new System.IO.StreamWriter(abFullPathWithName);
  242. // abWriter.Write(assetBundleData);
  243. // abWriter.Close();
  244. // abWriter.Dispose();
  245. Debug.Log(string.Format(
  246. "SResourceManager -> SaveAssetBundle abName:{0} abPath:{1}", assetBundleName, abFullPathWithName
  247. ));
  248. }
  249. public static void SaveRemoteVersion(SVersionInfo remoteVersion)
  250. {
  251. string platformName = SPlatformInfo.GetBuildPlatformName();
  252. string versionPath = Application.persistentDataPath + "/Bundles/" + platformName + "/";
  253. string jsonInfo = LitJson.JsonMapper.ToJson(remoteVersion);
  254. SToolFunction.MakeSurePathExisted(versionPath);
  255. versionPath += SFrameworkDef.VersionFileName;
  256. System.IO.StreamWriter versionWriter = new System.IO.StreamWriter(versionPath);
  257. versionWriter.Write(jsonInfo);
  258. versionWriter.Close();
  259. versionWriter.Dispose();
  260. Debug.Log(string.Format(
  261. "SResourceManager -> SaveRemoteVersion -> SavePath:{0} VersionInfo:{1}", versionPath, jsonInfo
  262. ));
  263. }
  264. }
  265. }