Optimise for long time running on Android

Hi everybody, I am quickly making an app on Android, using unity, but I have a problem, that my application must run during a long time, more than an hour eventually ! And I do not want my phone to lose too much battery and get warm !
So I need to optimize my app, but I need you to show me how I could do it.

This is my code :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class Timer : MonoBehaviour {

    private float holdTime = 0.075f;
    private float acumTime = 0;
   

    public Image hourGlassUp;
    public Image hourGlassDown;

    public Text timerText;   
    public float startTime;
    public float countTime;
    public float ratio;

    void Start () {
        timerText = GetComponent<Text>();
        if(Application.loadedLevel == 1)
        {
            startTime = PlayerPrefs.GetInt("workTime");
        }else if(Application.loadedLevel == 2)
        {
            startTime = PlayerPrefs.GetInt("pauseTime");
        }
        countTime = startTime;
   
    }
   

    void Update () {
        if(countTime > 0)
        {
        countTime -= Time.deltaTime;
       
        ratio = countTime / startTime;
        float t = countTime;
        hourGlassUp.fillAmount = ratio;
        hourGlassDown.fillAmount = 1-ratio;
        string minutes = ((int) t /60).ToString();
        string seconds = ( t % 60).ToString("f0");
        timerText.text = minutes + ":" + seconds;
        }else{
        timerText.color = Color.red;
        }

        if (Input.GetKeyDown(KeyCode.Escape))
            Application.LoadLevel(0);

        if(Input.GetTouch(0).tapCount == 2 & startTime-countTime > 0.5)
        {
            if(Application.loadedLevel == 1)
            {
                Application.LoadLevel(2);
            }else if(Application.loadedLevel == 2)
            {
                Application.LoadLevel(1);
            }
        }

        if(Input.touchCount > 0)
        {
            acumTime += Input.GetTouch(0).deltaTime;
           
            if(acumTime >= holdTime)
            {
                if(Input.GetTouch(0).phase == TouchPhase.Ended)
                {
                    acumTime = 0;
                    Application.LoadLevel(0);
                }

            }
           
            if(Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                acumTime = 0;
            }
        }
    }
}

PS : I have an idea already, maybe “update” not once per frame like the Update function do, but once per second ? But I do not really know how to do… Do you think that easily realizable and can really make a difference ?

I would not expect this script to affect battery usage much. Simply just rendering a frame will use orders of magnitudes more power. Reducing the frame rate is probably a much more efficient way to reduce power consumption.

Thank you guy, if someone have a better way i’m interrest !

Small optimization would be to use string.Format and hardcoded string values on line 45 to reduce garbage collection, also using a Coroutine and only updating once per second would help.

its just micro optimizations though, and wont effect the power consumption of heat on the device, he really just wants to lock it to a lower framerate which should be doable via Application.targetFrameRate

1 Like