protected override IEnumerator OnLoadAssetAsync(AssetLoadHandler handler) { UnityWebRequest webRequest = UnityWebRequest.Get(handler.path); webRequest.timeout = 30; handler.timeoutChecker.stayTime = webRequest.timeout; yield return(webRequest.SendWebRequest()); if (webRequest.isHttpError || webRequest.isNetworkError) { handler.result = AssetLoadResult.EMPTY_RESULT; handler.status = AssetLoadStatus.LOAD_ERROR; handler.Callback(); } else { //因为第一次可能还没加载完,返回的是0没有数据,所以需要判断一下 while (webRequest.responseCode != 200) { yield return(null); //这里只能等自己超时了 } if (webRequest.responseCode == 200)//200表示接受成功 { byte[] data = webRequest.downloadHandler.data; handler.result = new AssetLoadResult(data, true); handler.status = AssetLoadStatus.LOAD_FINISHED; handler.Callback(); } } }
protected override void OnLoadAssetSync(AssetLoadHandler handler) { string filePath = handler.path; try { FileInfo fi = new FileInfo(filePath); byte[] buff = new byte[fi.Length]; FileStream fs = fi.OpenRead(); fs.Read(buff, 0, Convert.ToInt32(fs.Length)); fs.Close(); handler.result = new AssetLoadResult(buff, true); handler.status = AssetLoadStatus.LOAD_FINISHED; handler.Callback(); } catch { handler.result = AssetLoadResult.EMPTY_RESULT; handler.status = AssetLoadStatus.LOAD_ERROR; handler.Callback(); } }
protected override IEnumerator OnLoadAssetAsync(AssetLoadHandler handler) { var filePath = handler.path; using (FileStream fsRead = new FileStream(filePath, FileMode.Open)) { int fsLen = (int)fsRead.Length; int stLen = 1024 * 1024; byte[] heByte = new byte[fsLen]; int hadReadedLen = 0; int needReadLen = hadReadedLen + stLen < fsLen ? stLen : fsLen - hadReadedLen; while (true) { int r = fsRead.Read(heByte, hadReadedLen, needReadLen); hadReadedLen = hadReadedLen + r; if (hadReadedLen >= fsLen) { handler.result = new AssetLoadResult(heByte, true); handler.status = AssetLoadStatus.LOAD_FINISHED; handler.Callback(); yield break; } else { yield return(null); } } } }
//如果存在加载过的,直接下一帧返回,不需要启动协程 protected override void OnStartLoad(AssetLoadHandler handler) { if (handler == null) { return; } if (!m_loadNodes.ContainsKey(handler.id)) { handler.loader = this; var loadNode = GetOrCreateLoadNode(); var loadMethod = GetLoadMethod(handler); if (loadMethod == LoadMethod.Coroutine) { loadNode.coroutine = StartCoroutine(LoadAssetCoroutine(handler)); } else if (loadMethod == LoadMethod.Nextframe) { loadNode.nextframe = StartNextframe(() => { LoadAssetNextframe(handler); }); } else if (loadMethod == LoadMethod.Immediately) { OnLoadAssetSync(handler); } m_loadNodes[handler.id] = loadNode; } }
private LoadMethod GetLoadMethod(AssetLoadHandler handler) { if (handler.mode == AssetLoadMode.Sync) { return(LoadMethod.Immediately); } return(OnLoadMethod(handler)); }
protected LoadNode GetLoadNode(AssetLoadHandler handler) { if (m_loadNodes.ContainsKey(handler.id)) { return(m_loadNodes[handler.id]); } return(null); }
protected void StopLoadWithHandler(AssetLoadHandler handler) { var loadingMap = GetLoadingMap(); if (loadingMap.ContainsKey(handler.path)) { handler.status = AssetLoadStatus.LOAD_ABORT; OnStopLoad(handler); } }
protected override void OnLoadAssetSync(AssetLoadHandler handler) { string assetPath = handler.path; var request = Resources.Load(assetPath); handler.status = AssetLoadStatus.LOAD_FINISHED; var result = new AssetLoadResult(request, true); handler.Callback(result); }
protected void StartLoadWithHandler(AssetLoadHandler handler) { var loadingMap = GetLoadingMap(); if (!loadingMap.ContainsKey(handler.path)) { loadingMap.Add(handler.path, handler); handler.status = AssetLoadStatus.LOAD_LOADING; OnStartLoad(handler); } }
protected override IEnumerator OnLoadAssetAsync(AssetLoadHandler handler) { string assetPath = handler.path; var request = Resources.LoadAsync(assetPath); //Resources加载路径不能有后缀 yield return(request); handler.status = AssetLoadStatus.LOAD_FINISHED; var result = new AssetLoadResult(request.asset, request.isDone); handler.Callback(result); }
protected AssetLoadHandler GetOrCreateHandler(string path) { //先判断正作加载的队列中是否已经正在加载 AssetLoadHandler handler = null; if (m_loadingMap != null && m_loadingMap.TryGetValue(path, out handler)) { return(handler); } handler = AssetLoadHandlerManager.GetInstance().GetOrCreateHandler(); handler.timeoutChecker.UpdateTick(); handler.path = path.ToLower(); return(handler); }
public void AddChild(AssetLoadHandler handler) { handler.m_parents = handler.m_parents ?? new HashSet <AssetLoadHandler>(); m_children = m_children ?? new List <AssetLoadHandler>(); if (handler.m_parents.Contains(this)) { return; } handler.mode = mode; //保持加载方式一致 handler.Retain(); handler.m_parents.Add(this); m_children.Add(handler); }
protected override void OnStopLoad(AssetLoadHandler handler) { if (handler == null) { return; } if (m_loadNodes.ContainsKey(handler.id)) { var loadNode = m_loadNodes[handler.id]; if (loadNode.loadMethod == LoadMethod.Coroutine) { StopCoroutine(loadNode.coroutine); } else if (loadNode.loadMethod == LoadMethod.Nextframe) { StopNextframe(loadNode.nextframe); } m_loadNodes.Remove(handler.id); } }
public virtual void StopLoad(AssetLoadHandler handler) { if (handler.status == AssetLoadStatus.LOAD_WAIT) { if (m_waitQueue != null && m_waitQueue.Count > 0) { for (LinkedListNode <AssetLoadHandler> iterNode = m_waitQueue.Last; iterNode != null; iterNode = iterNode.Previous) { var waitHandler = iterNode.Value; if (waitHandler == handler) { m_waitQueue.Remove(iterNode); GetReleaseQueue().AddLast(handler); break; } } } } else if (handler.status == AssetLoadStatus.LOAD_LOADING) { StopLoadWithHandler(handler); } }