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 Ilkzz · May 27, 2014 at 12:27 AM · c#mobileoptimizationpooling

Optimising for mobile - Instantiate

Hi,

I am creating a game for iOS/Android and I seem to have some "lag" when its on the phone. Just making a simple endless game. My draw call and poly count/tris etc is all very low so I have no problem there.

Testing on a iPhone 5, the movement doesn't seem smooth. Xcode tells me the CPU levels, RAM and FPS are all good. Testing on a HTC Sensation XL, lag is seen at times

I have researched and found that I should not be using instantiate and destroy on mobile and also found that an alternative is pooling. They are all paid on the asset store, which I cannot afford at the moment. How do i go about doing this pooling thing? How do I store variables and reuse them again? I've only just got my head around instantiate so Im no techy.

 public GameObject rocks;
     
     // Use this for initialization
     void Start()
     {
         InvokeRepeating("CreateObstacle", 1.5f, 2f);
     }
     
     void CreateObstacle()
     {
         Instantiate(rocks);
     }

Ever so simple code to spawn the object I need. They are destroyed 6 seconds later.

Comment
Add comment · Show 1
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 Clet_ · May 27, 2014 at 12:38 AM 0
Share

There is A LOT of free pool implementation on any language you can possibly think of. There is free implementation of a pool even here on Unity Answers/forums.

Please do some research before posting

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lovrenc · May 27, 2014 at 12:33 AM

You could use something like that. Maybe some elements from other classes are missing, modify it for your need.

On mobile you really want to reuse elements. Every time garbage collector does his job, you get a slight delay. Here you only create instance if you are lacking them.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 /// <summary>
 /// This is an object factory. Instances of game objects are being reused so there is no garbage produced.
 /// </summary>
 public class ObjectFactory : MonoBehaviour {
 
     /// <summary>
     /// Reference to object that holds all the variations.
     /// </summary>
     public Transform variationsObject;
 
     /// <summary>
     /// Holds POSSIBLE! variations of game object.
     /// </summary>
     private List<GameObject> variations = new List<GameObject>();
 
     /// <summary>
     /// Holds available instances of gameObject.
     /// </summary>
     protected List<GameObject> holder = new List<GameObject>();
 
     protected List<GameObject> activeObjects = new List<GameObject>();
 
 
     
     protected void Start () {
         Debug.Log ("Started! " + variationsObject.childCount);
         for(int i = 0; i < variationsObject.childCount; i++) {
             variations.Add ((GameObject)GameObject.Instantiate(variationsObject.GetChild(i).gameObject));
         }
     }
 
     /// <summary>
     /// returns instance of game object.
     /// </summary>
     /// <returns>The object of index.</returns>
     /// <param name="index">Index.</param>
     private GameObject GetInstanceByIndex(int index) {
         var go = GetInstance ();
         go.GetComponent<SpriteRenderer>().sprite = variations[index].GetComponent<SpriteRenderer>().sprite;
         return go;
     }
 
     protected GameObject GetInstance() {
         var result = holder.Count > 0 ? holder.Pull() : (GameObject)GameObject.Instantiate(variations[Random.Range(0, variations.Count)]);
         result.SetActive(true);
         result.renderer.enabled = true;
         return result;
     }
 
     /// <summary>
     /// Creates and positions element on given position
     /// </summary>
     /// <param name="position">Position.</param>
     public virtual GameObject PositionOn(Vector2 position) {
         var obj = GetInstance();
         obj.transform.position = position;
         activeObjects.Push(obj);
         return obj;
     }
 
 
 
     /// <summary>
     /// Updates all active elements 
     /// </summary>
     public virtual void UpdateElements() {
         for(int i = 0; i < this.activeObjects.Count; i++) {
             var obj = activeObjects[i];
             obj.transform.Translate(Vector2.up * -1 * Config.Instance.gameSpeed * Time.deltaTime);
             
             if(!obj.renderer.enabled || obj.transform.position.y + obj.renderer.bounds.size.y < Config.Instance.bottomY)
                 ReleaseInstance(activeObjects[i]);
         }
     }
 
     /// <summary>
     /// Returns instance of GameObject back for reuse.
     /// </summary>
     /// <param name="gameObject">Game object.</param>
     public void ReleaseInstance(GameObject gameObject) {
         gameObject.SetActive(false);
         activeObjects.Remove(gameObject);
         holder.Push(gameObject);
     }
 }
Comment
Add comment · Show 2 · Share
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 Ilkzz · May 27, 2014 at 12:40 AM 0
Share

Hi thanks for the script. The script seems to be filled with errors. Are you able to describe the script a bit more so I can understand what is going on?

avatar image Lovrenc · May 27, 2014 at 12:47 AM 1
Share

Well, you are missing list extensions, config singleton etc, so obviously you'll get errors if you just dump it on your GameObject. I feel code is commented enough to give you general idea if you read it.

Also there is plenty of pooling examples on internet, not everything takes 5 seconds, so start reading.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Colliders vs Callbacks: Which is better for performance in my case? 0 Answers

Why is my camera motion so jittery? 3 Answers

Jump Force (rigid body velocity) is not equal in different mobile phones 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