Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Android performance problem

Discussion in 'Android' started by alex161, Jun 29, 2014.

  1. alex161

    alex161

    Joined:
    Jun 29, 2014
    Posts:
    6
    Hi, ive an very strange problem. Im programming an 2d Game for mobile platforms. As im processing further my application has an horrible performance, but only on my android device (PC is perfect) and i dont know what to do about it.
    Atm we have an surface with box collider with an friction of 0. Then there are Objects(prefabs) with rigidbody2d that slide onto it. These Objects have an script that looks for the velocity and applys a force if its under an value. It also handels touch commands (problem occurs even if there is no input). These objects stutter and feel extremely laggy on my Samsung S4. On the normal PC, this effect is not visible with fps over 2k.
    We instanciate an Object every Second and destroy an other every second too. There are not more than 10 Objecte present at any time.
    The prefab has an normal Sprite Renderer attached to it (Sprites-Default material), Rigidbody2D with Interpolate turned on and Mode= Continous.

    Some data at peek:
    Draw Calls:7
    Tris:38 Verts:84
    Used Textures: 8 - 2.6MB
    I think this is very low.

    Weve done the following:
    Set almost everything on static
    Change Texture Size to 512 and to compressed
    Optimze Mesh Data (but we only use some sprites anyway)
    Change Physics from continous to discrete
    Instanciate 500 of our Objects and deactivate them. Later we only activate it instead of instanciate and we dont destroy them, we only deactivate it.
    Set resolution on Phone from FullHD to 300:200
    Keep almost all out of Update() and move to FixexUpdate

    As far nothing helped. Does anybody has an idea? I think there is somthing im missing. I dont have pro, so there is no profiler. But an monitor app on my s4 showed my app used peak only 20% CPU on my device, so this is not the limiting factor

    Thanks for everybody reading my huge post and wants to help,




    Adrian

    Code (CSharp):
    1. void Start ()
    2.     {debugger = new GameDebugger("FoodScript");
    3.     if (abfallObject == null)
    4.         {
    5.         myGravityFactor = rigidbody2D.gravityScale;
    6.  
    7.         //seppMouth = GameObject.Find ("Sepp_Mouth");
    8.         //seppMouthVector = new Vector2 (seppMouth.transform.position.x, seppMouth.transform.position.y);
    9.  
    10.         abfallObject = GameObject.Find ("Abfalleimer");
    11.         abfallAnimator = abfallObject.GetComponent<Animator>();
    12.         abfallStateHash = Animator.StringToHash("AbfallState");
    13.      
    14.         gameManager = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
    15.  
    16.         }
    17.  
    18.     setRandomTexture();
    19.  
    20.     }
    21.  
    22.  
    23.     void Update ()
    24.     {
    25.     followMouse();
    26.     }
    27.  
    28.     void FixedUpdate()
    29.     {
    30.     if (isSelected)    //Addiert de ganze Zeit Mousevektor. Wird später der Durchschnitt genommen und dann Differenz als Kraft addiert!
    31.         {
    32.         if (AfterIsSelectedVectorInt >= AfterIsSelectedVector.Length) AfterIsSelectedVectorInt = 0;
    33.      
    34.         AfterIsSelectedVector[AfterIsSelectedVectorInt++] = (Vector2) Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1f));
    35.         }
    36.  
    37.     }
    38.  
    39.     void OnCollisionStay2D(Collision2D collision) //Wenn auf Laufband und zu langsam, gib Stoß bis Zielgeschwindigkeit erreicht ist
    40.     {
    41.         if (collision.gameObject.name == laufbandString && !isSelected && rigidbody2D.velocity.x < targetSpeed)
    42.             {
    43.             rigidbody2D.AddForce(Vector2.right * forceForwordFactor);
    44.             //transform.position = Vector3.Lerp(transform.position, transform.position + Vector3.right * forceForwordFactor/2.5f,   0.2f * Time.deltaTime);
    45.             }
    46.     }
    47.  
    48.     void OnTriggerEnter2D(Collider2D other)
    49.  
    50.     {
    51.         if (other.gameObject.name == FoodEndColliderName)     //wenn Mülleimer berührt
    52.             {
    53.             //Debug.Log ("Object hat " + FoodEndColliderName + " Trigger berührt");
    54.             //onLaufband = false;    //Führt zu Problemen
    55.             int abfallState = abfallAnimator.GetInteger(abfallStateHash);
    56.  
    57.             if (checkedIfSpriteIsBad());    //wenn böse Essen, mach nix
    58.             else     {
    59.                     if (abfallState > 2)  Application.LoadLevel("GameOverScene");
    60.                     abfallAnimator.SetInteger(abfallStateHash, ++abfallState);    //Wenn gutes Essen, fülle Mülleimer
    61.                     }
    62.  
    63.             //Destroy(gameObject, 0.1f);
    64.             gameObject.SetActive(false);
    65.             }  
    66.  
    67.         if (other.gameObject.name == SeppMouth_ColliderName && !isSelected)     //wenn Mund berührt und nicht von Mouse controlliert
    68.             {
    69.             //Debug.Log ("Object hat " + SeppMouth_ColliderName + " Trigger berührt");
    70.             //onLaufband = false;    //Führt zu Problemen
    71.             if (!checkedIfSpriteIsBad()) gameManager.addPoint();
    72.             else {/*Time.timeScale = 0;*/ gameManager.addPlayerDamage(); }
    73.          
    74.  
    75.             }  
    76.     }
    77.  
    78.     void OnTriggerStay2D(Collider2D other)    //Für den Fall, dass Essen bereits im Collider liegt
    79.     {
    80.         //Debug.Log("Object in OnTriggerStay2D");
    81.         if (other.gameObject.name == SeppMouth_ColliderName && !isSelected)     //wenn Mund berührt und nicht von Mouse controlliert
    82.         {
    83.             //Debug.Log ("Object hat " + SeppMouth_ColliderName + " Trigger berührt");
    84.             //onLaufband = false;
    85.             if (!checkedIfSpriteIsBad()) gameManager.addPoint();
    86.             else {/*Time.timeScale = 0;*/ gameManager.addPlayerDamage(); }
    87.  
    88.             //Destroy(gameObject);
    89.             gameObject.SetActive(false);
    90.         }  
    91.     }
    92.  
    93.     //Input here
    94.     void OnMouseDown()
    95.     {
    96.     isSelected = true;
    97.     rigidbody2D.gravityScale = 0;
    98.     rigidbody2D.velocity = Vector2.zero;
    99.     rigidbody2D.angularVelocity = 0f;
    100.  
    101.     //Alte Version, wo Objekt zu Mund geflogen ist
    102.     //Debug.Log ("Mouse Clicked on Object");
    103.     }
    104.  
    105.     void OnMouseUp()
    106.     {
    107.     isSelected = false;
    108.     rigidbody2D.gravityScale = myGravityFactor;    //Setzt alten Gravity-Wert
    109.  
    110.     Vector2 tmpVector = Vector2.zero;
    111.     for (int i = AfterIsSelectedVector.Length; i > 0; i--)    //Addiert alle Vektoren
    112.         {
    113.         tmpVector = tmpVector + AfterIsSelectedVector[i-1];
    114.         }
    115.     tmpVector = tmpVector / AfterIsSelectedVector.Length;    //Bildet Durschnitt
    116.     tmpVector = (Vector2) transform.position - tmpVector;    //Bildet resultierenden Vektor
    117.     rigidbody2D.AddForce(tmpVector * 200);        //Fügt als Kraft hinzu..
    118.  
    119.     }
    120.  
    121.     void followMouse()
    122.     {
    123.     if (isSelected && Input.GetButton("Fire1"))
    124.         {
    125.         Vector3 mousePos = Input.mousePosition;
    126.         Vector3 wantedPos = Camera.main.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, 10f));
    127.         transform.position = wantedPos;
    128.      
    129.         //transform.position = wantedPos;
    130.         //Vector2 directionVector = (Vector2) wantedPos - new Vector2(transform.position.x , transform.position.y);
    131.         //rigidbody2D.AddForce(directionVector * forceUpFactor);
    132.         transform.position = wantedPos;
    133.         }
    134.     }
    135.  
    136.  
    137.     private void setRandomTexture()
    138.     {
    139.     SpriteRenderer sr = GetComponent<SpriteRenderer>();
    140.     //sr.sprite = sprites [1];
    141.     sr.sprite = sprites [Random.Range((int)0, sprites.Length)];
    142.  
    143.     }
    144.  
    145.     private bool checkedIfSpriteIsBad()
    146.     {
    147.     bool containsBadSprite = false;
    148.     string spriteName;
    149.  
    150.     SpriteRenderer sr = GetComponent<SpriteRenderer>();
    151.     spriteName = sr.sprite.name;
    152.  
    153.     for (int i = 0; i < badSpriteArray.Length; i++)
    154.         {
    155.          if (spriteName.Equals(badSpriteArray[i])) return true;
    156.         }
    157.  
    158.     return containsBadSprite;
    159.     }
    160.  
    161.  
    162. }
     
  2. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
    Did you try Unity profiler?
     
    alex161 likes this.
  3. alex161

    alex161

    Joined:
    Jun 29, 2014
    Posts:
    6
    Hi, thank you for your answer. We dont have pro so there is no profiler. Im at my and of knowledge, because theoretically everything should run fine, but i doesnt :-(.. does anyone have an idea? Ive written a simple algorithm that shows time that the script takes to run. Sometimes this shows some microseconds, sometimes incredible times like a minute or so. Commenting out that parts of script does not help..
    Greetings
     
  4. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    There is till a basic profiler. You turn on development build turn on enable internal profiler and watch through logcat. it will give numbers that MAY help you
     
    alex161 likes this.
  5. alex161

    alex161

    Joined:
    Jun 29, 2014
    Posts:
    6
    Hi, thank you for your tip! I didnt know that. I'll check that and will post here, if i have found an solution. Thx!!!!
     
  6. alex161

    alex161

    Joined:
    Jun 29, 2014
    Posts:
    6
    Ok, now i took a look at the internal profiler. Thats what it showed me at the moment a lag occurs:

    Code (CSharp):
    1.  
    2. Unity(29312): Android Unity internal profiler stats:
    3. Unity(29312): cpu-player>    min:  4.1   [B]max: 557.1[/B]   avg: 26.8
    4. Unity(29312): cpu-ogles-drv> min:  0.1   max:  0.5   avg:  0.3
    5. Unity(29312): cpu-present>   min: -16.8   max:  0.2   avg: -0.7
    6. Unity(29312): frametime>     min:  4.5   [B]max: 557.5[/B]   avg: 26.4
    7. Unity(29312): draw-call #>   min:   3    max:   7    avg:   5     | batched:     0
    8. Unity(29312): tris #>        min:     8  max:    30  avg:    14   | batched:     1
    9. Unity(29312): verts #>       min:    16  max:    68  avg:    31   | batched:     3
    10. Unity(29312): player-detail> physx:  0.0 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  1.3 fixed-update-count: 0 .. 1
    11. Unity(29312): mono-scripts>  update:  1.5   fixedUpdate:  0.0 coroutines:  0.0
    12. Unity(29312): mono-memory>   used heap: 4295348224 allocated heap: 4610650190427193344  max number of collections: -536870912 collection total duration:  0.0
    13.  
    As im guessing, it seems to be a cpu-problem not related to fixedUpdate or Update. Hm i have to rethink my code. This tip was great, thanks Kilrath81!!

    Greetings
     
  7. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    yea im guessing that 557.1 spike likely not good
     
    alex161 likes this.