- Home /
Question by
NJ333 · Apr 11 at 09:41 AM ·
unity 2dassetbundle
Addressables Correctly release gameobject which has also another addressable component
Let's say I have Addressable prefab "itemReference". "itemReference" has a sprite component and the sprite itself is another addressable as well. If I want to Destroy itemReference what is a proper way to do it?
This is instantiate code of itemReference:
var spritesHandle = Addressables.LoadAssetsAsync<Sprite>(keys, (sprite) =>
{
itemReference.InstantiateAsync(layoutGroup).Completed += (handle) =>
{
var go = handle.Result;
go.transform.Find("Image").GetComponent<Image>().sprite = sprite;
go.transform.Find("Text").GetComponent<Text>().text = sprite.name;
};
}, Addressables.MergeMode.Union);
Then when I am done and want to release all itemReference gameObjects I am doing this:
foreach (Transform item in layoutGroup.transform)
{
Addressables.ReleaseInstance(item.gameObject);
}
but this doesn't clear the memory it only destroys the game objects from the scene.
If I first release sprite handle and then release itemReference gameObjects it clears the memory correctly. But I don't know if this is a correct way to do it.
Addressables.Release(spritesHandle);
foreach (Transform item in layoutGroup.transform)
{
Addressables.ReleaseInstance(item.gameObject);
}
Comment
Your answer