//异步加载 public void LoadAssetAsync <T>(string path, UnityAction <T> onLoadComplate) where T : Object { CacheDataInfo info = queryCache(path); if (info != null) { info.UpdateTick(); if (onLoadComplate != null) { onLoadComplate(info.CacheObj as T); } } else { switch (LoadMode) { case AssetLoadMode.Editor: StartCoroutine(editorLoader.LoadAssetAsync <T>(path, onLoadComplate)); break; case AssetLoadMode.AssetBundler: StartCoroutine(abLoader.LoadAssetAsync <T>(path, onLoadComplate)); break; } } }
/// <summary> /// Sets the asynchronous. /// </summary> /// <param name="name">The name.</param> /// <param name="item">The item.</param> /// <param name="keepAlive">The keep alive.</param> /// <returns></returns> public async Task SetAsync(string name, object item, TimeSpan?keepAlive = null) { var cInfo = new CacheDataInfo <object> { CachedDateTime = DateTime.Now, KeepAlive = keepAlive ?? TimeSpan.Zero, NewCacheData = item, }; await _container.SetAsync(name, _serialier.Serialize(cInfo), keepAlive); }
//加入缓冲区 public void pushCache(string path, Object obj) { Debug.Log("[AssetManager]加入缓存:" + path); lock (cacheDataDic) { if (cacheDataDic.ContainsKey(path)) { cacheDataDic[path].UpdateTick(); } else { CacheDataInfo info = new CacheDataInfo(path, obj); cacheDataDic.Add(path, info); info.UpdateTick(); } } }
//同步加载 public T LoadAsset <T>(string path) where T : Object { CacheDataInfo info = queryCache(path); if (info != null) { info.UpdateTick(); return(info.CacheObj as T); } else { switch (LoadMode) { case AssetLoadMode.Editor: return(editorLoader.LoadAsset <T>(path)); case AssetLoadMode.AssetBundler: return(abLoader.LoadAsset <T>(path)); } return(null); } }
/// <summary> /// Determines whether the specified c information has expired. /// </summary> /// <param name="cInfo">The c information.</param> /// <returns> /// <c>true</c> if the specified c information has expired; otherwise, <c>false</c>. /// </returns> private bool HasExpired(CacheDataInfo cInfo) { return(cInfo.KeepAlive != TimeSpan.Zero && DateTime.Now.Subtract(cInfo.CachedDateTime) >= cInfo.KeepAlive); }