Search Unity

Question analog clock stopwatch script

Discussion in 'Scripting' started by Veteran66, May 26, 2021.

  1. Veteran66

    Veteran66

    Joined:
    Jul 12, 2015
    Posts:
    50
    Hi all
    Unity 2020 Android

    i use the script from here:


    it work fine, but when my screen go off, the watch will make a stop, after screen aktiv the watch will go.

    What can I do to keep the watch active when the smartphone is paused?
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
    You can use System.DateTime to store and process time when your app is not running.

    Inside of System.DateTime is all sorts of ways of manipulating and calculating time.

    If you want the system to start up your app when it is not in the foreground, this is a FAR more involved process using something called local notifications.

    This process lives entirely outside of Unity and must be integrated on a case-by-case basis, eg, different for Android and iOS, plus the user generally must agree to letting your app show notifications.
     
  3. Veteran66

    Veteran66

    Joined:
    Jul 12, 2015
    Posts:
    50
    sorry i an no scripter, can you add a exemble in AnalogClock script?
     

    Attached Files:

  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
  5. Veteran66

    Veteran66

    Joined:
    Jul 12, 2015
    Posts:
    50
    I was hoping for a little help, not a scripting course :(
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
    Handling time is trivial. You can just use a float to count seconds, which is likely what the above stopwatch does.

    Fully and properly handling time advancement when your app isn't running is a colossally massive can of worms and edge cases and hassle and data formats and back-tracking of events that must be reconciled and handled if the app misses its initial start signal, etc.

    I suggest trying the minimum implementations, iteratively, step by step proving each part works:

    - write the start time of the stopwatch out to persistent storage (such as PlayerPrefs)
    - read that start time back and ensure you're getting it correct when the app resumes
    - continue the stopwatch counting once you read it back

    That is the bare minimum of your original ask,