Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement search by name #5

Open
wayheart opened this issue Mar 28, 2024 · 4 comments
Open

Implement search by name #5

wayheart opened this issue Mar 28, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@wayheart
Copy link

I would like to see the implementation of search in addressable by the name of the save and receive. Thanks for you!

@dre0dru
Copy link
Owner

dre0dru commented Apr 13, 2024

Can you please elaborate more on what do you mean? Possibly with examples. You can already search addressables by their addressable name in editor and during asset loading, so you probably mean something else. Also what do you mean by "save and receivce"?

@skyp1nus
Copy link

skyp1nus commented Aug 2, 2024

Sorry for the long answer, this is my main account, I meant that it would be more convenient to pass the file name instead of ReferenceT, and do the search through the file name too

@skyp1nus
Copy link

skyp1nus commented Aug 2, 2024

This is a very good idea when you have a bunch of pictures and you can't transfer all the ReferenceTs to them like (1000 pieces), it will be convenient to load these pictures by name, for example: sprite1...1000; using a loop
for(int i = 0; i < 1000; i++) { await loader.PreloadAssetAsync($"Sprite{i}"); }

@skyp1nus
Copy link

skyp1nus commented Aug 2, 2024

Something like that

using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using Dre0Dru.AddressableAssets.Loaders;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

namespace Dre0Dru.AddressableAssets.Loader
{
    public class AssetsNameLoader<TAsset>  : IAssetsLoader<string, TAsset> where TAsset : Object
    {
        private readonly Dictionary<int, AsyncOperationHandle<TAsset>> _operationHandles =
            new Dictionary<int, AsyncOperationHandle<TAsset>>();


        public UniTask PreloadAssetAsync(string name) => LoadAssetAsync(name);

        public async UniTask<TAsset> LoadAssetAsync(string name)
        {
            try
            {
                AsyncOperationHandle<TAsset> handle = Addressables.LoadAssetAsync<TAsset>(name);

                await handle.ToUniTask();

                if (handle.Status == AsyncOperationStatus.Succeeded)
                {
                    if (!_operationHandles.TryAdd(name.GetHashCode(), handle))
                    {
                        Addressables.Release(_operationHandles[name.GetHashCode()]); // Розвантажити попередню операцію
                        _operationHandles[name.GetHashCode()] = handle; // Оновити дескриптор операції
                    }

                    return handle.Result;
                }
                else
                {
                    Debug.LogError($"Не вдалося завантажити ресурс: {name}");
                }
            }
            catch (System.Exception e)
            {
                Debug.LogError($"Сталася помилка під час завантаження ресурсу: {e.Message}");
            }

            return null;
        }

        public bool IsAssetLoaded(string name)
        {
            if (_operationHandles.TryGetValue(name.GetHashCode(), out AsyncOperationHandle<TAsset> handle))
            {
                return handle.Status == AsyncOperationStatus.Succeeded;
            }

            return false;
        }

        public TAsset GetAsset(string name)
        {
            return _operationHandles[name.GetHashCode()].Result;
        }

        public bool TryGetAsset(string name, out TAsset asset)
        {
            asset = default;

            if (IsAssetLoaded(name))
            {
                asset = GetAsset(name);

                return asset;
            }

            return false;
        }

        public void UnloadAsset(string name)
        {
            int hashKey = name.GetHashCode();

            if (_operationHandles.TryGetValue(hashKey, out AsyncOperationHandle<TAsset> handle))
            {
                Addressables.Release(handle);
                _operationHandles.Remove(hashKey);
            }
        }

        public void UnloadAllAssets()
        {
            foreach (var handle in _operationHandles.Values)
            {
                Addressables.Release(handle);
            }

            _operationHandles.Clear();
        }
    }
}

@dre0dru dre0dru added the enhancement New feature or request label Aug 4, 2024
@dre0dru dre0dru self-assigned this Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants