Home Game Development unity – Profiling Loading and Unloading of Addressables

unity – Profiling Loading and Unloading of Addressables

0
unity – Profiling Loading and Unloading of Addressables

[ad_1]

I’ve lately arrange a small helper class to load and unload addressables, it accommodates the next strategies for that objective

    non-public IEnumerator LoadObjects<TObject>(IList<object> keys, Addressables.MergeMode mergeMode = Addressables.MergeMode.Union)
    {
            var areas = Addressables.LoadResourceLocationsAsync(keys, mergeMode, typeof(TObject));

            yield return areas;

            var asyncOperationHandles = new List<AsyncOperationHandle>(areas.Result.Count);
            
            foreach (var location in areas.Result)
            {
                    if (_allObjectAsyncOperationHandlesByLocationKey.ContainsKey(location.PrimaryKey))
                    {
                            proceed;
                    }
                    
                    var asyncOperationHandle = Addressables.LoadAssetAsync<TObject>(location);

                    asyncOperationHandle.Completed += obj =>
                    {
                            if (obj.Status == AsyncOperationStatus.Succeeded)
                            {
                                    _allObjectAsyncOperationHandlesByLocationKey.Add(location.PrimaryKey, obj);
                                    _allAddressableLocationKeysByObject.Add(obj.Result, location.PrimaryKey);

                                    if (obj.Result is GameObject gameObj) OnGameObjectLoaded?.Invoke(gameObj);
                            }
                            else
                            {
                                    // TODO: 
                            }
                    };
                    
                    asyncOperationHandles.Add(asyncOperationHandle);
            }

            yield return Addressables.ResourceManager.CreateGenericGroupOperation(asyncOperationHandles, true);
    }
    
    non-public IEnumerator UnloadObjects(List<object> objs)
    {
            var keys = new List<string>();
            foreach (var obj in objs)
            {
                    if (_allAddressableLocationKeysByObject.ContainsKey(obj))
                    {
                            keys.Add(_allAddressableLocationKeysByObject[obj]);
                    }
            }
            
            foreach (var key in keys)
            {
                    if (!_allObjectAsyncOperationHandlesByLocationKey.ContainsKey(key))
                    {
                            proceed;
                    }

                    var asyncOperationHandle = _allObjectAsyncOperationHandlesByLocationKey[key];

                    if (asyncOperationHandle.Result is GameObject gameObj) OnGameObjectUnloaded?.Invoke(gameObj);
                    
                    _allObjectAsyncOperationHandlesByLocationKey.Remove(key);
                    
                    _allAddressableLocationKeysByObject.Remove(asyncOperationHandle.Result);
                    
                    Addressables.Release(asyncOperationHandle);
            }

            yield return true; // Return true solely in order that this methodology might be queued as a coroutine (similar as loading)
    }

Now I’ve examined to load one thing and unload it similarly, I’ve proofed this with the reminiscence module of the profiler and noticed the reminiscence go up when loading and go down the identical quantity when unloading (in a construct).

But now I’ve coupled this technique with an object pool, i.e. as quickly as a sport object is loaded from an addressable I create a pool for it. The problem now’s that I load the useful resource, I create a pool (i.e. I create sport objects through the traditional instantiate utilizing the end result returned by the operation deal with), then when unloading I first delete the pool (f.e. destroying the instantiated sport objects) and lastly launch the operation deal with answerable for the useful resource. And this appears to form of work, however the reminiscence does not go down the identical quantity whereas unloading as once I loaded it, and I do not know the best way to debug this facet. I attempted with the addressable occasion view however I do not know what to do with its outcomes. I haven’t got a screenshot at hand proper now, however what I noticed was that the useful resource was loaded (I used to be capable of see the precise prefab), but it surely was the kid of a dependency, and once I unloaded the prefab, it disappeared from the occasion viewer, however the dependency remained with a ref rely of 1, which is likely to be the difficulty of the remaining quantity of reminiscence?

My query subsequently is, how can I appropriately decide if the loading and unloading of addressables is working (and why if not) when not counting on the instantiate async methodology utilizing the profiler and occasion viewer?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here