在Unity中增加Google Play游戏中的成就问题

问题描述 投票:0回答:1

我有一个成就,随着存活时间的增加而增加。我用Time.time来计算存活的时间。当我死亡时,我用Time.time的值来增加成就,以增加存活的秒数,但它增加的时间远远超过了它应该增加的时间。我的代码

using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;

    public class Timer : MonoBehaviour
    {
        private float StartTime;
        public static float TimerControl;

        void Start()
        {
            StartTime = Time.time;
        }

        void Update()
        {
            //The player is alive
            if (PlayerController.dead == false)
            {
                TimerControl = Time.time - StartTime;
            }

            //The player is dead
            if (PlayerController.dead == true)
            {
                PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(TimerControl), (bool success) => {

                });
            }
        }
    }

成就中增加的存活时间比它应该增加的多得多。

有什么建议吗?谢谢!

c# unity3d google-play-games achievements
1个回答
0
投票

试试这个,如果不成功,告诉我调试结果是什么。

using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
public class Timer : MonoBehaviour
{
    void Update()
    {
        //The player is dead
        if (PlayerController.dead == true)
        {
        Debug.Log("Time : " + Time.time);
        PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(Time.time), (bool success) => {
            });
        }
    }
}

编辑:好吧,我想我知道发生了什么事,你是在更新循环中递增的,所以你一定是实现了很多次。

bool recorded = false;
// ...
        //The player is dead
        if (PlayerController.dead == true && !recorded)
        {
            PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(TimerControl), (bool success) => {

            });
        recorded = true;
        }

0
投票

你有没有试过使用 Time.deltaTime? "或者把这段代码放在-

void FixedUpdate() { 
    //code 
}
© www.soinside.com 2019 - 2024. All rights reserved.