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 GKdev1980 · Apr 03, 2014 at 09:16 PM · date

Days since first launch

I want to count the days since the first launch of my game. I'm not talking about playing time, I'm talking about the total days that have passed, to display some statistics when the player finishes the game. How can I do this?

I suppose it has something to do with System.DateTime, but I'm wondering exactly what I would have to implement.

The very first time the game was launched it would have to store that day's System.Datetime.Today in a variable (of type DateTime?) and then do the same at the day someone would finish the game and then subtract the two and get an integer as a result?

If so, how would I ensure that the date of the first launch would only be stored once and not every time the game is launched?

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

4 Replies

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

Answer by Lo0NuhtiK · Apr 03, 2014 at 11:21 PM

I wasn't exactly sure how to do this either until I read your question a few minutes ago and used some voodoo known as Google.

I threw together a script just now using PlayerPrefs that seems to be working. I changed my PC's date a few times to test it out. Anyway, here ya go.

Put this script (rename it to whatever, of course) on some kind of gameManager object you're using in your game.

 public class WhateverScriptNameYouWant: MonoBehaviour
 {    
 
     private static System.DateTime startDate ;
     private static System.DateTime today ;
 
     void Start()
     {
         SetStartDate() ;
     }
 
     void SetStartDate()
     {
         if(PlayerPrefs.HasKey("DateInitialized")) //if we have the start date saved, we'll use that
             startDate = System.Convert.ToDateTime(PlayerPrefs.GetString("DateInitialized")) ;
         else //otherwise...
         {
             startDate = System.DateTime.Now ; //save the start date ->
             PlayerPrefs.SetString("DateInitialized", startDate.ToString()) ;
         }
     }
 
 
     public static string GetDaysPassed()
     {
         today = System.DateTime.Now ;
 
         //days between today and start date -->
         System.TimeSpan elapsed = today.Subtract(startDate) ;
 
         double days = elapsed.TotalDays ;
 
         return days.ToString("0") ;
     }
 
 }


Then from whatever other script(s) you're using to show the days passed, you just call it.

eg :

 void OnGUI()
 {
    GUILayout.Label("Days Passed : " + WhateverScriptNameYouWant.GetDaysPassed()) ;
 }
Comment
Add comment · Show 1 · 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 GKdev1980 · Apr 04, 2014 at 07:27 AM 0
Share

Thanks for the answer, I haven't used PlayerPrefs too much until now (I've only used it for saving score during exit and retrieving it on relaunch) so I guess that's where I should be focusing. Reading through your code makes perfect sense.

Cheers..

avatar image
0

Answer by dillon_kneeland · Apr 03, 2014 at 09:36 PM

You could store it in a text file that could be read by the game when they finish. And in order to make it do it only once you could have it check to see if the file was already there.

Comment
Add comment · Show 1 · 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 GKdev1980 · Apr 04, 2014 at 07:28 AM 0
Share

Hmmm. A text file seems easily editable, I'm not sure it's very practical..

avatar image
0

Answer by KnightRiderGuy · Jan 05, 2017 at 12:58 AM

I tried this out but for some reason overtime I start up my game session the OnGUI spits out this insane number something like: 736333 instead of what I think I was saving with the SaveStartDate... I thought the way it was supposed to work was save the start date and then subtract the current date from the start date in the OnGUI... instead I get this each timealt text


screenshot-2017-01-04-151205.png (39.6 kB)
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
avatar image
0

Answer by jmgek · Jan 05, 2017 at 02:59 AM

While @loonhawks answer is great that opens up a few hacks, The user could easily change their system time thus work around what you're trying to do. He even went on to test it that way.

I suggest you use his answer as a backup if the user has no Internet access, if they have Internet access get your date time from the Internet:

 public static DateTime GetNistTime()
 {
     DateTime dateTime = DateTime.MinValue;
 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
     request.Method = "GET";
     request.Accept = "text/html, application/xhtml+xml, */*";
     request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
     request.ContentType = "application/x-www-form-urlencoded";
     request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     if (response.StatusCode == HttpStatusCode.OK)
     {
         StreamReader stream = new StreamReader(response.GetResponseStream());
         string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
         string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
         double milliseconds = Convert.ToInt64(time) / 1000.0;
         dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
     }
 
     return dateTime;
 }
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 KnightRiderGuy · Jan 05, 2017 at 03:13 PM 0
Share

Thanks @ jmgek,

I really don't need anything that complicated... what I was looking for was a method of tracking how many days had passed since a certain question was asked.

I have an if statement that in the first part checks to see if the question was asked and if it was gives a response of "You Just Asked $$anonymous$$e That" If the question was not asked then it would ignore the first part of the if statement and go onto the 2nd part where a different response would be given, something like "No I'm afraid not"

But I'm having difficulty trying to figure out how to get my data saved into player prefs back into a usable format for my if statement.

I'm not really concerned if some tries to change their time information or even the player prefs document, the process does not need to be that secure as it's only to make the conversational aspects of the VR more realistic by checking things like system time... stuff like that so no one would feel the need to cheat that... at least I can't think of any reason that would make sense. ;)

avatar image jmgek KnightRiderGuy · Jan 05, 2017 at 05:19 PM 0
Share

Ah, okay thats fine, I was assu$$anonymous$$g you were needing data for persistent logic "similar to how fable 3 calculated investments when the player was not playing"

So what you're going to want to do is extend what @loonhawm said and create a class that could hold all your info, question asked, last time asked, correct answers, etc and serialize that class.

 Questions{
 Datetime lastTimeAsked;
 String question;
 //other properties of your question
 } 

Alternatively if you just need the question and date just make a dictionary:

Dictionary<datetime, string> lastquestion;

https://msdn.microsoft.com/en-us/library/4abbf6k0(v=vs.110).aspx

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

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

Related Questions

Unity 3 Coming out? 1 Answer

how to load and save date to && from Internet ? 2 Answers

In game clock/Time 1 Answer

How can I get the time since the epoch date in Unity3D? 2 Answers

[ANSWERD] Do something when it is the correct date 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