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



我的成就随着生存时间的推移而增加。我用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) => {
});
}
}
}

在成就上增加的存活时间远远超过了应有的时间。

有什么建议吗?谢谢

试试看,如果不起作用,告诉我调试给了你什么。

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

您尝试过使用Time.deltaTime吗?或者将代码块放入-

void FixedUpdate() { 
//code 
}

最新更新