Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Louis-LCM · Jul 27, 2016 at 10:49 PM · androideditorandroid buildissuecountdown

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);
     }
 }
 
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
1

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;
     }
 }

Comment
Add comment · Show 2 · 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 Louis-LCM · Jul 28, 2016 at 07:54 AM 0
Share

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.

avatar image Denscrivent · Aug 02, 2016 at 04:44 AM 0
Share

Have you tried adb logcat? that might help. I developed games on android too and it worked.

avatar image
0

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.

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

94 People are following this question.

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

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


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