// Copyright (c) Alexander Bogarsukov.
// Licensed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace UnityFx.Tasks
{
///
/// Extensions of and related.
///
public static class AssetBundleExtensions
{
#region interface
///
/// Loads a from an asset bundle.
///
/// The source asset bundle.
/// The scene load mode.
/// Name of the scene to load or to load the any scene.
/// Returns the instance that can be used to track the operation state.
///
public static Task LoadSceneTaskAsync(this AssetBundle assetBundle, LoadSceneMode loadMode, string sceneName = null)
{
return LoadSceneTaskAsync(assetBundle, loadMode, sceneName, CancellationToken.None);
}
///
/// Loads a from an asset bundle.
///
/// The source asset bundle.
/// The scene load mode.
/// Name of the scene to load or to load the any scene.
/// A token that can be used to cancel the operation.
/// Returns the instance that can be used to track the operation state.
///
public static Task LoadSceneTaskAsync(this AssetBundle assetBundle, LoadSceneMode loadMode, string sceneName, CancellationToken cancellationToken)
{
if (!assetBundle.isStreamedSceneAssetBundle)
{
throw new InvalidOperationException();
}
if (string.IsNullOrEmpty(sceneName))
{
var scenePaths = assetBundle.GetAllScenePaths();
if (scenePaths != null && scenePaths.Length > 0 && !string.IsNullOrEmpty(scenePaths[0]))
{
sceneName = Path.GetFileNameWithoutExtension(scenePaths[0]);
}
if (string.IsNullOrEmpty(sceneName))
{
throw new UnityAssetLoadException("The asset bundle does not contain scenes.", null, typeof(Scene));
}
}
return TaskUtility.LoadSceneAsync(sceneName, loadMode, cancellationToken);
}
///
/// Loads an asset from an .
///
/// Type of the asset to load.
/// The source asset bundle.
/// Name of the asset to load.
/// Returns the instance that can be used to track the operation state.
///
public static Task LoadAssetTaskAsync(this AssetBundle assetBundle, string name) where T : UnityEngine.Object
{
return LoadAssetTaskAsync(assetBundle, name, CancellationToken.None);
}
///
/// Loads an asset from an .
///
/// Type of the asset to load.
/// The source asset bundle.
/// Name of the asset to load.
/// A token that can be used to cancel the operation.
/// Returns the instance that can be used to track the operation state.
///
public static Task LoadAssetTaskAsync(this AssetBundle assetBundle, string name, CancellationToken cancellationToken) where T : UnityEngine.Object
{
return assetBundle.LoadAssetAsync(name, typeof(T)).ToTask(cancellationToken);
}
///
/// Loads all assets of the specified type from an .
///
/// Type of the assets to load.
/// The source asset bundle.
/// Returns the instance that can be used to track the operation state.
///
public static Task LoadAllAssetsTaskAsync(this AssetBundle assetBundle) where T : UnityEngine.Object
{
return LoadAllAssetsTaskAsync(assetBundle, CancellationToken.None);
}
///
/// Loads all assets of the specified type from an .
///
/// Type of the assets to load.
/// The source asset bundle.
/// A token that can be used to cancel the operation.
/// Returns the instance that can be used to track the operation state.
///
public static Task LoadAllAssetsTaskAsync(this AssetBundle assetBundle, CancellationToken cancellationToken) where T : UnityEngine.Object
{
return assetBundle.LoadAllAssetsAsync(typeof(T)).ToTask(cancellationToken);
}
#endregion
#region implementation
#endregion
}
}