Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.AddressableAssets;
- using UnityEngine.ResourceManagement.AsyncOperations;
- using System.Collections.Generic;
- [CreateAssetMenu(fileName = "SpriteAsset", menuName = "Assets/SpriteAsset")]
- public class SpriteAsset : ScriptableObject
- {
- public AssetReferenceSprite[] spriteReferences;
- }
- public class SpriteManager : MonoBehaviour
- {
- public SpriteAsset spriteAsset;
- private Dictionary<string, Sprite> spriteDictionary;
- void Start()
- {
- InitializeSpriteDictionary();
- }
- private void InitializeSpriteDictionary()
- {
- spriteDictionary = new Dictionary<string, Sprite>();
- foreach (AssetReferenceSprite spriteRef in spriteAsset.spriteReferences)
- {
- spriteRef.LoadAssetAsync<Sprite>().Completed += handle =>
- {
- if (handle.Status == AsyncOperationStatus.Succeeded)
- {
- Sprite loadedSprite = handle.Result;
- spriteDictionary.Add(loadedSprite.name, loadedSprite);
- }
- };
- }
- }
- public Sprite GetSprite(string id)
- {
- if (spriteDictionary.TryGetValue(id, out Sprite sprite))
- {
- return sprite;
- }
- else
- {
- Debug.LogWarning("Sprite with id " + id + " not found.");
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement