- Home /
Unity time counter 50% slower FPS?
Hello, I have made a time counter to my game, to show people how long time they have played the game. When I use the script on my PC it work's fine and I don't notice any FPS slowdown. But when I play it on my Android device (Galaxy SIII), it switches from 60 FPS (without the time-script) to 20-30 FPS (with the time-script). Can someone please tell me what's wrong? Here's my script :)
Thanks, Andreas
using UnityEngine;
using System.Collections;
public class TimePlayed : MonoBehaviour {
float SecondsPlayed;
float MinutesPlayed;
float HoursPlayed;
string Minutes;
string Seconds;
string Hours;
int RoundedSeconds;
int RoundedMinutes;
int RoundedHours;
public GUIText SecondsPlayedDisplay;
// Use this for initialization
void Start () {
SecondsPlayed = PlayerPrefs.GetFloat("SecondsPlayed", SecondsPlayed);
MinutesPlayed = PlayerPrefs.GetFloat("MinutesPlayed", MinutesPlayed);
HoursPlayed = PlayerPrefs.GetFloat("HoursPlayed", HoursPlayed);
}
// Update is called once per frame
void Update () {
RoundedSeconds = Mathf.RoundToInt(SecondsPlayed);
RoundedMinutes = Mathf.RoundToInt(MinutesPlayed);
RoundedHours = Mathf.RoundToInt(HoursPlayed);
//Second or Seconds
if (RoundedSeconds == 1) {
Seconds = "Second";
}
if (RoundedSeconds != 1) {
Seconds = "Seconds";
}
//Minute or Minutes
if (RoundedMinutes == 1) {
Minutes = "Minute";
}
if (RoundedMinutes != 1) {
Minutes = "Minutes";
}
//Hour or Hours
if (RoundedHours == 1) {
Hours = "Hour";
}
if (RoundedHours != 1) {
Hours = "Hours";
}
SecondsPlayed += Time.deltaTime;
if (SecondsPlayed >= 60f) {
SecondsPlayed = 0f;
MinutesPlayed += 1f;
}
if (MinutesPlayed >= 60f) {
MinutesPlayed = 0f;
HoursPlayed += 1f;
}
SecondsPlayedDisplay.text = (Mathf.RoundToInt(HoursPlayed).ToString() + " " + Hours + ", " + Mathf.RoundToInt(MinutesPlayed).ToString() + " " + Minutes + " and " + Mathf.RoundToInt(SecondsPlayed).ToString() + " " + Seconds );
PlayerPrefs.SetFloat("SecondsPlayed", SecondsPlayed);
PlayerPrefs.SetFloat("MinutesPlayed", MinutesPlayed);
PlayerPrefs.SetFloat("HoursPlayed", HoursPlayed);
}
}
Answer by whydoidoit · May 10, 2013 at 10:14 PM
Writing to PlayerPrefs every frame is not necessarily a very good idea and could be considerably slower on the device.
What can I do ins$$anonymous$$d? :-) I would like to save the "total" time, the player has played :-)
I should also point out that it might not be saved on the device unless you use PlayerPrefs.Save
Thank you so much :-) :D, I didn't know there was an OnApplicationQuit function :)
Answer by chillersanim · May 11, 2013 at 09:12 AM
You could try to update it not every frame, but let's say every fifth frame. That can be done by simply pasting this code at the beginning of update():
if (Time.deltaTime % 5 != 0)
{
return;
}
Because you are only working with seconds, it is not necessary to update it that often ;)
Greetings Chillersanim
On that way, you could also save the played time all [n] seconds, to prevent data loss by a program crash. I would recommend you to save regularly, because not all mobile devices are stable.
Your answer
Follow this Question
Related Questions
Game speed slows down as FPS slows down (on android, only after unity3.4) 1 Answer
i have a fps sniper game and it runs slow on the tablet 1 Answer
Why an empty project jumping 20 - 120 fps on android? 2 Answers
Graphics.PresendAndSync, CPU Usage Performance issue 0 Answers
FPS of up-to-date action game on mobile devices? 60 vs 30 FPS? 0 Answers