c# - Optimizing Unity Code - InvokeRepeating / StartCoroutine -
i have powerup countdowntimer. timer use invokerepeating. use startcoroutine.
so 1 choose? don't difference...
here code:
float powerupduration = 5;               // duration of buff     float slowmotiontimescale = 0.5f;     // slow down player 0.5     float defaulttimescale = 1;              // normal game speed     private void ontriggerenter(collider collision)     // player colliding powerup     {             if (collision.gameobject.tag == "player")             {                     time.timescale = slowmotiontimescale;     // time gets slowed                     time.fixeddeltatime = time.fixeddeltatime * time.timescale;     // smooth slow down                     invokerepeating("speedmanager", 0, 1); // start countdown             }     }     void speedmanager()     {             if (powerupduration > 0)         // count 0             {                     powerupduration--;             }             else              // stop countdown, set timespeed default             {                     cancelinvoke("speedmanager");                     time.timescale = defaulttimescale;                     powerupduration = 5;                 // reset timer             }     }      
invokerepeating() allows invoke set interval.
startcoroutine() allows fade-wait-unfade , continue , on.
hence not make difference 1 use on other. might when functionality require changes.
do consider, if not need fade-wait-unfade stuff in function, better fit 1 of many update() functions of unity instead. 
invokerepeating() internaly makes use of reflection, in 99.99% of scenarios slower using build in update. 
to decide update suit functionality best
update:  input, translate/rotation (non-physics)
fixedupdate: physics
lateupdate: camera movement, animation 
Comments
Post a Comment