private void LoadAsset(string prefabName, Type type, ILoadListent callBack, bool isKeepInMemory) { if (string.IsNullOrEmpty(prefabName)) { if (callBack != null) callBack.Failed(); return; } //如果缓存中包含此资源包则直接返回 if (mAssetPacksDic.ContainsKey(prefabName)) { if (mAssetPacksDic[prefabName].asset == null) { if (callBack != null) { callBack.Failed(); } } else { callBack.Succeed(mAssetPacksDic[prefabName].asset); } return; //如果找到就不在往下执行 } //如果不包含,则先判断当前正在加载的列表中是否包含,如果包含则直接把回调事件加入 for (int i = 0; i < mLoadList.Count; i++) { ResLoadRequest request = mLoadList[i]; if (!request.assetName.Equals(prefabName)) continue; request.AddListent(callBack); return; } foreach (ResLoadRequest request in mWaitLoads) { if (!request.assetName.Equals(prefabName)) continue; request.AddListent(callBack); return; } //如果都不存在的话,则加载进等待队列 ResLoadRequest loadRequest = new ResLoadRequest(prefabName, isKeepInMemory, type); loadRequest.listents.Add(callBack); mWaitLoads.Enqueue(loadRequest); }
/// <summary> /// 从Resource加载一个资源 /// </summary> /// <param name="prefabName">资源名称</param> /// <param name="type">类型</param> /// <param name="callBack">加载完成后的回调</param> /// <param name="isKeepInMemory">是否常驻内存</param> public void LoadAssetAsync(string prefabName, Type type, ILoadListent callBack, bool isKeepInMemory = false) { LoadAsset(prefabName, type, callBack, isKeepInMemory); }
/// <summary> /// 添加一个回调到回调列表 /// </summary> /// <param name="listent"></param> public void AddListent(ILoadListent listent) { if (listents == null || listents.Contains(listent)) return; listents.Add(listent); }