Last active
November 7, 2021 11:41
-
-
Save Cxyda/d710ef843bc2829f406b15111578437c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Simulation.Data; | |
using UnityEngine.AddressableAssets; | |
using UnityEngine.ResourceManagement.AsyncOperations; | |
namespace Game.AssetLoading | |
{ | |
public interface IAssetLoadService | |
{ | |
void LoadAsset<T>(AssetType assetType, EntityType entityType, Action<T> onLoadedCallback); | |
} | |
/// <summary> | |
/// This class handles finding and loading game assets asynchronously. | |
/// It uses Unity's <see cref="Addressables"/> for this. | |
/// It identifies the assets via <see cref="AssetType"/> and <see cref="EntityType"/> enums. | |
/// | |
/// When the assets have been loaded it invokes the onLoadedCallback callback. | |
/// </summary> | |
public class AssetLoadService : IAssetLoadService | |
{ | |
public void LoadAsset<T>(AssetType assetType, EntityType entityType, Action<T> onLoadedCallback) | |
{ | |
// Load the asset with the given address (key) and call OnLoaded when it's done | |
Addressables.LoadAssetAsync<T>($"{assetType.ToString()}/{entityType.ToString()}").Completed += OnLoaded; | |
void OnLoaded(AsyncOperationHandle<T> handle) | |
{ | |
// Invoke the callback and pass the asset back | |
onLoadedCallback.Invoke(handle.Result); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment