- Home /
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.
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
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);
}
}
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?
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.