Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Blue-Legs · Apr 10, 2015 at 05:35 AM · timesavequit

How to measure the real time differential between application runs

Ok, i'm making a game like "My Talking Angela". I have all the sliders (happy, hunger, sleep...) and the values decrease when my game is open. But when i close or just go to "home" in my android, or sliders values reset or just stop decreasing.

How i can put to decrease even when i close the game? I tried this, based on the subtraction of the time that has passed, but doesn't work:

 using UnityEngine;
 using System.Collections;
 using System;
 using UnityEngine.UI;
 
 public class calcular_tempo : MonoBehaviour {
 
         
         DateTime oldDate;
         DateTime currentDate;
         
         float seconds;
         
         
         public Slider BarraFelicidade;
 
 
         void Start()
         {
             currentDate = System.DateTime.Now;
             seconds = currentDate.Second - oldDate.Second;
             
             if(seconds/1 > BarraFelicidade.value) {
                 BarraFelicidade.value--;
             }
         }
         
 
         void OnApplicationQuit () 
         {
             BarraFelicidade.value = BarraFelicidade.value;
             seconds = 0;
             oldDate = System.DateTime.Now;
 
             PlayerPrefs.Save();
             
         }
         
         
         
 }
Comment
Add comment · Show 8
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 AlwaysSunny · Apr 09, 2015 at 08:55 PM 1
Share

$$anonymous$$easuring the time that has passed since the application closed is the appropriate method. Things may get screwy if the user changes time zones or messes with their system time, but I wouldn't worry about that.

Ensure you're actually saving the oldDate values to PlayerPrefs - it doesn't look like you are, unless there's new shorthand I don't know about (?) or you're doing it somewhere else.

Have a look at the documentation for PlayerPrefs and the $$anonymous$$SDN docs for DateTime to ensure your logic is sound.

avatar image AlwaysSunny · Apr 10, 2015 at 05:40 AM 1
Share

I'm afraid it won't be possible to help further without seeing your new code. Your code for this process should not be too big to post here; if it's more than a few lines, you're doing something in an inefficient way.

I recommend some research to see if anyone else has issues working with DateTime or PlayerPrefs on Android.

avatar image HarshadK · Apr 10, 2015 at 05:40 AM 1
Share

@blue-legs you can post your code to somewhere like pastebin and post that link here.

avatar image NoseKills · Apr 10, 2015 at 06:17 AM 1
Share

I'm not sure if OnQuit gets called always. If the app gets closed so that user puts the app to background first, it might not. Better use OnApplicationPaused

avatar image HarshadK · Apr 10, 2015 at 06:33 AM 1
Share

Just a few things to make your code small and clear.

  1. Rather than breaking apart DateTime into day, month, hours, $$anonymous$$utes, seconds etc just store your current datetime as string. You can convert DateTime to string using DateTime.ToString $$anonymous$$ethod (String)

  2. You can retrieve this string and convert it to DateTime again using How to: Convert a String to a DateTime (C# Program$$anonymous$$g Guide)

  3. Convert your retrieved and current DateTime string to Linux Time and subtract them to get values in seconds. How to convert To and From DateTime to Linux Time.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Webplayer - action on leave page 1 Answer

Saving highscore with PlayerPrefs 1 Answer

Some questions that needs answers. 1 Answer

How to record the real playing time? 1 Answer

A Few Questions :D 3 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