I have a question about when to release a UI prefab after loading it with Addressables.
Is it safe to call Addressables.Release immediately after instantiate the prefab loaded in Addressables?
Or shouldn’t I call Addressables.Release until I destroy the instantiated GameObject?
(This prefab explicitly contains only .prefab, but implicitly expects to contain .png, .mat, etc.)
The simple fact is that when you instantiate, there will be no reference to the data loaded in the addressables.
So, I think it’s okay to release it immediately. But is this not correct, or is it a case by case basis?
For safety reasons, of course, I think it’s better to do Addressables.Release after destroying the instantiated game object.
The sample code is shown below.
GameObject m_prefab;
// Start the UI
void startUI()
{
StartCoroutine( loadPrefab() );
}
IEnumerator loadPrefab()
{
var request = Addressables.LoadAssetAsync<GameObject>( "UI_prefab" );
yield return request;
var tmp = request.Result as GameObject;
m_prefab = Instantiate(tmp) as GameObject;
m_prefab.SetActive(true);
// Are you sure you want to release it here?
/*
Addressables.Release( request );
*/
}
// End of UI
void endUI()
{
GameObject.Destroy( m_prefab );
// Or should I just Release it here?
/*
Addressables.Release( request );
*/
}