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 ?