/// <summary> /// 添加打入Lua代码的AssetBundle /// </summary> /// <param name="bundle"></param> public void AddBundle(string bundleName) { byte[] stream = null; string url = Util.DataPath + bundleName.ToLower(); if (File.Exists(url)) { stream = File.ReadAllBytes(url); byte[] assetBytes = ResDefine.DecryptAssetbundle(stream); AssetBundle bundle = AssetBundle.LoadFromMemory(assetBytes); if (bundle != null) { bundleName = bundleName.Replace("lua/", "").Replace(".unity3d", ""); base.AddSearchBundle(bundleName.ToLower(), bundle); } } }
/// <summary> /// 载入AssetBundle /// </summary> /// <param name="abname"></param> /// <returns></returns> public AssetBundle LoadAssetBundle(string abname) { abname = abname.ToLower(); if (!abname.EndsWith(ResDefine.ExtName)) { abname += ResDefine.ExtName; } AssetBundleInfo bundle = null; if (m_AssetBundleManifest == null) { return(bundle.m_AssetBundle); } if (!m_LoadedAssetBundles.ContainsKey(abname)) { byte[] stream = null; string uri = Util.DataPath + abname; if (!File.Exists(uri)) { Debug.LogError(uri + " not exist "); return(null); } LoadDependencies(abname); stream = File.ReadAllBytes(uri); if (stream.Length == 0) { Debug.LogError("load error " + abname); return(null); } byte[] assetBytes = ResDefine.DecryptAssetbundle(stream); AssetBundle bundleObj = AssetBundle.LoadFromMemory(assetBytes); if (bundleObj != null) { m_LoadedAssetBundles.Add(abname, new AssetBundleInfo(bundleObj)); } return(bundleObj); } else { m_LoadedAssetBundles.TryGetValue(abname, out bundle); return(bundle.m_AssetBundle); } }
IEnumerator OnLoadAssetBundle(string abName, Type type) { string url = Util.GetRelativePath() + abName; //AssetBundleCreateRequest asset = null; WWW download = null; if (type == typeof(AssetBundleManifest)) { download = new WWW(url); } else { string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName); if (dependencies.Length > 0) { if (!m_Dependencies.ContainsKey(abName)) { m_Dependencies.Add(abName, dependencies); } for (int i = 0; i < dependencies.Length; i++) { string depName = dependencies[i]; AssetBundleInfo bundleInfo = null; if (m_LoadedAssetBundles.TryGetValue(depName, out bundleInfo)) { bundleInfo.m_ReferencedCount++; } else if (!m_LoadRequests.ContainsKey(depName)) { yield return(StartCoroutine(OnLoadAssetBundle(depName, type))); } } } if (m_LoadedAssetBundles.ContainsKey(abName)) { yield break; } download = new WWW(url); download.threadPriority = ThreadPriority.BelowNormal; } yield return(download); if (download.error != null) { Debug.LogError(url); Debug.LogError(download.error); yield break; } AssetBundleInfo kbundleInfo = null; if (m_LoadedAssetBundles.TryGetValue(abName, out kbundleInfo)) { kbundleInfo.m_ReferencedCount++; } else { if (download.bytes.Length == 0) { Debug.LogError("load error " + abName); } byte[] assetBytes = ResDefine.DecryptAssetbundle(download.bytes); AssetBundle assetObj = AssetBundle.LoadFromMemory(assetBytes); if (assetObj != null) { m_LoadedAssetBundles.Add(abName, new AssetBundleInfo(assetObj)); } } download.Dispose(); download = null; }