void Snippet() { #region doc_ConvertTypes // Load asset using typed handle: AsyncOperationHandle <Texture2D> textureHandle = Addressables.LoadAssetAsync <Texture2D>("mytexture"); // Convert the AsyncOperationHandle<Texture2D> to an AsyncOperationHandle: AsyncOperationHandle nonGenericHandle = textureHandle; // Convert the AsyncOperationHandle to an AsyncOperationHandle<Texture2D>: AsyncOperationHandle <Texture2D> textureHandle2 = nonGenericHandle.Convert <Texture2D>(); // This will throw and exception because Texture2D is required: AsyncOperationHandle <Texture> textureHandle3 = nonGenericHandle.Convert <Texture>(); #endregion }
public static async Task <T> LoadOrGetComponentAsync <T>(this AssetReference assetReference) where T : Component { AsyncOperationHandle op = assetReference.OperationHandle; if (op.IsValid()) { AsyncOperationHandle <GameObject> handle = op.Convert <GameObject>(); while (handle.IsDone == false) { await Task.Yield(); } if (handle.Result == null) { return(null); } return(handle.Result.GetComponent <T>()); } GameObject go = await assetReference.LoadAssetAsync <GameObject>().Task; if (go == null) { return(null); } return(go.GetComponent <T>()); }
public IEnumerator AddressablesImpl_DownloadDependenciesAsync_CanDownloadDependenciesWithChainFromObjectList() { // Setup yield return(Init()); if (TypeName == "BuildScriptFastMode" || TypeName == "BuildScriptVirtualMode") { Assert.Ignore($"Skipping test {nameof(AddressablesImpl_DownloadDependenciesAsync_CanDownloadDependenciesWithChainFromObjectList)} for {TypeName}"); } List <object> deps = new List <object>(); deps.Add(AddressablesTestUtility.GetPrefabLabel("BASE")); m_Addressables.hasStartedInitialization = false; AsyncOperationHandle op = m_Addressables.DownloadDependenciesAsync(deps, Addressables.MergeMode.Intersection, false); yield return(op); m_Addressables.hasStartedInitialization = true; // Test var wrapOp = op.Convert <IList <IAssetBundleResource> >(); AssertDownloadDependencyBundlesAreValid(wrapOp); // Cleanup op.Release(); }
public IEnumerator AddressablesImpl_DownloadDependenciesAsync_CanDoWithChainOpHandle() { // Setup yield return(Init()); if (TypeName == "BuildScriptFastMode" || TypeName == "BuildScriptVirtualMode") { Assert.Ignore($"Skipping test {nameof(AddressablesImpl_DownloadDependenciesAsync_CanDoWithChainOpHandle)} for {TypeName}"); } IList <IResourceLocation> locations; var ret = m_Addressables.GetResourceLocations(new object[] { "prefabs_evenBASE" }, typeof(GameObject), Addressables.MergeMode.Intersection, out locations); Assert.IsTrue(ret); m_Addressables.hasStartedInitialization = false; AsyncOperationHandle op = m_Addressables.DownloadDependenciesAsync(locations, false); m_Addressables.hasStartedInitialization = true; yield return(op); // Test var wrapOp = op.Convert <IList <IAssetBundleResource> >(); AssertDownloadDependencyBundlesAreValid(wrapOp); // Cleanup op.Release(); }
public IEnumerator AddressablesImpl_DownloadDependenciesAsync_CanDoWithChainKey() { // Setup if (TypeName == "BuildScriptFastMode" || TypeName == "BuildScriptVirtualMode") { Assert.Ignore($"Skipping test {nameof(AddressablesImpl_DownloadDependenciesAsync_CanDoWithChainKey)} for {TypeName}"); } yield return(Init()); string label = AddressablesTestUtility.GetPrefabLabel("BASE"); m_Addressables.hasStartedInitialization = false; AsyncOperationHandle op = m_Addressables.DownloadDependenciesAsync(label, false); m_Addressables.hasStartedInitialization = true; yield return(op); // Test var wrapOp = op.Convert <IList <IAssetBundleResource> >(); AssertDownloadDependencyBundlesAreValid(wrapOp); // Cleanup op.Release(); }
public void AsyncOperationHandle_ConvertToTyped_WithValidOpSucceeds() { var op = new FakeTypedOperation(); AsyncOperationHandle handle = new AsyncOperationHandle(op); AsyncOperationHandle <GameObject> typedHandle = handle.Convert <GameObject>(); Assert.True(handle.IsValid()); Assert.True(typedHandle.IsValid()); }
public void AsyncOperationHandle_ConvertToTyped_WithInvalidOpThrows() { var op = new FakeTypedOperation(); AsyncOperationHandle handle = new AsyncOperationHandle(op); AsyncOperationHandle handle2 = new AsyncOperationHandle(op); handle2.Release(); Assert.Throws <Exception>(() => { handle.Convert <GameObject>(); }); }
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); }
/// <summary> /// Load the <typeparamref name="TObject"/> at the specified <paramref name="location"/>. /// </summary> /// <returns>An async operation.</returns> /// <param name="location">Location to load.</param> /// <typeparam name="TObject">Object type to load.</typeparam> public AsyncOperationHandle <TObject> ProvideResource <TObject>(IResourceLocation location) { AsyncOperationHandle handle = ProvideResource(location, typeof(TObject)); return(handle.Convert <TObject>()); }
public AsyncOperationHandle <SceneInstance> UnloadSceneAsync(AsyncOperationHandle handle, bool autoReleaseHandle = true) { return(UnloadSceneAsync(handle.Convert <SceneInstance>(), autoReleaseHandle)); }