PlantList
This commit is contained in:
1 parent
ba49ef5015
commit
7c207ef5b9
44 files changed
+2795
-1147
No files matched your search
@@ -1,18 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
namespace Code.Calendar.Constants
|
||||
{
|
||||
// Написать кодрайтер, который будет находить конфиги и добавлять их сюда пока написано само
|
||||
public class ConstantImagesService
|
||||
public interface IConstantImagesService
|
||||
{
|
||||
UniTaskVoid LoadImage(ConstantImageType imageType, Action<Sprite> callback);
|
||||
void ReleaseImage(ConstantImageType imageType);
|
||||
}
|
||||
|
||||
public class ConstantImagesService : IConstantImagesService
|
||||
{
|
||||
private readonly EventBus _eventBus;
|
||||
|
||||
public List<ConstantImageConfig> Configs { get; private set; }
|
||||
public Dictionary<ConstantImageType, ConstantImageConfig> Configs { get; private set; }
|
||||
|
||||
public ConstantImagesService(EventBus eventBus, IConfigLocator configLocator)
|
||||
{
|
||||
@@ -20,10 +29,10 @@ namespace Code.Calendar.Constants
|
||||
|
||||
var label = configLocator.TryGetManyConfigAssetReferences<ConstantImageConfig>();
|
||||
|
||||
LoadImage(label).Forget();
|
||||
LoadImages(label).Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadImage(string imagesLabel)
|
||||
private async UniTaskVoid LoadImages(string imagesLabel)
|
||||
{
|
||||
if ((await Addressables.LoadResourceLocationsAsync(imagesLabel).Task).Count != 0)
|
||||
{
|
||||
@@ -32,13 +41,29 @@ namespace Code.Calendar.Constants
|
||||
|
||||
await images.ToUniTask();
|
||||
|
||||
Configs = images.Result.ToList();
|
||||
Configs = images.Result.ToDictionary(x=> x.imageType);
|
||||
}
|
||||
|
||||
_eventBus.RaiseEvent<IConstantImagesServiceHandle>(x=> x.ImagesLoaded());
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary<ConstantImageType, AsyncOperationHandle<Sprite>> _spritesHandles = new();
|
||||
public async UniTaskVoid LoadImage(ConstantImageType imageType, Action<Sprite> callback)
|
||||
{
|
||||
if(!_spritesHandles.ContainsKey(imageType))
|
||||
_spritesHandles.Add(imageType, Addressables.LoadAssetAsync<Sprite>(Configs[imageType].image));
|
||||
|
||||
await _spritesHandles[imageType].ToUniTask();
|
||||
|
||||
callback?.Invoke(_spritesHandles[imageType].Result);
|
||||
}
|
||||
|
||||
public void ReleaseImage(ConstantImageType imageType)
|
||||
{
|
||||
_spritesHandles[imageType].Release();
|
||||
_spritesHandles.Remove(imageType);
|
||||
}
|
||||
}
|
||||
public interface IConstantImagesServiceHandle : IGlobalSubscriber
|
||||
{
|
||||
void ImagesLoaded();
|
||||
|
||||
Reference in new issue
Block a user