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 Ful · Apr 26, 2014 at 08:14 PM · javascriptplayerprefstimerformat

Is it possible to set a format for a playerprefs value? (Timer)

Hello Everyone!

I've managed to create a simple timer and highscore system for my game (attached to a GUIText object), which indicates on how long the player is currently surviving. When the players dies the timer stops, and if the player manages to beat his/hers record, the highscore gets updated.

The script is working perfectly, however I'm having some difficulties on figuring a way on formatting the playerprefs value of the timer in a {0:00}:{1:00}:{2:00} format for the highscore.

My script is as follows:

 #pragma strict
 
 // Timer
 
 var time                    : float;
 var timer                     : int;
 var timertext                 : String;
 var timeActive              : boolean;
 
 // HighScore
 
 var HighscoreText : GUIText;
 var playername : String; // optional feature, still work in progress
 
 function Awake() {
 
 HighscoreText.text = "HighScore: " + PlayerPrefs.GetInt ("timerScore"); // at the start of the game the highscore will be displayed
 
 }
 
 function Update () {
 
     if (timeActive) {
     
         timer   = Time.timeSinceLevelLoad;
         time   += Time.deltaTime;
     }
     
      HighscoreUpdate ();
 }
 
 
 function OnGUI () { // though ongui shouldn't be used for mobile, a simple task like this one is fine
 
 //The gui-Time is the difference between the actual time and the start time.
 var minutes : int = time / 60; //Divide the guiTime by sixty to get the minutes.
 
 var seconds : int = time % 60;//Use the euclidean division for the seconds.
 
 var fraction : int = (time * 100) % 100;
  
     timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
  
     GetComponent(GUIText).text = timertext;
  
 }
 
 
 function StopTimer2 () {
 
     
     timer = 0;
     timeActive = false;
      print ("time2 stoped!");
 
 
 }
 
 function HighscoreUpdate () {
 
     // If the value of timer happens to be more than what the current scroe is...
      if ( timer > PlayerPrefs.GetInt ( "timerScore" )) {
     // then update the new score, timerScore is a playerpref created which allows the player to save his highscore
             PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value
 
         }
 }

As you can see im using the timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); to make the timer easier to read, rather than displaying a whole number.

The highscore function gets called every time the player dies and if a new record has been set, then it updates the value.

 function HighscoreUpdate () {
 
     // If the value of timer happens to be more than what the current scroe is...
      if ( timer > PlayerPrefs.GetInt ( "timerScore" )) {
     // then update the new score, timerScore is a playerpref created which allows the player to save his highscore
             PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value
 
         }
 }

So here's my question, would anyone have an idea on how I could format the highscore in a way its more readable like I did for my timer? Such as using decimals points rather than having a whole number. I was looking into playerprefs.getstring which does work to a certain extend, but it just saves whatever time the player gets after dying, so it isn't exactly what I'm looking for.

Please let me know if I need to clarify anything else, and thanks everyone in advance!

Comment
Add comment · Show 1
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 Vonni · Apr 28, 2014 at 11:10 AM 0
Share

I just skimmed this, but why dont you use a ins$$anonymous$$d. I dont see why you need the stored data to be "easy readable", you could just convert 605.00045 sec to something afterwards. Or did I comeplete misunderstand you?

1 Reply

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

Answer by jamesflowerdew · Apr 28, 2014 at 12:02 PM

does this help? you can split a string with string slpit so you can just use whatever you like to delimit the item like below, (to and from string).

 string timertext = ""+ minutes+"":"+seconds+":"+fraction);
 
 string [] vItems=timertext.Split(':');
 
 minutes=float.Parse(vItems[0]);
 
 seconds=float.Parse(vItems[1]);
 
 fraction=float.Parse(vItems[2]);

[edit] this is c#, sorry, but the theory shouldd still work :)

Comment
Add comment · 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

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

My timer freezes everytime I leave the scene and come back. 1 Answer

How do you save timer with PlayerPrefs? 1 Answer


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