Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to retrieve a scriptable object while releasing the handle?

Discussion in 'Addressables' started by gabrimo, May 19, 2023.

  1. gabrimo

    gabrimo

    Joined:
    May 2, 2015
    Posts:
    86
    Hello, I would like to know if it's possible to released a bunch of scriptable objects, select a random member of the result and release the remaining items while keep the randomly chosen item in memory.

    Every attempt so far causes NullReferenceException, so I'm being forced to retain the whole result in order to make my code work. Here's my current implementation, which is working:

    Code (CSharp):
    1. void LoadActivePattern(KeyValuePair<JerseyStyle,ColorBias> graphicPair)
    2.     {
    3.         var searchLabelsByJerseyStyle = new List<List<string>>();
    4.         var styleIndex = (int)graphicPair.Key;
    5.  
    6.         var labelsHeader = new List<string>
    7.         {
    8.             "Torso pattern",
    9.             styleIndex.ToString(),
    10.             activeTradition.ToString(),
    11.             graphicPair.Value.ToString()
    12.         };
    13.  
    14.         Addressables.LoadAssetsAsync<JerseyPattern>(labelsHeader, loadedHeader => { },
    15.         Addressables.MergeMode.Intersection).Completed += async dataAsyncHandle =>
    16.         {
    17.             if (dataAsyncHandle.IsDone)
    18.             {
    19.                 eligiblePatterns = dataAsyncHandle.Result.ToList();
    20.                 activePattern = ListUtilities.RandomItemOf(eligiblePatterns);
    21.                 onLoad(activePattern);
    22.                 //Addressables.Release(dataAsyncHandle);
    23.                 //If I release the handle, "activePattern" gets null...
    24.                 OnReadyRaiser();
    25.             }
    26.             else
    27.                 await dataAsyncHandle.Task;
    28.         };
    29.     }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    I don't think you can, at least not in the way you're doing it.
    LoadAssetsAsync
    returns one handle for all the loaded assets. Releasing the handle releases everything.

    You would have to load them all individually to have individual handles for each one.
     
  3. gabrimo

    gabrimo

    Joined:
    May 2, 2015
    Posts:
    86
    Yeah, the thing is, I would like to instantiate/create a new scriptable object instance and set it with the "activePattern" variable before releasing the handle. But all attempts on that regard failed too...