Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Calculate Time of a shutted down app

Discussion in 'Scripting' started by Pumpkinotto, Jul 8, 2015.

  1. Pumpkinotto

    Pumpkinotto

    Joined:
    Jan 10, 2015
    Posts:
    6
    hi,
    i'd like to make a script that increase a value every second, that can be done with something like that:
    Code (CSharp):
    1. public ValueToIncrease;
    2. public ValuePerSecond;
    3.  
    4. Void start() {
    5.      InvokeRepeating ("IncreasePerSecond", 0f, 1f);
    6. }
    7.  
    8. void IncreasePerSecond(){
    9. ValueToIncrease =ValueToIncrease + ValuePerSecond;
    10. }
    the problem is that this script works only if the application is running;
    i'm going to make an adnroid application and i need that this script runs in backgrond.

    or

    a script that takes the ClockTime when the app is turned off and the ClockTime when the app is turend on,
    makes the difference and than multiply the Time in seconds with the ValuePerSecond.
    but i don't know how to get the ClockTime and theorically it could be exploited by changing the phone time.

    any help?

    thanks in advance
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    This is what you have to do; if any app could update once a second while out of focus, phone batteries would only last a few hours.


    System.DateTime.Now


    You'll probably just have to live with that. The only way (I know of) to get around that is by having a server that you can request the time from, and that would make things a lot more complicated for you (and may cost you time/money to maintain the server).
     
    Pumpkinotto likes this.
  3. Pumpkinotto

    Pumpkinotto

    Joined:
    Jan 10, 2015
    Posts:
    6
    thanks a lot.
    i need to have something like unix clock (n Seconds) and not something like the Normal Clock (DD/MM/YYYY HH:MM:SS). how i can get this?
    thanks
     
    Last edited: Jul 9, 2015
  4. Pumpkinotto

    Pumpkinotto

    Joined:
    Jan 10, 2015
    Posts:
    6
  5. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    System.DateTime.Now returns a DateTime object. This object supports the - operator, so you can simply serialize the System.DateTime.Now before your app shuts down, deserialize when it starts up, get the current System.DateTime.Now DateTime object and subtract the two. This will leave you with a TimeSpan object which can be converted to millisends, seconds, minutes, etc.. by accessing the corresponding property.
     
    Pumpkinotto likes this.
  6. Pumpkinotto

    Pumpkinotto

    Joined:
    Jan 10, 2015
    Posts:
    6
    Thanks