Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AndreasX12 · May 10, 2013 at 10:04 PM · androidfpstimerslowcount

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);
      }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndreasX12 · May 10, 2013 at 10:19 PM 0
Share

What can I do ins$$anonymous$$d? :-) I would like to save the "total" time, the player has played :-)

avatar image whydoidoit · May 11, 2013 at 12:26 AM 0
Share

Save it in OnDisable or OnDestroy...

avatar image whydoidoit · May 11, 2013 at 12:26 AM 0
Share

Or OnApplicationQuit or OnApplicationPause

avatar image whydoidoit · May 11, 2013 at 07:45 AM 0
Share

I should also point out that it might not be saved on the device unless you use PlayerPrefs.Save

avatar image AndreasX12 · May 11, 2013 at 09:12 AM 0
Share

Thank you so much :-) :D, I didn't know there was an OnApplicationQuit function :)

avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndreasX12 · May 11, 2013 at 09:25 AM 0
Share

Thank you, this is also a good idea :)

avatar image chillersanim · May 11, 2013 at 09:31 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges