• 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 Gate · Nov 01, 2013 at 09:23 PM · date

Save the date of first run

Hi.

I want to know how can I save the date of first run of the game, so I can compare it with each days date and according to days passed the game will do some thing. I would really appreciate if you help me. Or show me some documents since I didn't know what to search.

Thanks in advanced

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
6
Best Answer

Answer by aldonaletto · Nov 02, 2013 at 12:24 AM

As @Deathdefy suggested, you could use PlayerPrefs - but with some extra code to ensure that the launch date would be saved only once. The logic is simple: try to read the initial date and, if there's none, save the current date. You must save the date as a string, since PlayerPrefs doesn't handle DateTime directly. Subtracting the launch date from the current date produces a TimeSpan value, which can be converted to days.

All of these weird functions and types are part of Mono/.Net (see .Net docs for more details), and you must import the namespace System in order to introduce them to Unity's JS. The code could be something like this:

 #pragma strict
 
 import System; // introduce DateTime to the script
 
 function Start(){
     // try to get the launch date saved as a string:
     var savedDate: String = PlayerPrefs.GetString("LaunchDate", "");
     if (savedDate == ""){ // if not saved yet...
         // convert current date to string...
         savedDate = DateTime.Now.ToString();
         // and save it in PlayerPrefs as LaunchDate:
         PlayerPrefs.SetString("LaunchDate", savedDate);
     }
     // at this point, the string savedDate contains the launch date
     // let's convert it to DateTime:
     var launchDate: DateTime;
     DateTime.TryParse(savedDate, launchDate);
     // get current DateTime:
     var now: DateTime = DateTime.Now;
     // calculate days ellapsed since launch date:
     var days: long = (now-launchDate).Days;
     print("Days since first launch:"+days);
 }


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 Gate · Nov 03, 2013 at 12:12 PM 0
Share

Thanks a lot for your answer :) it helped a lot

avatar image aldonaletto · Nov 03, 2013 at 01:50 PM 0
Share

You probably will have to clear the launch date some times during development - create a cheat code and use PlayerPrefs.Delete$$anonymous$$ey for this purpose:

 function Update(){
   // if P and 9 pressed at the same time...
   if (Input.Get$$anonymous$$ey("p") && Input.Get$$anonymous$$ey("9"){
     // delete the launch date:
     PlayerPrefs.Delete$$anonymous$$ey("LaunchDate");
   }
 }
avatar image
3

Answer by Deathdefy · Nov 01, 2013 at 10:46 PM

You can save it to your Playerprefs and load it up the exact same way.

 PlayerPrefs.SetString("FirstLaunchTime",System.DateTime.Now.ToString("MM/dd/yyyy"));

This will save the month day and year of the first launch.

You can access it by using GetString...

string dateTime = PlayerPrefs.GetString("FirstLaunchTime");

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 Gate · Nov 03, 2013 at 12:12 PM 0
Share

Thanks a lot for your help Deathdefy, I really appreciate it

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

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

Retrieve Internet real Time 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