/// <summary> /// Resource异步加载; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <param name="action">资源回调</param> /// <param name="progress">进度回调</param> /// <returns></returns> public IEnumerator <float> LoadResAsync <T>(AssetType type, string assetName, Action <T> action, Action <float> progress) where T : Object { string path = FilePathUtility.GetResourcePath(type, assetName); IAssetLoader <T> loader = CreateLoader <T>(type); T ctrl = null; bool isInstance = false; if (path != null) { ResourceRequest request = Resources.LoadAsync <T>(path); while (request.progress < 0.99) { if (progress != null) { progress(request.progress); } yield return(Timing.WaitForOneFrame); } while (!request.isDone) { yield return(Timing.WaitForOneFrame); } ctrl = loader.GetAsset(request.asset as T, out isInstance); } if (action != null) { action(ctrl); } else { LogUtil.LogUtility.PrintError(string.Format("[ResourceMgr]LoadResAsync Load Asset {0} failure!", assetName + "." + type.ToString())); } }
/// <summary> /// Asset sync load from AssetBundle; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <returns>ctrl</returns> public T LoadAssetSync <T>(AssetType type, string assetName) where T : Object { T ctrl = null; IAssetLoader <T> loader = CreateLoader <T>(type); AssetBundle assetBundle = AssetBundleMgr.Instance.LoadAssetBundleSync(type, assetName); bool isInstance = false; if (assetBundle != null) { T tempObject = assetBundle.LoadAsset <T>(assetName); ctrl = loader.GetAsset(tempObject, out isInstance); } if (ctrl == null) { LogUtil.LogUtility.PrintError(string.Format("[ResourceMgr]LoadAssetFromAssetBundleSync Load Asset {0} failure!", assetName + "." + type.ToString())); } else { if (isInstance) { var go = ctrl as GameObject; if (go) { UnityUtility.AddOrGetComponent <AssetBundleTag>(go, (tag) =>//自动卸载; { tag.AssetBundleName = assetName; tag.Type = type; tag.IsClone = false; }); } } } return(ctrl); }
/// <summary> /// Resource异步加载; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <param name="action">资源回调</param> /// <param name="progress">进度回调</param> /// <returns></returns> public IEnumerator LoadResourceAsync <T>(AssetType type, string assetName, Action <T> action, Action <float> progress) where T : Object { string path = FilePathUtil.GetResourcePath(type, assetName); IAssetLoader <T> loader = CreateLoader <T>(type); T ctrl = null; if (path != null) { ResourceRequest request = Resources.LoadAsync <T>(path); while (request.progress < 0.99) { if (progress != null) { progress(request.progress); } yield return(null); } while (!request.isDone) { yield return(null); } ctrl = loader.GetAsset(request.asset as T); } if (action != null) { action(ctrl); } else { Debug.LogError(string.Format("[ResourceMgr]LoadResourceAsync Load Asset {0} failure!", assetName + "." + type.ToString())); } }
/// <summary> /// Resource同步加载; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <returns>ctrl</returns> public T LoadResourceSync <T>(AssetType type, string assetName) where T : Object { string path = FilePathUtil.GetResourcePath(type, assetName); IAssetLoader <T> loader = CreateLoader <T>(type); if (path != null) { T ctrl = Resources.Load <T>(path); if (ctrl != null) { return(loader.GetAsset(ctrl)); } } Debug.LogError(string.Format("[ResourceMgr]LoadResourceSync Load Asset {0} failure!", assetName + "." + type.ToString())); return(null); }
/// <summary> /// Resource同步加载; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <returns>ctrl</returns> public T LoadResSync <T>(AssetType type, string assetName) where T : Object { string path = FilePathUtility.GetResourcePath(type, assetName); IAssetLoader <T> loader = CreateLoader <T>(type); bool isInstance = false; if (path != null) { T ctrl = Resources.Load <T>(path); if (ctrl != null) { return(loader.GetAsset(ctrl, out isInstance)); } } LogUtil.LogUtility.PrintError(string.Format("[ResourceMgr]LoadResSync Load Asset {0} failure!", assetName + "." + type.ToString())); return(null); }
/// <summary> /// Asset sync load from AssetBundle; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <returns>ctrl</returns> public Object LoadAssetFromAssetBundleSync(AssetType type, string assetName) { Object ctrl = null; IAssetLoader <Object> loader = CreateLoader <Object>(type); AssetBundle assetBundle = AssetBundleMgr.Instance.LoadAssetBundleSync(type, assetName); if (assetBundle != null) { Object tempObject = assetBundle.LoadAsset(assetName); ctrl = loader.GetAsset(tempObject); } if (ctrl == null) { Debug.LogError(string.Format("[ResourceMgr]LoadAssetFromAssetBundleSync Load Asset {0} failure!", assetName + "." + type.ToString())); } return(ctrl); }
/// <summary> /// Asset async load from AssetBundle; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <param name="action">资源回调</param> /// <param name="progress">progress回调</param> /// <returns></returns> public IEnumerator LoadAssetFromAssetBundleAsync(AssetType type, string assetName, Action <Object> action, Action <float> progress) { Object ctrl = null; AssetBundle assetBundle = null; IAssetLoader <Object> loader = CreateLoader <Object>(type); IEnumerator itor = AssetBundleMgr.Instance.LoadAssetBundleAsync(type, assetName, ab => { assetBundle = ab; }, null); while (itor.MoveNext()) { yield return(null); } AssetBundleRequest request = assetBundle.LoadAssetAsync(assetName); while (request.progress < 0.99) { if (progress != null) { progress(request.progress); } yield return(null); } while (!request.isDone) { yield return(null); } ctrl = loader.GetAsset(request.asset); if (ctrl == null) { Debug.LogError(string.Format("[ResourceMgr]LoadAssetFromAssetBundleSync Load Asset {0} failure!", assetName + "." + type.ToString())); } if (action != null) { action(ctrl); } }
public void GetAsset <T>(string alias, string url, Action <T> callback, bool asyncHint = false) where T : Object { VerifyUrl(url); if (!url.IsEmpty()) { T o = assetCache.Get(url) as T; if (o != null) { callback(o); } else { callbacks.Add(url, callback); if (callbacks.GetCount(url) == 1) { log.Debug("Loading {0}", url); loader.GetAsset <T>(pathConverter(url), a => { if (a != null) { AddToCache(alias, url, a); } List <Delegate> slot = callbacks.GetSlot(url); callbacks.Remove(url); foreach (Delegate c in slot) { ((Action <T>)c)(a); } }, asyncHint); } } } else { callback(null); } }
/// <summary> /// Asset async load from AssetBundle; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <param name="action">资源回调</param> /// <param name="progress">progress回调</param> /// <returns></returns> public IEnumerator <float> LoadAssetAsync <T>(AssetType type, string assetName, Action <T> action, Action <float> progress) where T : Object { T ctrl = null; AssetBundle assetBundle = null; IAssetLoader <T> loader = CreateLoader <T>(type); IEnumerator itor = AssetBundleMgr.Instance.LoadAssetBundleAsync(type, assetName, ab => { assetBundle = ab; }, null); while (itor.MoveNext()) { yield return(Timing.WaitForOneFrame); } AssetBundleRequest request = assetBundle.LoadAssetAsync <T>(assetName); while (request.progress < 0.99) { if (progress != null) { progress(request.progress); } yield return(Timing.WaitForOneFrame); } while (!request.isDone) { yield return(Timing.WaitForOneFrame); } bool isInstance = false; ctrl = loader.GetAsset(request.asset as T, out isInstance); if (ctrl == null) { LogUtil.LogUtility.PrintError(string.Format("[ResourceMgr]LoadAssetFromAssetBundleSync Load Asset {0} failure!", assetName + "." + type.ToString())); } else { if (isInstance) { var go = ctrl as GameObject; if (go) { UnityUtility.AddOrGetComponent <AssetBundleTag>(go, (tag) =>//自动卸载; { tag.AssetBundleName = assetName; tag.Type = type; tag.IsClone = false; }); } } if (action != null) { action(ctrl); } } }