public static AsyncLoadRequest AsyncLoad(string resPath, AsyncLoadRequest.AsyncLoadCallBack onAsyncLoadComplete, object context, bool preload) { var request = new AsyncLoadRequest(onAsyncLoadComplete, context, preload); System.Object resObject = ResourceManager.Get(resPath); if (resObject != null) { Callback(resPath, request, resObject as Object); } else { SameRequestCache src; if (Instance_.sameRequestCache_.TryGetValue(resPath, out src)) { src.srcList.Add(request); } else { src = new SameRequestCache(); src.srcList.Add(request); Instance_.sameRequestCache_[resPath] = src; Instance_.requestQueue_.Add(new LoadQueueData(resPath)); } } return(request); }
/// <summary> /// <see cref="ISerializationCallbackReceiver.OnAfterDeserialize"/> implementation. /// </summary> void ISerializationCallbackReceiver.OnAfterDeserialize() { LoadedAsset = null; _assetType = null; asyncLoadRequest = null; asyncLoadedAssetGetter = null; }
/// <summary> /// <para>Unloads the original asset if it's currently loaded.</para> /// <para>Must not be called if there's an unfinished <see cref="AsyncLoadRequest"/> operation (generated from a previous call to <see cref="LoadAsync"/>).</para> /// </summary> /// <exception cref="InvalidOperationException">Thrown if there's an unfinished <see cref="AsyncLoadRequest"/> operation when this method is called.</exception> public void Unload() { if (asyncLoadRequest != null && !asyncLoadRequest.IsDone) { throw new InvalidOperationException("Called DelayedAsset.Unload() when there was an AsyncLoadRequest operation in progress"); } if (loadedAsset != null) { // If the original asset is inside a proxy, unload it. Only if not in the editor, since otherwise it won't allow to load the asset again: #if !UNITY_EDITOR { var originalAsset = GetOriginalAsset(loadedAsset); if (originalAsset != null && originalAsset != loadedAsset) { Resources.UnloadAsset(originalAsset); } } #endif // Unload the loaded asset: Resources.UnloadAsset(loadedAsset); } loadedAsset = null; asyncLoadRequest = null; asyncLoadedAssetGetter = null; }
//回调 private static void Callback(string url, AsyncLoadRequest request, Object resObj) { Object result = resObj; if (!result) { Debug.Log(string.Format("xxx错误加载失败{1},{2},地址:{0}", url, resObj, result)); } else if (resObj is Material || resObj is GameObject) { result = Instantiate(resObj); ReplaceShader(result); } request.LoadFinish(result); }
private static IEnumerator AsyncPreloadRoleBone(float Preloadpercent) { string[] files = Directory.GetFiles(@"res/model", "*.bone", SearchOption.AllDirectories); foreach (string file in files) { AsyncLoadRequest ar = XYSingleAssetLoader.AsyncPreload(file); while (!ar.isDone) { yield return(null); } } yield return(null); AsyncPreloadTotal += Preloadpercent; }
/// <summary> /// <para>Unloads the original asset if it's currently loaded.</para> /// <para>Must not be called if there's an unfinished <see cref="AsyncLoadRequest"/> operation (generated from a previous call to <see cref="LoadAsync"/>).</para> /// </summary> /// <param name="forceUnloadAllUnusedAssetsIfAssetNotUnloadable">If the original asset is of a type that cannot be directly unloaded by Unity (<see cref="Component"/>, <see cref="GameObject"/>, or <see cref="AssetBundle"/>), force a call to <see cref="Resources.UnloadUnusedAssets"/> instead. Use it with care, since it may be slow.</param> /// <exception cref="InvalidOperationException">Thrown if there's an unfinished <see cref="AsyncLoadRequest"/> operation when this method is called.</exception> public void Unload(bool forceUnloadAllUnusedAssetsIfAssetNotUnloadable = false) { if (asyncLoadRequest != null && !asyncLoadRequest.IsDone) { throw new InvalidOperationException("Called DelayedAsset.Unload() when there was an AsyncLoadRequest operation in progress"); } if (LoadedAsset != null) { // If the original asset is inside a proxy, unload it. Only if not in the editor, since otherwise it won't allow to load the asset again: #if !UNITY_EDITOR { var originalAsset = GetOriginalAsset(LoadedAsset); if (originalAsset != null && originalAsset != LoadedAsset) { if (CanBeDirectlyUnloaded(originalAsset)) { Resources.UnloadAsset(originalAsset); } else if (forceUnloadAllUnusedAssetsIfAssetNotUnloadable) { originalAsset = null; Resources.UnloadUnusedAssets(); } } } #endif // Unload the loaded asset: if (CanBeDirectlyUnloaded(LoadedAsset)) { Resources.UnloadAsset(LoadedAsset); } else if (forceUnloadAllUnusedAssetsIfAssetNotUnloadable) { LoadedAsset = null; Resources.UnloadUnusedAssets(); } } LoadedAsset = null; asyncLoadRequest = null; asyncLoadedAssetGetter = null; }
/// <summary> /// Asynchronously loads the original asset. Does not attempt to load it again if it was already loaded. /// To unload it do not use <see cref="Resources.UnloadAsset(UnityEngine.Object)"/>, use <see cref="Unload"/> instead. /// </summary> /// <returns>An <see cref="AsyncLoadRequest"/> object, from which the asset can be retrieved once the operation is completed. Will be null if the original asset couldn't be found, or hadn't been assigned.</returns> public AsyncLoadRequest LoadAsync() { if (asyncLoadRequest == null) { if (loadedAsset != null) { asyncLoadRequest = new AsyncLoadRequest(loadedAsset, out asyncLoadedAssetGetter); } else if (!string.IsNullOrEmpty(assetRelativePath)) { ResourceRequest resourceRequest = Resources.LoadAsync(assetRelativePath, assetType); asyncLoadRequest = new AsyncLoadRequest(resourceRequest, out asyncLoadedAssetGetter); } } return(asyncLoadRequest); }
/// <summary> /// Asynchronously loads the original asset. Does not attempt to load it again if it was already loaded. /// To unload it do not use <see cref="Resources.UnloadAsset(UnityEngine.Object)"/>, use <see cref="Unload"/> instead. /// </summary> /// <returns>An <see cref="AsyncLoadRequest"/> object, from which the asset can be retrieved once the operation is completed. Will be null if the original asset couldn't be found, or hadn't been assigned.</returns> public AsyncLoadRequest LoadAsync() { #if UNITY_EDITOR { UpdateRuntimeSerializedData(); } #endif if (asyncLoadRequest == null) { if (LoadedAsset != null) { asyncLoadRequest = new AsyncLoadRequest(LoadedAsset, out asyncLoadedAssetGetter); } else if (!string.IsNullOrEmpty(assetRelativePath)) { ResourceRequest resourceRequest = Resources.LoadAsync(assetRelativePath, AssetType); asyncLoadRequest = new AsyncLoadRequest(resourceRequest, out asyncLoadedAssetGetter); } } return(asyncLoadRequest); }