Hello Guest

Author Topic: Performance Question  (Read 5140 times)

appymedia

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Performance Question
« on: October 02, 2014, 09:10:07 am »
Hi Folks,

I'm really new to both Unity and 2D Toolkit and have started out on a first simple project to more or less replicate a PIXI benchmark here :-

http://www.goodboydigital.com/pixijs/bunnymark/

I have my scene setup, 2D Toolkit camera in place, a simple sprite collection made with one sprite (the wabbit :)), an empty game object which is responsible for instantiating a prefab I have made out of a simple 2D Toolkit sprite & C# script that controls the bunnies behavior, here's the script attached to the prefab :-

Code: [Select]
using UnityEngine;
using System.Collections;

public class myBunny : MonoBehaviour {

private float minX = 0.0f;
private float maxX = 9.6f;
private float minY = 0.0f;
private float maxY = 6.4f;
private float speedX = 0;
private float speedY = 0;

tk2dSprite mySprite;

public float gravity = 0.09f;

// Use this for initialization
void Start () {
mySprite = (tk2dSprite)gameObject.GetComponent ("tk2dSprite");
speedX = Random.Range (0.0f, 8.0f);
speedY = Random.Range (-4.0f, 1.5f);
transform.position = new Vector2 (0.0f, 6.4f - mySprite.GetUntrimmedBounds().size.y);
maxX -= mySprite.GetUntrimmedBounds().size.x;
}

// Update is called once per frame
void Update () {
speedY -= gravity;

transform.Translate (speedX * Time.deltaTime, (speedY * Time.deltaTime), 0);

if ((transform.position.x < minX) || transform.position.x > maxX) {
speedX *= -1;
transform.position = new Vector2(Mathf.Clamp(transform.position.x, minX, maxX),transform.position.y);
}

if (transform.position.y < minY) {
speedY = Random.Range (3.0f, 7.6f);
transform.position = new Vector2(transform.position.x, Mathf.Clamp(transform.position.y, minY, maxY));
}
}
}

...an heres the code in the simple script on the empty game object...

Code: [Select]
using UnityEngine;
using System.Collections;

public class myBunnyMarkController : MonoBehaviour {

// Starting number of bunnies to create
public int startBunnyCount = 2;

// Use this for initialization
void Start () {
for (int i = 1; i <= startBunnyCount; i++) {
Instantiate(Resources.Load("myPrefabs/myWabbitPreFab"));
}

((tk2dTextMesh)GameObject.FindGameObjectWithTag ("TextMesh").GetComponent("tk2dTextMesh")).text = "Number of bunnies : " + startBunnyCount.ToString();
((tk2dTextMesh)GameObject.FindGameObjectWithTag ("TextMesh").GetComponent("tk2dTextMesh")).Commit();
}

// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0)) {
for (int i = 1; i <= 50; i++) {
Instantiate(Resources.Load("myPrefabs/myWabbitPreFab"));
}
startBunnyCount += 50;

((tk2dTextMesh)GameObject.FindGameObjectWithTag ("TextMesh").GetComponent("tk2dTextMesh")).text = "Number of bunnies : " + startBunnyCount.ToString();
((tk2dTextMesh)GameObject.FindGameObjectWithTag ("TextMesh").GetComponent("tk2dTextMesh")).Commit();
}
}
}

The codes a bit messy in the empty game object 'controller' but its only fired when starting up or if the mouse is down, I wanted to come back to it once everything else was ok.  When I run this I hit about 5000 sprites onscreen in both the Unity player and a standalone build before things start to dip below 60fps, I was hoping for a lot lot more based upon the PIXI benchmark I got the idea from as I'm seeing 10,000's running through their HTML5 renderer, any ideas folks?

Happy to post up the project in full etc but I have 2D Toolkit in use and suspect it would have to go in the private board?

Thanks all :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Performance Question
« Reply #1 on: October 02, 2014, 11:10:09 am »
There is a overhead involved with gameobjects in unity. It gets really expensive once you go beyond a certain number. (about 10k last time I checked on iOS). Not straightforward to compare the 2, Unity does a metric ton more than that js engine.

appymedia

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Performance Question
« Reply #2 on: October 02, 2014, 11:07:44 pm »
Many thanks for the reply.

I did wonder if there was some sort of Unity overhead with the Game Object's but had hoped by not having things such as collider's and so on it would be minimal.  I know its an Apple vs Pears comparison but but I wanted to see what can be achieved.

I'd be interested to hear anyone's thoughts on optimizing what I've done code wise (particularly in the first block of code where there's the translation / positioning stuff running on each update).  Also, if changing the shader on the prefab might help?  If I'm honest I don't pretend to understand shaders just yet fully but am aware there might be a performance hit on certain ones.

Look forward to anyone's advice.  Thanks again.

appymedia

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Performance Question
« Reply #3 on: October 02, 2014, 11:10:02 pm »
...an also whether my approach looks ok.  Am I positioning things the most efficient way etc.  Bear in mind Unity is very new to me so any advice or pointers are welcome :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Performance Question
« Reply #4 on: October 03, 2014, 10:06:52 am »
You can cache the transform property (its not cached by default in unity 4.x but is in 5.x) but thats about it. Shader isn't gonna help, once you hit a certain number of gameobjects, overhead of calling update far exceeds the cost of the update itself.

This is a contrived example, in what scenario would you need 20000 identical objects on screen bouncing with one another? :) When you start adding different objects affecting sort order and batching perf is bound to drop. Eg. if you want a closer comparison, create a particle system, you can throw out 200k sprites without breaking a sweat.

appymedia

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Performance Question
« Reply #5 on: October 03, 2014, 10:50:53 am »
Hi,

Many thanks again for the reply and advice.  I agree I can't see any 'real world' use case for what I've been replicating.  I was merely using it as a learning exercise and also interested in how I could optimize things (if at all) to get me some good grounding for later on.

Thanks again, great product and support by the way.