Search Unity

Does changing gameobject scale hit performance?

Discussion in 'General Graphics' started by KamranWali, Feb 27, 2019.

  1. KamranWali

    KamranWali

    Joined:
    Sep 10, 2015
    Posts:
    24
    Hello all, I wanted to know if changing gameobject scale during runtime causes some performance issue? Some years ago I read that changing gameobject scale during runtime or having unequal scales(scale = 1, 1, 0.5) causes some performance issue on both CPU and GPU but this was for another game engine, so I wanted to know if Unity have something similar. If it does cause performance hit then what is the right way of changing the scale continuously during run time?

    The platform I am targeting is mobile. I have made a monobehaviour that just changes the scale overtime. So far I have no performance issue but still want to know and do it the correct way.

    This is my code for changing the scale over time:
    Code (CSharp):
    1. public class ScaleController : MonoBehaviour {
    2.  
    3.     public Vector3 OriginalScale;    // Original scale of the gameobject
    4.     public Vector3 TargetScale;      // Target scale of the gameobject
    5.     public float Speed;              // Speed of scale change
    6.     private float _ScaleCovered = 0; // Scaling factor
    7.     private int _Dir = 1;            // Direction of scaling
    8.                                      //  1 = Increase scale,
    9.                                      // -1 = Decreasing scale
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.         // Incrementing the scaling factor
    15.         _ScaleCovered += Speed * _Dir * Time.deltaTime;
    16.  
    17.         // Condition for starting decreasing the scale
    18.         if (_ScaleCovered >= 1) { _ScaleCovered = 1; _Dir = -1; }
    19.         // Condition for starting increasing the scale
    20.         else if (_ScaleCovered <= 0) { _ScaleCovered = 0; _Dir = 1; }
    21.        
    22.         // Changing the scale using Slerp
    23.         transform.localScale = Vector3.Slerp(OriginalScale,
    24.                                             TargetScale,
    25.                                             _ScaleCovered);
    26.     }
    27. }
    Thank you.