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 Matroyer · Jan 31, 2012 at 07:53 PM · iphonetimercountdownclosed

Countdown for iPhone that continues while the apps is closed

I'm currently working on a game on iPhone with resources regained over time like a few game already do. I was wondering how do you make a countdown that continue while the apps is closed. I've read about the System.DateTime.Now that tell you the current real time on the machine but it never seemed to work like I wanted. If someone could give me hints or like me to a similar thread, it would be really appreciated because all I've found for the moment is how to make a basic timer...

Thanks for your help!

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

Answer by flamy · Feb 02, 2012 at 02:33 PM

System.DateTime.Now.ToUniversalTime(); this function returns the current time in a universal format. what you have to do is save it to local file (or playerprefs) and reload it back when you open the app again get the time using the same function. Now is you find the difference between these you will get the time offline, subtract it from the countdown time you have saved early to the local. The difference will be in System.TimeSpan so should know abt tht too

example:

this code i used to retrieve the difference in minutes

     string temp=PlayerPrefs.GetString("lastoffline");
     
     double lastMinutes = System.Double.Parse(temp);
     System.DateTime dt2 = System.DateTime.Now.ToUniversalTime();
     
     System.TimeSpan dt3 = dt2-((new System.DateTime(0))).ToUniversalTime();
     double currentMinutes = dt3.TotalMinutes;
     double totalOffline=currentMinutes-lastMinutes;

    // this code in start..

previously i have saved it from the OnApplicationQuit method of monobehaviour

     System.DateTime dt = System.DateTime.Now.ToUniversalTime();
     System.DateTime dt1 = new System.DateTime(0).ToUniversalTime();
     System.TimeSpan time1 = dt-dt1;
     double lastMinutes = time1.TotalMinutes;
 
     PlayerPrefs.SetString("lastoffline",(lastMinutes).ToString());


im changing double to string because playerprefs doesnt have option to save double,hence saving it as string and retrieving original data on loading

Comment
Add comment · Show 3 · 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 Matroyer · Feb 06, 2012 at 04:42 PM 0
Share

I tried to add this in my app but im coding in Javascript so I get some errors... I changed a few things, following the errors message, but I still can't figure out how to convert or use the doubles and the Double.Parse method...

avatar image Julien-Lynge · Feb 06, 2012 at 04:47 PM 0
Share

Try these resources for converting: http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/

avatar image Matroyer · Feb 08, 2012 at 04:40 PM 0
Share

Thanks alot for the link, I figured out late yesterday that I had to use System.Double ins$$anonymous$$d of double, now everything works fine!

avatar image
1

Answer by cupsster · Feb 02, 2012 at 02:02 PM

Try to store time from internet source when application closes and on application load get time again. After this make your desired computation for gained resources. You can extend this idea to hash/crypt time in player preferences along with comparision of time on device to be sure user does not cheat. If watching time is crucial for your app you must be carefull about not let app fooled by user. i think that online time check is cruial for this.

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

8 People are following this question.

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

Related Questions

Calling methods 0 Answers

Timer doesn't work properly 2 Answers

When Score goes up by 25 change timer value 1 Answer

Countdown Timer Help About putting 0's 1 Answer

Timer counts down unwanted 2 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