Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sdgd · Oct 19, 2013 at 06:02 PM · c#efficiencypoolingadvice

Pooling Efficiency Transform vs gameObject

I've searched a bit but I'm not really sure witch one takes less memory Transform OR GameObject.

Unity Answers

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, ...

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 19, 2013 at 08:56 PM 1
Share

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.

avatar image sdgd · Oct 19, 2013 at 09:30 PM 0
Share

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

avatar image robertbu · Oct 19, 2013 at 10:02 PM 1
Share

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.

avatar image fafase · Oct 27, 2013 at 12:30 AM 1
Share

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...

avatar image robertbu · Oct 27, 2013 at 01:31 AM 1
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject Pool heaving difficulties getting a pool at all trying to find different aproach 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

how to use Enum? C# I'm keep getting Unexpected symbol `enum' 2 Answers

[C#] PlayerRelativeControl conversion errors 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges