/// <summary> /// 异步加载资源 /// </summary> public void LoadResourceAsync(string assetPath, OnLoadResAsyncDone asynCallback) { Object assetObj = LoadAssetFromCache(assetPath); if (null == assetObj) { if (m_callbackLoadingDict.ContainsKey(assetPath)) { //资源为空,说明资源等待加载或在正在加载( 该资源在等待队列或加载队列中) m_callbackLoadingDict[assetPath] += asynCallback; } else { m_callbackLoadingDict.Add(assetPath, asynCallback); //理论上 该资源不会包含在等待队列、加载队列中 if (!m_assetWaitLoadQueue.Contains(assetPath) && !m_assetLoadingList.Contains(assetPath)) { m_assetWaitLoadQueue.Add(assetPath); } } } else { if (null != asynCallback) { asynCallback(assetPath, assetObj); } } }
/// <summary> /// 异步加载资源 /// </summary> private IEnumerator LoadResourceAsync(string assetPath) { ResourceRequest request = Resources.LoadAsync(assetPath); while (!request.isDone) { yield return(null); } if (m_callbackLoadingDict.ContainsKey(assetPath)) { Object assetObj = request.asset; if (null != assetObj) { m_assetLoadedDict[assetPath] = new AssetLoader(assetObj); } else { LoggerHelper.Error(string.Format("LoadResAsync Fail: {0} not exist.", assetPath)); } OnLoadResAsyncDone asyncCallback = m_callbackLoadingDict[assetPath]; m_callbackLoadingDict.Remove(assetPath); m_assetLoadingList.Remove(assetPath); if (null != asyncCallback) { asyncCallback(assetPath, assetObj); } } else { LoggerHelper.Error(string.Format("LoadResAsync Callback Error: key {0} not exist.", assetPath)); throw new MissingReferenceException(string.Format("LoadResAsync Callback Error: key {0} not exist.", assetPath)); } }