Search Unity

System date countdown c#

Discussion in 'Scripting' started by Louis-LCM, Jul 28, 2016.

  1. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    Hi. I'm trying to make a countdown that continue even if the app is closed. In editor works fine. Everything. But the problem is when I compile the apk for my Android. On android it don't work and I really can't figure why. Can anyone help me ? Here is my script:

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5. public class CountDown : MonoBehaviour {
    6.      DateTime currentDate;
    7.      DateTime oldDate;
    8.      public float FinishTime = 1.0f;
    9.      public Text FinishTimeText;
    10.      public Text GoneText;
    11.      public float GoneTime = 0.0f;
    12.      public bool startedProcess = false;
    13.      void Start () {
    14.          currentDate = System.DateTime.Now;
    15.          long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
    16.          DateTime oldDate = DateTime.FromBinary(temp);
    17.          TimeSpan difference = currentDate.Subtract(oldDate);
    18.          print("Difference: " + difference);
    19.          FinishTime -= (difference.Seconds) + (difference.Minutes * 60) + (difference.Hours * 60 * 60) + (difference.Days * 60 * 60 * 24);
    20.          GoneTime = (difference.Seconds) + (difference.Minutes * 60) + (difference.Hours * 60 * 60) + (difference.Days * 60 * 60 * 24);
    21.      }
    22.    
    23.      // Update is called once per frame
    24.      void Update () {
    25.          GoneText.text = GoneTime.ToString ();
    26.          var timeSpan = System.TimeSpan.FromSeconds(FinishTime);
    27.          if(FinishTime > 3600){
    28.              FinishTimeText.text = timeSpan.Hours.ToString() + "H" + " " + timeSpan.Minutes.ToString() + "M" + " " + timeSpan.Seconds.ToString() + "S";
    29.          }
    30.          else if(FinishTime > 60 && FinishTime < 3600){
    31.              FinishTimeText.text = timeSpan.Minutes.ToString() + "M" + " " + timeSpan.Seconds.ToString() + "S";
    32.          }
    33.          else if(FinishTime > 1 && FinishTime < 60){
    34.              FinishTimeText.text = timeSpan.Seconds.ToString() + "S";
    35.          }
    36.          else if(FinishTime < 1){
    37.              FinishTimeText.text = "Completed !";
    38.          }
    39.          if(startedProcess){
    40.              FinishTime = Mathf.Max (0, FinishTime-Time.deltaTime);
    41.              if(FinishTime <= 0)
    42.              {
    43.                  print("Finished");
    44.                  startedProcess = false;
    45.              }
    46.          }
    47.      }
    48.      public void StartCountDown(){
    49.          print("Started");
    50.          startedProcess = true;
    51.      }
    52.      void OnApplicationQuit()
    53.      {
    54.          //Savee the current system time as a string in the player prefs class
    55.          PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
    56.          print("Saving this date to prefs: " + System.DateTime.Now);
    57.      }
    58. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,603
    Just as a warning. System time is easy to change, so if this is an important feature, you're better off getting your time from a server which can't be changed by the player.

    Also, I'm not sure onapplicationquit is the best place to save at, considering they may not quit by normal means, which could create issues.

    If your getString calls doesn't exist, it will return "". Which when converted...not sure honestly what that gives you. It may be giving you an error. You may want to do a playerprefs.haskey check first to see if the playerpref is there and then use a default date if it isn't.

    I also highly recommend log viewer (reporter) https://www.assetstore.unity3d.com/en/#!/content/12047. This allows you to do a swirl on the screen and get your print and debug.log statements shown on your screen in an overlay. It will also show you errors which could help you track down if something isn't right.
     
  3. LTK

    LTK

    Joined:
    Jul 16, 2015
    Posts:
    22
    I don't why but I use DateTime.Ticks for my game and works fine on editor, android and ios :)
     
  4. TDArlt

    TDArlt

    Joined:
    May 22, 2013
    Posts:
    23
    Could be that OnApplicationQuit() won't be called on Android in your case.
    --> https://developer.android.com/images/activity_lifecycle.png
    --> https://developer.android.com/reference/android/app/Activity.html
    On Android, every app will go into "Pause" first and that's the only thing that's sure for the application. Everything after this state is managed by Android and could be a very fast destruction where no line of code is executed anymore.

    As a couroutine won't run in pause-mode as well, the timer will be wrong anyway. So, you should use OnPause here.
     
  5. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    @Brathnann The game will be offline. So I can't use a server for date check. And I will do a playerprefs check after my code will work properly.

    @LTK Can you give me an example ?

    @CF-Studios OnApplicationPause don't work. Is saving just some nanoseconds. No matter how long the app was closed.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,603
    I guess what you should do is say what part doesn't work. And if you don't know exactly what is going wrong, that is why I suggested the log viewer asset, which is free, as it can certainly help track down what part of your code isn't working right.