public void LoadConfig() { #if UNITY_EDITOR #else Type = ResourceType.TYPE_BUNDLE; #endif if (IsRead) { return; } IsRead = true; string fsPath = AssetConfigPath + "/Asset.xml"; StreamReader fs = new StreamReader(fsPath); XmlDocument doc = new XmlDocument(); doc.LoadXml(fs.ReadToEnd()); XmlNodeList list = doc.SelectSingleNode("root").ChildNodes; foreach (var current in list) { XmlElement element = current as XmlElement; if (element == null) { continue; } GTResourceUnit u = new GTResourceUnit(); for (int i = 0; i < element.Attributes.Count; i++) { XmlAttribute attr = element.Attributes[i]; switch (attr.Name) { case "AssetBundleName": u.AssetBundleName = attr.Value; break; case "AssetName": u.AssetName = attr.Value; break; case "Path": u.Path = attr.Value; break; case "GUID": u.GUID = attr.Value; break; } } Units.Add(u.AssetName, u); GTResourceBundle bundle = null; Bundles.TryGetValue(u.AssetBundleName, out bundle); if (bundle == null) { bundle = new GTResourceBundle(); bundle.AssetBundleName = u.AssetBundleName; Bundles.Add(u.AssetBundleName, bundle); } } fs.Dispose(); fs.Close(); }
void AddLoadBundleTask(string assetName, GTResourceBundle bundle, GTResourceTask parentTask, System.Action <Object> callback) { GTResourceTask task = new GTResourceTask(); task.Bundle = bundle; task.LoadedDepCount = 0; task.Parent = parentTask; task.AssetName = assetName; task.AssetCallback = callback; GTCoroutinueManager.Instance.StartCoroutine(LoadAsyncBundle(task)); }
public void UnloadAssetBundle(string assetBundleName, bool all = false) { GTResourceBundle rb = null; Bundles.TryGetValue(assetBundleName, out rb); if (rb != null) { if (rb.State == GTResourceBundle.TYPE_LOADED && rb.AB != null) { Bundles.Remove(assetBundleName); rb.AB.Unload(all); } } }
void LoadBundle(string assetName, System.Action <Object> callback) { GTResourceUnit unit = null; Units.TryGetValue(assetName, out unit); if (unit == null) { Debug.LogError("配置表中不存在:" + assetName); return; } GTResourceBundle bundle = null; Bundles.TryGetValue(unit.AssetBundleName, out bundle); if (bundle == null) { Debug.LogError("不存在这个Bundle:" + unit.AssetBundleName); return; } AddLoadBundleTask(unit.AssetName, bundle, null, callback); }
IEnumerator LoadAsyncManifest(System.Action callback) { string url = string.Format("{0}{1}/{2}", WWWPath, BundlePPath, "AssetBundles"); WWW www = new WWW(url); yield return(www); if (!string.IsNullOrEmpty(www.error)) { Debug.LogError(www.error); yield break; } AssetBundle ab = www.assetBundle; AssetBundleManifest maniFest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest"); ab.Unload(false); foreach (var current in maniFest.GetAllAssetBundles()) { string[] deps = maniFest.GetAllDependencies(current); if (deps == null || deps.Length == 0) { continue; } GTResourceBundle bundle = null; Bundles.TryGetValue(current, out bundle); if (bundle == null) { continue; } bundle.Deps.AddRange(deps); } if (callback != null) { callback.Invoke(); } www.Dispose(); yield return(null); }
IEnumerator LoadAsyncBundle(GTResourceTask task) { GTResourceBundle taskBundle = task.Bundle; AssetBundle ab = null; if (taskBundle.State >= GTResourceBundle.TYPE_LOADING) { while (taskBundle.State != GTResourceBundle.TYPE_LOADED) { yield return(null); } ab = taskBundle.AB; } else { taskBundle.State = GTResourceBundle.TYPE_LOADING; string url = string.Format("{0}{1}/{2}", WWWPath, BundlePPath, taskBundle.AssetBundleName); WWW www = new WWW(url); yield return(www); if (!string.IsNullOrEmpty(www.error)) { Debug.LogError(www.error); yield break; } ab = www.assetBundle; taskBundle.State = GTResourceBundle.TYPE_LOADED; taskBundle.AB = ab; www.Dispose(); } for (int i = 0; i < taskBundle.Deps.Count; i++) { string depAbName = taskBundle.Deps[i]; GTResourceBundle dpBundle = null; Bundles.TryGetValue(depAbName, out dpBundle); if (dpBundle == null) { continue; } AddLoadBundleTask(null, dpBundle, task, null); } if (task.Parent != null) { task.Parent.LoadedDepCount++; } while (task.LoadedDepCount < taskBundle.Deps.Count) { yield return(null); } AssetBundleRequest req = null; if (task.AssetName != null) { req = ab.LoadAssetAsync <Object>(task.AssetName); while (!req.isDone) { yield return(null); } } if (req != null) { if (req.asset == null) { Debug.LogError(ab); yield break; } if (task.AssetCallback != null) { task.AssetCallback.Invoke(req.asset); } } yield return(0); }