public void StopSpawn() { CancelInvoke(nameof(SpawnFood)); CancelInvoke(nameof(BrokenMachineEvent)); if (_foodHandler.IsValid()) { Addressables.Release(_foodHandler); } if (spriteAtlas.IsValid()) { Addressables.Release(spriteAtlas); } }
private IEnumerator SetSkyboxInternal(int skyboxIndex) { if (_currentSkyboxMaterialOperationHandle.IsValid()) { Addressables.Release(_currentSkyboxMaterialOperationHandle); } var skyboxMaterialReference = _skyboxMaterials[skyboxIndex]; _currentSkyboxMaterialOperationHandle = skyboxMaterialReference.LoadAssetAsync <Material>(); yield return(_currentSkyboxMaterialOperationHandle); RenderSettings.skybox = _currentSkyboxMaterialOperationHandle.Result; }
public IEnumerator DownloadDependenciesAsync_AutoReleaseHandle_ReleasesCorrectHandle() { yield return(Init()); IList <IResourceLocation> locations; m_Addressables.GetResourceLocations(new object[] { "prefabs_evenBASE" }, typeof(GameObject), Addressables.MergeMode.Intersection, out locations); AsyncOperationHandle op = m_Addressables.DownloadDependenciesAsync(locations, true); yield return(op); Assert.IsFalse(op.IsValid()); }
public void AsyncOperationHandle_ConvertToTypeless_MaintainsValidity() { var op = new FakeTypedOperation(); AsyncOperationHandle <GameObject> typedHandle = new AsyncOperationHandle <GameObject>(op); //implicit conversion of valid op AsyncOperationHandle typelessHandle = (AsyncOperationHandle)typedHandle; Assert.IsNotNull(typelessHandle); Assert.IsTrue(typedHandle.IsValid()); Assert.IsTrue(typelessHandle.IsValid()); //make handle invalid AsyncOperationHandle <GameObject> typedHandle2 = new AsyncOperationHandle <GameObject>(op); typedHandle2.Release(); //implicit conversion of invalid op AsyncOperationHandle invalidHandle = (AsyncOperationHandle)typedHandle; Assert.IsNotNull(invalidHandle); Assert.IsFalse(invalidHandle.IsValid()); Assert.IsFalse(typedHandle.IsValid()); }
public async void Load() { if (_asset == null || _handle.IsValid()) { return; } _handle = _asset.LoadAssetAsync <T>(); await _handle.Task; if (_handle.Status == AsyncOperationStatus.Succeeded) { _data = _handle.Result; } }
public void Release() { if (handle.IsValid()) { var temp = handle; if (loadingTrackers.TryGetValue(handle, out var pair)) { loadingTrackers.Remove(handle); handle.Completed -= OnCompleted; handle = default; pair.callback?.Invoke(this); } else { handle.Completed -= OnCompleted; handle = default; } Addressables.Release(temp); } }
public static async Task <T> LoadOrGetAsync <T>(this AssetReference assetReference) where T : Object { AsyncOperationHandle op = assetReference.OperationHandle; if (op.IsValid()) { AsyncOperationHandle <T> handle = op.Convert <T>(); while (handle.IsDone == false) { await Task.Yield(); } return(handle.Result); } return(await assetReference.LoadAssetAsync <T>().Task); }
private void AssertDownloadDependencyBundlesAreValid(AsyncOperationHandle op) { Assert.AreEqual(AsyncOperationStatus.Succeeded, op.Status); Assert.IsTrue(op.IsValid()); var opList = (List <IAssetBundleResource>)op.Result; Assert.AreEqual(2, opList.Count); if (opList.Count > 0) { foreach (var resultBundle in opList) { Assert.NotNull(resultBundle.GetAssetBundle()); } } }
/// <inheritdoc/> public void Stop() { StopAllCoroutines(); musicController.Stop(); // Bug: IsValid is false if loading I think. Or release does not work. if (currentOperationHandle.IsValid()) { Addressables.Release <AudioClip>(currentOperationHandle); } // Bug: IsValid is false if loading I think. if (oldOperationHandle.IsValid()) { Addressables.Release <AudioClip>(oldOperationHandle); } isPlaying = false; }
public IEnumerator AddressablesLoad() { AsyncOperationHandle <GameObject> loadAssetHandle = Addressables.LoadAssetAsync <GameObject>("Assets/Prefabs/Complex.prefab"); _loadAssetHandle = loadAssetHandle; yield return(loadAssetHandle); if (loadAssetHandle.IsValid()) { if (loadAssetHandle.Status == AsyncOperationStatus.Succeeded) { _instantiateHandle = Addressables.InstantiateAsync("Assets/Prefabs/Complex.prefab"); _objectComplex = _instantiateHandle.Result; } } }
internal static IEnumerator UnloadSceneFromHandlerRefCountCheck(AsyncOperationHandle <SceneInstance> op, AddressablesImpl addressables) { string sceneName = op.Result.Scene.name; Assert.IsNotNull(sceneName); var prevRefCount = op.ReferenceCount; var unloadOp = addressables.UnloadSceneAsync(op, false); yield return(unloadOp); Assert.AreEqual(AsyncOperationStatus.Succeeded, unloadOp.Status); Assert.IsFalse(unloadOp.Result.Scene.isLoaded); if (op.IsValid()) { Assert.AreEqual(prevRefCount - 1, op.ReferenceCount); } }
public IEnumerator AddressablesImpl_DownloadDependenciesAsync_CantDownloadWhenGetResourceLocFailsAutoReleasesKey() { // Setup yield return(Init()); string label = "badLabel"; bool autoRelease = true; AsyncOperationHandle op = new AsyncOperationHandle(); using (new IgnoreFailingLogMessage()) { op = m_Addressables.DownloadDependenciesAsync(label, autoRelease); yield return(op); } // Test Assert.IsFalse(op.IsValid()); }
public void AsyncOperationHandle_ReleaseToZero_InvalidatesAllHandles() { var op = new FakeTypedOperation(); AsyncOperationHandle <GameObject> typedHandle = new AsyncOperationHandle <GameObject>(op); AsyncOperationHandle <GameObject> typedHandle2 = new AsyncOperationHandle <GameObject>(op); typedHandle.Release(); Assert.IsFalse(typedHandle.IsValid()); Assert.IsFalse(typedHandle2.IsValid()); op = new FakeTypedOperation(); AsyncOperationHandle typelessHandle = new AsyncOperationHandle(op); AsyncOperationHandle typelessHandle2 = new AsyncOperationHandle(op); typelessHandle.Release(); Assert.IsFalse(typelessHandle.IsValid()); Assert.IsFalse(typelessHandle2.IsValid()); }
/// <inheritdoc /> protected override bool InvokeWaitForCompletion() { if (IsDone) { return(true); } if (m_DepOp.IsValid() && !m_DepOp.IsDone) { m_DepOp.WaitForCompletion(); } m_RM?.Update(Time.deltaTime); if (!HasExecuted) { InvokeExecute(); } m_Addressables.ResourceManager.Update(Time.deltaTime); return(IsDone); }
/// <summary> /// Load the level according to <c>_curLevelID</c>, /// unload the previous loaded level, and reset the static manager /// </summary> private void LoadLevel() { // If the level is loaded before the game started, unload it by SceneManager // Because it's not loaded by the SceneLoader if (!string.IsNullOrEmpty(_initialLoadedScenePath)) { SceneManager.UnloadSceneAsync(_initialLoadedScenePath); _initialLoadedScenePath = ""; } else if (_curLevelHandle.IsValid()) { SceneLoader.UnloadScene(_curLevelHandle, OnLevelUnLoaded); } GamePause(); OnLevelEnded?.Invoke(); SceneLoader.LoadScene( _levelData.GetLevelScene(curLevelID), LoadSceneMode.Additive, OnLevelLoaded); }
/// <inheritdoc /> protected override bool InvokeWaitForCompletion() { if (IsDone) { return(true); } if (m_DepOp.IsValid() && !m_DepOp.IsDone) { m_DepOp.WaitForCompletion(); } m_RM?.Update(Time.unscaledDeltaTime); if (!HasExecuted) { InvokeExecute(); } m_RM?.Update(Time.unscaledDeltaTime); return(IsDone); }
void Update() { if (preloadOp.IsValid()) { loadingText.text = string.Format("Loading: {0}%", (int)(preloadOp.PercentComplete * 100)); if (preloadOp.PercentComplete == 1) { Addressables.Release(preloadOp); preloadOp = new AsyncOperationHandle(); loadingText.text = ""; } } if (restart) { if (Input.GetKeyDown(KeyCode.R) || Input.GetButton("Fire1")) { // ADDRESSABLES UPDATES Addressables.LoadSceneAsync(nextSceneAddress); } } }
public IEnumerator AddressablesLoad(string assetName) { if (_objectComplex != null) { DestroyComplex(); } AsyncOperationHandle <GameObject> loadAssetHandle = Addressables.LoadAssetAsync <GameObject>(assetName); //加载到内存中 yield return(loadAssetHandle); if (loadAssetHandle.Status == AsyncOperationStatus.Succeeded && loadAssetHandle.IsValid()) { //实例化 _instantiateHandle = Addressables.InstantiateAsync(assetName); //Addressables.UnloadSceneAsync _objectComplex = _instantiateHandle.Result; Addressables.Release(loadAssetHandle); } }
public void AsyncOperationHandle_ReleaseToNonZero_InvalidatesOnlyCurrentHandle() { var op = new FakeTypedOperation(); AsyncOperationHandle <GameObject> typedHandle = new AsyncOperationHandle <GameObject>(op); IncreaseRefCount(typedHandle, 1); AsyncOperationHandle <GameObject> typedHandle2 = new AsyncOperationHandle <GameObject>(op); typedHandle.Release(); Assert.IsFalse(typedHandle.IsValid()); Assert.IsTrue(typedHandle2.IsValid()); op = new FakeTypedOperation(); AsyncOperationHandle typelessHandle = new AsyncOperationHandle(op); IncreaseRefCount(typelessHandle, 1); AsyncOperationHandle typelessHandle2 = new AsyncOperationHandle(op); typelessHandle.Release(); Assert.IsFalse(typelessHandle.IsValid()); Assert.IsTrue(typelessHandle2.IsValid()); }
public bool MoveNext() { if (completed) { TryReturn(); return(false); } if (cancellationToken.IsCancellationRequested) { completed = true; core.TrySetCanceled(cancellationToken); return(false); } if (progress != null && handle.IsValid()) { progress.Report(handle.PercentComplete); } return(true); }
/// <inheritdoc/> public AsyncOperationHandle <SceneInstance> ProvideScene(ResourceManager resourceManager, IResourceLocation location, LoadSceneMode loadMode, bool activateOnLoad, int priority) { AsyncOperationHandle <IList <AsyncOperationHandle> > depOp = default(AsyncOperationHandle <IList <AsyncOperationHandle> >); if (location.HasDependencies) { depOp = resourceManager.ProvideResourceGroupCached(location.Dependencies, location.DependencyHashCode, typeof(IAssetBundleResource), null); } SceneOp op = new SceneOp(resourceManager); op.Init(location, loadMode, activateOnLoad, priority, depOp); var handle = resourceManager.StartOperation <SceneInstance>(op, depOp); if (depOp.IsValid()) { depOp.Release(); } return(handle); }
public IEnumerator AddressablesImpl_DownloadDependenciesAsync_CantDownloadWhenGetResourceLocFailsAutoReleasesObjectList() { // Setup yield return(Init()); var deps = new List <object>(); var provideHandle = new ProvideHandle(m_Addressables.ResourceManager, new ProviderOperation <AssetBundleResource>()); provideHandle.GetDependencies(deps); bool autoRelease = true; AsyncOperationHandle op = new AsyncOperationHandle(); using (new IgnoreFailingLogMessage()) { op = m_Addressables.DownloadDependenciesAsync(deps, Addressables.MergeMode.Intersection, autoRelease); yield return(op); } // Test Assert.IsFalse(op.IsValid()); }
void Update() { switch (curState) { case EUpdateState.CheckUpdateCatalogs: { checkUpdateTime += Time.deltaTime; if (checkUpdateTime > CHECKTIMEMAX) { SetState(EUpdateState.AfterCheckCatalogs); StopAllCoroutines(); DownComplete(false); } else if (checkHandle.IsValid()) { OnDownProgress(checkHandle.PercentComplete); } break; } case EUpdateState.StartUpdateCatalogs: { OnDownProgress(catelogHandle.PercentComplete); break; } case EUpdateState.StartUpdateData: { if (catelogHandle.IsValid()) { OnDownProgress(downloadHandle.PercentComplete); } break; } default: break; } }
public static UniTask <T> ToUniTask <T>(this AsyncOperationHandle <T> handle, IProgress <float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) { return(UniTask.FromCanceled <T>(cancellationToken)); } if (!handle.IsValid()) { throw new Exception("Attempting to use an invalid operation handle"); } if (handle.IsDone) { if (handle.Status == AsyncOperationStatus.Failed) { return(UniTask.FromException <T>(handle.OperationException)); } return(UniTask.FromResult(handle.Result)); } return(new UniTask <T>(AsyncOperationHandleConfiguredSource <T> .Create(handle, timing, progress, cancellationToken, out var token), token)); }
public void AsyncOperationHandle_Acquire_IncrementsRefCount() { int expectedCount = 2; var op = new FakeTypedOperation(); AsyncOperationHandle <GameObject> typedHandle = new AsyncOperationHandle <GameObject>(op); var copyTyped = typedHandle.Acquire(); Assert.True(copyTyped.IsValid()); Assert.True(typedHandle.IsValid()); int actualCount = DestructiveGetRefCount(typedHandle); Assert.AreEqual(expectedCount, actualCount); op = new FakeTypedOperation(); AsyncOperationHandle typelessHandle = new AsyncOperationHandle(op); var copyTypeless = typelessHandle.Acquire(); Assert.True(copyTypeless.IsValid()); Assert.True(typelessHandle.IsValid()); actualCount = DestructiveGetRefCount(typelessHandle); Assert.AreEqual(expectedCount, actualCount); }
public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress <float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) { return(UniTask.FromCanceled(cancellationToken)); } if (!handle.IsValid()) { // autoReleaseHandle:true handle is invalid(immediately internal handle == null) so return completed. return(UniTask.CompletedTask); } if (handle.IsDone) { if (handle.Status == AsyncOperationStatus.Failed) { return(UniTask.FromException(handle.OperationException)); } return(UniTask.CompletedTask); } return(new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, out var token), token)); }
private void UpdateSpriteSheet() { if (mResourceRequest != null) { if (mResourceRequest.isDone) { if (mResourceRequest.asset == null) { spriteSheetAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); } else { var spritesTextureOriginal = (Texture2D)mResourceRequest.asset; spriteSheetAsset.progress = 1; FinalizeTexture(spritesTextureOriginal); } } else { spriteSheetAsset.progress = (Mathf.Clamp01(mResourceRequest.progress) * (1.0f - INFO_LOAD_WORTH)) + INFO_LOAD_WORTH; } } else if (mWebRequest != null) { try { if (mWebRequest.isDone) { #if UNITY_2020_1_OR_NEWER if (mWebRequest.result == UnityWebRequest.Result.ConnectionError) #else if (mWebRequest.isNetworkError) #endif { spriteSheetAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NetworkError); } #if UNITY_2020_1_OR_NEWER else if (mWebRequest.result == UnityWebRequest.Result.ProtocolError) #else else if (mWebRequest.isHttpError) #endif { // Start with generic "ServerError" for all HTTP errors var resultError = RB.Result.ServerError; // Assign specific code for common HTTP errors switch (mWebRequest.responseCode) { case 400: resultError = RB.Result.BadParam; break; case 403: resultError = RB.Result.NoPermission; break; case 404: resultError = RB.Result.NotFound; break; case 500: resultError = RB.Result.ServerError; break; } spriteSheetAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, resultError); } else { var spritesTextureOriginal = DownloadHandlerTexture.GetContent(mWebRequest); spriteSheetAsset.progress = 1; FinalizeTexture(spritesTextureOriginal); } } else { spriteSheetAsset.progress = (Mathf.Clamp01(mWebRequest.downloadProgress) * (1.0f - INFO_LOAD_WORTH)) + INFO_LOAD_WORTH; } } catch (Exception) { spriteSheetAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); } } #if ADDRESSABLES_PACKAGE_AVAILABLE else if (mAddressableRequestTexture.IsValid()) { if (mAddressableRequestTexture.Status == AsyncOperationStatus.Failed) { // Can't really figure out failure reason Addressables.Release(mAddressableRequestTexture); spriteSheetAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); return; } else if (mAddressableRequestTexture.Status == AsyncOperationStatus.Succeeded) { spriteSheetAsset.progress = 1; var spritesTextureOriginal = mAddressableRequestTexture.Result; FinalizeTexture(spritesTextureOriginal); // Safe to realease here, FinalizeTexture makes a copy of the asset into a RenderTexture Addressables.Release(mAddressableRequestTexture); spriteSheetAsset.SetErrorStatus(RB.AssetStatus.Ready, RB.Result.Success); } if (!mAddressableRequestTexture.IsDone) { spriteSheetAsset.progress = mAddressableRequestTexture.PercentComplete * (1.0f - INFO_LOAD_WORTH) + INFO_LOAD_WORTH; } } #endif }
private void UpdateMapInfo() { byte[] loadedBytes = null; if (mResourceRequest != null) { if (mResourceRequest.isDone) { if (mResourceRequest.asset == null) { mapAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); } else { var textAsset = (TextAsset)mResourceRequest.asset; loadedBytes = textAsset.bytes; } } } else if (mWebRequest != null) { try { if (mWebRequest.isDone) { #if UNITY_2020_1_OR_NEWER if (mWebRequest.result == UnityWebRequest.Result.ConnectionError) #else if (mWebRequest.isNetworkError) #endif { mapAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NetworkError); } #if UNITY_2020_1_OR_NEWER else if (mWebRequest.result == UnityWebRequest.Result.ProtocolError) #else else if (mWebRequest.isHttpError) #endif { // Start with generic "ServerError" for all HTTP errors var resultError = RB.Result.ServerError; // Assign specific code for common HTTP errors switch (mWebRequest.responseCode) { case 400: resultError = RB.Result.BadParam; break; case 403: resultError = RB.Result.NoPermission; break; case 404: resultError = RB.Result.NotFound; break; case 500: resultError = RB.Result.ServerError; break; } mapAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, resultError); } else { loadedBytes = mWebRequest.downloadHandler.data; } } else { mapAsset.progress = Mathf.Clamp01(mWebRequest.downloadProgress); } } catch (Exception) { mapAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); } } #if ADDRESSABLES_PACKAGE_AVAILABLE else if (mAddressableRequest.IsValid()) { if (mAddressableRequest.Status == AsyncOperationStatus.Failed) { // Can't really figure out failure reason Addressables.Release(mAddressableRequest); mapAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); return; } else if (mAddressableRequest.Status == AsyncOperationStatus.Succeeded) { var textAsset = mAddressableRequest.Result; if (textAsset != null) { loadedBytes = textAsset.bytes; // Don't release yet, wait for loadedBytes to be processed } else { // Can't really figure out failure reason Addressables.Release(mAddressableRequest); mapAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); } } if (!mAddressableRequest.IsDone) { mapAsset.progress = mAddressableRequest.PercentComplete; return; } } #endif if (loadedBytes != null) { if (FinalizeMapInfo(loadedBytes)) { mapAsset.progress = 1; mapAsset.InternalSetErrorStatus(RB.AssetStatus.Ready, RB.Result.Success); } else { mapAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.BadFormat); } #if ADDRESSABLES_PACKAGE_AVAILABLE if (mAddressableRequest.IsValid()) { Addressables.Release(mAddressableRequest); } #endif } }
/// <summary> /// Update asynchronous loading /// </summary> public void Update() { // If not loading then there is nothing to update if (audioAsset == null || audioAsset.status != RB.AssetStatus.Loading) { return; } if (mResourceRequest != null) { if (mResourceRequest.isDone) { if (mResourceRequest.asset == null) { audioAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); } else { audioAsset.audioClip = (AudioClip)mResourceRequest.asset; audioAsset.progress = 1; audioAsset.InternalSetErrorStatus(RB.AssetStatus.Ready, RB.Result.Success); } } else { audioAsset.progress = mResourceRequest.progress; } } else if (mWebRequest != null) { try { if (mWebRequest.isDone) { #if UNITY_2020_1_OR_NEWER if (mWebRequest.result == UnityWebRequest.Result.ConnectionError) #else if (mWebRequest.isNetworkError) #endif { audioAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NetworkError); } #if UNITY_2020_1_OR_NEWER else if (mWebRequest.result == UnityWebRequest.Result.ProtocolError) #else else if (mWebRequest.isHttpError) #endif { // Start with generic "ServerError" for all HTTP errors var resultError = RB.Result.ServerError; // Assign specific code for common HTTP errors switch (mWebRequest.responseCode) { case 400: resultError = RB.Result.BadParam; break; case 403: resultError = RB.Result.NoPermission; break; case 404: resultError = RB.Result.NotFound; break; case 500: resultError = RB.Result.ServerError; break; } audioAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, resultError); } else { audioAsset.progress = 1; audioAsset.audioClip = DownloadHandlerAudioClip.GetContent(mWebRequest); audioAsset.InternalSetErrorStatus(RB.AssetStatus.Ready, RB.Result.Success); } } else { audioAsset.progress = Mathf.Clamp01(mWebRequest.downloadProgress); } } catch (Exception) { audioAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); } } #if ADDRESSABLES_PACKAGE_AVAILABLE else if (mAddressableRequest.IsValid()) { if (mAddressableRequest.Status == AsyncOperationStatus.Failed) { // Can't really figure out failure reason Addressables.Release(mAddressableRequest); audioAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined); return; } else if (mAddressableRequest.Status == AsyncOperationStatus.Succeeded) { audioAsset.progress = 1; audioAsset.audioClip = mAddressableRequest.Result; audioAsset.addressableHandle = mAddressableRequest; audioAsset.SetErrorStatus(RB.AssetStatus.Ready, RB.Result.Success); return; } if (!mAddressableRequest.IsDone) { audioAsset.progress = mAddressableRequest.PercentComplete; return; } } #endif }
/// <summary> /// Returns the state of the internal operation. /// </summary> /// <returns>True if the operation is valid.</returns> public bool IsValid() { return(m_Operation.IsValid()); }