- Home /
System date countdown c#
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:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class CountDown : MonoBehaviour {
DateTime currentDate;
DateTime oldDate;
public float FinishTime = 1.0f;
public Text FinishTimeText;
public Text GoneText;
public float GoneTime = 0.0f;
public bool startedProcess = false;
void Start () {
currentDate = System.DateTime.Now;
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
DateTime oldDate = DateTime.FromBinary(temp);
TimeSpan difference = currentDate.Subtract(oldDate);
print("Difference: " + difference);
FinishTime -= (difference.Seconds) + (difference.Minutes * 60) + (difference.Hours * 60 * 60) + (difference.Days * 60 * 60 * 24);
GoneTime = (difference.Seconds) + (difference.Minutes * 60) + (difference.Hours * 60 * 60) + (difference.Days * 60 * 60 * 24);
}
// Update is called once per frame
void Update () {
GoneText.text = GoneTime.ToString ();
var timeSpan = System.TimeSpan.FromSeconds(FinishTime);
if(FinishTime > 3600){
FinishTimeText.text = timeSpan.Hours.ToString() + "H" + " " + timeSpan.Minutes.ToString() + "M" + " " + timeSpan.Seconds.ToString() + "S";
}
else if(FinishTime > 60 && FinishTime < 3600){
FinishTimeText.text = timeSpan.Minutes.ToString() + "M" + " " + timeSpan.Seconds.ToString() + "S";
}
else if(FinishTime > 1 && FinishTime < 60){
FinishTimeText.text = timeSpan.Seconds.ToString() + "S";
}
else if(FinishTime < 1){
FinishTimeText.text = "Completed !";
}
if(startedProcess){
FinishTime = Mathf.Max (0, FinishTime-Time.deltaTime);
if(FinishTime <= 0)
{
print("Finished");
startedProcess = false;
}
}
}
public void StartCountDown(){
print("Started");
startedProcess = true;
}
void OnApplicationQuit()
{
//Savee the current system time as a string in the player prefs class
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
print("Saving this date to prefs: " + System.DateTime.Now);
}
}
Answer by Denscrivent · Jul 28, 2016 at 04:06 AM
Hi Louis I made a simplified coding about the datetime. Try this
public static DateTime ExampleDateTime{
set{PlayerPrefs.SetString("Example",value.ToBinary().ToString());}
get{
long temp = Convert.ToInt64( PlayerPrefs.GetString("Example") ) ;
DateTime tempDate = DateTime.FromBinary(temp);
return tempDate;
}
}
Hi ...Thank you very much :D But I don't need simplified code ... I just want to know why is working on PC but not on android. And also to learn something new.
Have you tried adb logcat? that might help. I developed games on android too and it worked.
Answer by yasmin1818 · Sep 20, 2017 at 01:22 PM
Have you a problem or not about unity ? I want to show real time on application quit every time. But I can't solve this problem.
Your answer
Follow this Question
Related Questions
Android not render like Editor, Windows [Screenshots] 0 Answers
unity editor update,unity editor version update 1 Answer
com.UnityTestRunner.UnityTestRunner 0 Answers
android project issue/error/problem...help!! 0 Answers
Invalid Android NDK Directory 2 Answers