Android System.DateTime.now的Unity系统时间不起作用? [英] Unity system time for android System.DateTime.now doesn't work?

查看:527
本文介绍了Android System.DateTime.now的Unity系统时间不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的这段代码在编辑器上可以正常工作,但在手机上却无法正常工作.我使用统一2017.4.3. 问题是,当在Andriod设备中关闭该应用程序时,它根本感觉不到,但在编辑器中运行良好,因此为什么"System.DateTime.now"在Andriod设备中不起作用?离开使其工作?

I made this code that works fine on the editor but not on my phone. I used unity 2017.4.3. The problem is that when the app is closed in the andriod device, it doesnt feel it at all but it works fine in the editor so why doesn't "System.DateTime.now" doesn't work in the andriod device is is there away to make it work?

using UnityEngine;
using System.Collections;
using System;

public class TimeMaster : MonoBehaviour {

    DateTime currentDate;
    DateTime oldDate;

    public string saveLocation;
    public static TimeMaster instance;

    // Use this for initialization
    void Awake () {

        instance = this;

        saveLocation = "LastSavedDate1";
    }

    public float CheckDate()
    {
        currentDate = System.DateTime.Now;
        string tempString = PlayerPrefs.GetString (saveLocation, "1");

        long tempLong = Convert.ToInt64 (tempString);

        DateTime oldDate = DateTime.FromBinary (tempLong);
        print ("oldDate : " + oldDate);

        TimeSpan difference = currentDate.Subtract (oldDate);
        print ("difference :" + difference);

        return(float)difference.TotalSeconds;

    }

    public void SaveDate ()
    {
        PlayerPrefs.SetString (saveLocation, System.DateTime.Now.ToBinary ().ToString ());
        print ("saving this date to player prefs" + System.DateTime.Now);
    }
    // Update is called once per frame
    void Update () {

    }
}

其余的在关卡管理器脚本中

and the rest in the level manager script

if (PlayerPrefs.HasKey ("lifeTime"))
{    
            newLifeTime = PlayerPrefs.GetFloat ("lifeTime");
            if (CountAllLives) 
            {
                newLifeTime -= TimeMaster.instance.CheckDate ();
            }

} 

脚本的另一部分

void OnApplicationQuit()
{
    PlayerPrefs.SetInt ("PlayerLives",currentLives);
    PlayerPrefs.SetFloat ("lifeTime",newLifeTime);
    TimeMaster.instance.SaveDate ();
    print ("the count down is :" + newLifeTime);           
}

推荐答案

我创建了一个虚拟场景来实现您的功能,并且对于编辑器和android设备均能正常工作. 您需要确保的一件事是退出时调用Application.Quit();,因为在android设备上,OnApplicationQuit仅在您调用Application.Quit();时执行,这与编辑器不同,它是在用户停止播放模式时调用的.

I have created a dummy scene to implement your functionality and it worked fine for both editor and android device. One thing you need to make sure is to call Application.Quit(); while quitting, because in case of android devices the OnApplicationQuit executes only when you call Application.Quit(); unlike the editor were this is called when the user stops playmode.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html

这篇关于Android System.DateTime.now的Unity系统时间不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆