/// <summary> /// 获取到某个资源 /// </summary> /// <param name="path">资源路径</param> /// <param name="onSucc">成功时的回调</param> /// <param name="onFail">失败时的回调</param> /// <param name="resType">资源类型,默认是UnKnow,如果是在bundle模式下,并且传入Unknow类型,会改成AssetBundle类型,如果text或AudioClip不打成bungle,必须传入类型,获取资源使用特定的方法</param> /// <returns>返回当前加载的Resource</returns> public Resource GetResource(string path, ResourceHandler onSucc = null, ResourceHandler onFail = null, ResourceType resType = ResourceType.UnKnow) { if (string.IsNullOrEmpty(path)) { CLog.LogError("[GetResource]ResName can not is null!"); return(null); } Resource res; _mapRes.TryGetValue(GetRealResourcePath(path), out res); if (res != null) { if (res.isDone) { if (res.isSucc && onSucc != null) { ResourceHandler tempOnSucc = onSucc; onSucc = null; tempOnSucc.Invoke(res, path); tempOnSucc = null; } } else { AddListener(res, path, onSucc, onFail); } return(res); } res = new Resource(); res.realPath = GetRealResourcePath(path); res.resType = (!DirectLoadMode && resType == ResourceType.UnKnow) ? ResourceType.AssetBundle : resType; //这个需要在加载依赖之前放入字典中(如果出现循环引用时需要直接获取) _mapRes.Add(res.realPath, res); res.Retain(); //获取到当前资源的依赖资源(可能没法保证顺序,所以拿的时候需要保证所有依赖资源都已经加载好) if (!DirectLoadMode && res.resType == ResourceType.AssetBundle) { string[] listDependResPath = GetDependResPath(path); if (listDependResPath != null && listDependResPath.Length > 0) { List <Resource> listDependRes = new List <Resource>(); for (int i = 0; i < listDependResPath.Length; i++) { //加载依赖资源 Resource dependRes = GetResource(listDependResPath[i]); listDependRes.Add(dependRes); } res.SetDependsRes(listDependRes); } } //真正加载当前资源 //_mapRes.Add(res.realPath, res); //res.Retain(); AddListener(res, path, onSucc, onFail); _resLoader.Load(res); return(res); }
private void OnResourceLoaded(Resource res) { if (m_cResource != null) { m_cResource.Release(); } m_cResource = res; m_cResource.Retain(); UnityEngine.Object objPrefab = m_cResource.GetAsset(null); this.MainGO = GameObject.Instantiate(objPrefab) as GameObject; if (this.MainGO != null) { this.MainGO.transform.SetParent(ViewSys.Instance.Root2D.transform, false); this.MainGO.name = this.viewName; } this._subViews = this.BuildSubViews(); if (this._subViews != null) { int count = this._subViews.Count; for (int i = 0; i < count; ++i) { this._subViews[i].viewController = this; } } if (this.MainGO != null) { this.MainGO.SetActive(false); } OnResLoad(); }
private void OnFinish(Resource res) { if (res.isSucc) { res.Retain(); _mapRes.Add(res.path, res); } else { CLog.LogError("[MultiResourceLoader] load " + res.path + " fail!"); } _finishCount++; List <Action <Resource> > list; _mapTryGetRes.TryGetValue(res.path, out list); if (list != null) { _mapTryGetRes.Remove(res.path); for (int i = 0; i < list.Count; i++) { list[i].Invoke(res); } } if (_OnProgress != null) { Action <Resource> tempAction = _OnProgress; tempAction.Invoke(res); } if (_finishCount == _loadList.Count) { foreach (var item in _mapTryGetRes) { Resource tempRes; _mapRes.TryGetValue(item.Key, out tempRes); if (tempRes == null) { continue; } for (int i = 0; i < item.Value.Count; i++) { item.Value[i].Invoke(tempRes); } } _mapTryGetRes.Clear(); if (_OnComplete != null) { Action <MultiResourceLoader> tempAction = _OnComplete; _OnComplete = null; tempAction.Invoke(this); } } }
private void OnResourceLoaded(Resource res, string path) { if (m_cResource != null) { m_cResource.Release(); } m_cResource = res; m_cResource.Retain(); UnityEngine.Object objPrefab = m_cResource.GetAsset(path); this.MainGO = GameObject.Instantiate(objPrefab) as GameObject; if (this.MainGO != null) { this.MainGO.transform.SetParent(ViewSys.Instance.Root2D.transform, false); this.MainGO.name = this.viewName; } this._subViews = this.BuildSubViews(); if (this._subViews != null) { int count = this._subViews.Count; for (int i = 0; i < count; ++i) { this._subViews[i].viewController = this; } } if (this.MainGO != null) { Canvas canvas = this.MainGO.AddComponentOnce <Canvas>(); canvas.overrideSorting = true; this.MainGO.AddComponentOnce <UIViewMono>(); canvas.sortingOrder = 0; } if (this.MainGO != null) { this.MainGO.SetActive(false); } OnResLoad(); }
private void OnResLoad(Resource res) { if (res.isSucc) { Queue <GameObject> queueGO = null; if (!m_dicGO.TryGetValue(res, out queueGO)) { queueGO = new Queue <GameObject>(); m_dicGO.Add(res, queueGO); res.Retain(); } List <GameObjectPoolHandler> lstCallback = null; if (m_dicCallback.TryGetValue(res.path, out lstCallback)) { while (lstCallback.Count > 0) { GameObject go = null; if (queueGO.Count > 0) { go = queueGO.Dequeue(); } else { go = GetGameObject(res); } var callback = lstCallback[0]; lstCallback.RemoveAt(0); go.SetActive(true); callback.Invoke(go); } } } else { CLog.LogError("加载unit资源" + res.path + "失败"); } }
public void CacheObject(string path, bool isPrefab, int count, Action <string> callback) { ResourceObjectQueue resQueue; if (m_dicGO.TryGetValue(path, out resQueue)) { if (resQueue.isDone) { if (!isPrefab) { for (int i = 0; i < count; i++) { var go = GetGameObject(resQueue.prefab, path); SaveObject(path, go); } } if (callback != null) { callback.Invoke(path); } return; } } if (callback != null) { List <CacheCallbackStruct> lstCallbackStruct; if (!m_dicCacheCallback.TryGetValue(path, out lstCallbackStruct)) { lstCallbackStruct = new List <CacheCallbackStruct>(); m_dicCacheCallback.Add(path, lstCallbackStruct); } bool contain = false; for (int i = 0; i < lstCallbackStruct.Count; i++) { if (lstCallbackStruct[i].callback == callback) { contain = true; break; } } if (!contain) { CacheCallbackStruct callbackStruct = new CacheCallbackStruct(); callbackStruct.callback = callback; callbackStruct.count = count; callbackStruct.isPrefab = isPrefab; lstCallbackStruct.Add(callbackStruct); } } if (resQueue == null) { resQueue = ObjectPool <ResourceObjectQueue> .Instance.GetObject(); resQueue.isDone = false; resQueue.queue = new Queue <UnityEngine.Object>(); resQueue.prefab = null; m_dicGO.Add(path, resQueue); Resource res = ResourceSys.Instance.GetResource(path, OnResLoadCache); resQueue.res = res; res.Retain(); } }
public UnityEngine.Object GetObject(string path, bool isPrefab, ResourceObjectPoolHandler callback) { ResourceObjectQueue resQueue; if (m_dicGO.TryGetValue(path, out resQueue)) { if (resQueue.isDone) { UnityEngine.Object go = null; if (isPrefab) { go = resQueue.prefab; } else { if (resQueue.queue.Count > 0) { go = resQueue.queue.Dequeue(); } else { go = GetGameObject(resQueue.prefab, path); } ((GameObject)go).SetActive(true); } if (null != callback) { var tempCallback = callback; callback = null; tempCallback.Invoke(path, go); } return(go); } } if (callback != null) { List <GameObjectPoolCallbackStruct> lst = null; if (!m_dicCallback.TryGetValue(path, out lst)) { lst = new List <GameObjectPoolCallbackStruct>(); m_dicCallback.Add(path, lst); } bool contain = false; for (int i = 0; i < lst.Count; i++) { if (lst[i].callback == callback) { contain = true; break; } } if (!contain) { GameObjectPoolCallbackStruct callbackStruct = new GameObjectPoolCallbackStruct(); callbackStruct.callback = callback; callbackStruct.isPrefab = isPrefab; lst.Add(callbackStruct); } } if (resQueue == null) { resQueue = ObjectPool <ResourceObjectQueue> .Instance.GetObject(); resQueue.isDone = false; resQueue.queue = new Queue <UnityEngine.Object>(); resQueue.prefab = null; m_dicGO.Add(path, resQueue); Resource res = ResourceSys.Instance.GetResource(path, OnResLoad); resQueue.res = res; res.Retain(); } return(null); }