- Home /
Pooling Efficiency Transform vs gameObject
I've searched a bit but I'm not really sure witch one takes less memory Transform OR GameObject.
Pooling for Transform:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public struct Pooling {
public GameObject Prefab;
private Queue<Transform> QueueGO;
// private Transform Restore;
// OPTIONS
public int MinInaCount;
/****** START Constructors ******/
public Pooling (GameObject PrefabGO){
Prefab = PrefabGO;
QueueGO = new Queue<Transform>();
MinInaCount = 1;
// just a randome so structure won't get in a way
// Restore = ;
}
public Pooling (GameObject PrefabGO, int MinimumInactiveCountInPool){
Prefab = PrefabGO;
QueueGO = new Queue<Transform>();
MinInaCount = MinimumInactiveCountInPool;
// just a randome so structure won't get in a way
// Restore = PrefabGO;
}
/****** END Constructors ******/
public Transform CreateF(){
if (QueueGO.Count < MinInaCount){
return (Transform)UnityEngine.Object.Instantiate (Prefab);
}
else {
return ActivateF();
}
}
// we activate GO
private Transform ActivateF (){
Transform Restore = QueueGO.Dequeue();
Restore.gameObject.SetActive(true);
return Restore;
}
// we deactivate GO
public void DeactivateF (Transform DeactivateMe) {
QueueGO.Enqueue(DeactivateMe);
DeactivateMe.gameObject.SetActive(false);
}
}
Pooling with GameObject
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public struct Pooling {
public GameObject Prefab ;
private Queue<GameObject> QueueGO;
private GameObject Restore ;
// OPTIONS
public int MinInaCount;
/****** START Constructors ******/
public Pooling (GameObject PrefabGO){
Prefab = PrefabGO ;
QueueGO = new Queue<GameObject>() ;
MinInaCount = 1 ;
// just a randome so structure won't get in a way
Restore = PrefabGO;
}
public Pooling (GameObject PrefabGO, int MinimumInactiveCountInPool){
Prefab = PrefabGO ;
QueueGO = new Queue<GameObject>() ;
MinInaCount = MinimumInactiveCountInPool ;
// just a randome so structure won't get in a way
Restore = PrefabGO;
}
/****** END Constructors ******/
public GameObject CreateF(){
if (QueueGO.Count < MinInaCount){
return (GameObject)UnityEngine.Object.Instantiate (Prefab);
}
else {
return ActivateF();
}
}
// we activate GO
private GameObject ActivateF (){
Restore = QueueGO.Dequeue();
Restore.SetActive(true);
return Restore;
}
// we deactivate GO
public void DeactivateF (GameObject DeactivateMe) {
QueueGO.Enqueue(DeactivateMe);
DeactivateMe.SetActive(false);
}
}
witch one is more efficient or it doesn't really matter?
does GameObject take in memory all the components or just a tiny bit to what everything is attached and that Transform takes in memory all Position, Rotation, ...
There is no different in the amount of memory. Both are stored by reference. But I know of one difference that may or may not have an impact on your app. When you access the transform from a game object, under the hood there is a GetComponent() call. So if in using your pool you need to access the transform, it will be a bit more efficient to store the transform. There are other on this list with more experience with pooling, so I'm going to leave this as a comment and hopefully they can answer or comment as well.
but do you mean this is same as destroying the GO for garbage colector?
that I need to make a list and add to it, make them null if I take it out, ... so GC doesn't collect them
I don't understand your comment back. I'm not suggesting you get rid of your pools. So you need a new game object, and you get it from the pool. What is the first thing most apps do when they pull a game object from the pool? They set the position and or rotation. So you will have a line of code like:
retrievedGameObject.transform.position = somePosition;
When I access the transform through the game object like this, what is really happening is:
referenceGameObject.GetComponent<Transform>().position = somePosition;
So If I retrieved 100 game objects a frame and place them, I have 100 GetComponent() calls under the hood. These calls can be avoided if my pool uses Transforms. Of course if you only pulling a few items from the pool, or if you don't access the Transform, there is no win in using Transforms in the pool.
I would think the case of internal GetComponent does not apply here. The process is when accessing the transform via the component reference like:
transform.position = position;
This calls for GetComponent internally making it a little slower that expected
but in the case here, the Transform is stored in the queue meaning it is already the result of GetComponent that gets there so when using the queue, you are alredy in a cache version of it.
Choosing GameObject over Transform is just a matter of what you are using, if you do not need anything from GameObject and only use from Transform then use Transform. Using gameObject.transform would get a dereferenciation and the GetComponent problem since this is not a cache version of the Transform.
Am I being clear...? P.S: I realize this is long gone...
I guess I picking up on this and am confused:
I would think the case of internal GetComponent does not apply here.
He posted two version of his pooling code. One with GameObjects the other with Transforms and was asking if one was better than the other...more memory was his original question.