UI elements can be Pooled?

I've got a notification script that instances UI notifications. Apparently works with ObjectPool.Instantiate / Destroy (not sure if is pooling), but if I add the prefabs into the Object Pool Script it doesn't work.
 
Hi,
I actually have a problem with pooling UI elements too. I tried this:
C#:
GameObject temp = ObjectPool.Instantiate(m_ButtonPrefab, transform);
Debug.Log(ObjectPool.InstantiatedWithPool(temp));
//returns false
I haven't digged anymore than that. I'll look into it more if I have some time
 
Ok I figured it out, at least for my mistake.

First of all I realised that I wasn't using the ObjectPool instantiate method... Because ObjectPool does not have a Instantiate(GameObject, Transform) method. But since ObjectPool inherits monobehavior it uses the Object.Instantiate(GameObject,Transform) method, which is the reason I did not get an error.

So to fix the snippet above you need to make sure you are using the correct methods (you have the choice between a few). The one I use is:

C#:
public static GameObject Instantiate(GameObject original, Vector3 position, Quaternion rotation, Transform parent)

Here is the new snippet:
Code:
GameObject temp = ObjectPool.Instantiate(m_ButtonPrefab, transform.position, transform.rotation, transform);
Debug.Log(ObjectPool.InstantiatedWithPool(temp));
//returns true

When removing the object I get a warning because the UI gameobject uses RectTransform. The warning says that I should use the SetParent method instead of assigning a parent using the "=" operator.



@Justin
I would just recommend you to do two little tweaks to the ObjectPool class.

1) Add the Instatiate.(GameObject original, Transform parent) override

Code:
public static GameObject Instantiate(GameObject original, Transform parent)
{
    return Instance.InstantiateInternal(original, parent.position, parent.rotation, parent);
}

2) Change all the parent assignments statements to "SetParent()" instead of "parent =".
Mainly in the DestroyLocal and ObjectFromPool method

Thank you
 
Top