我有一個“玩家得分:100 分”文本,計時器從 50 秒計數到 0。我想讓它每 5 秒過去,從“玩家得分”中洗掉 10 分。所以每 5 秒就會有 10、90、80 等等。我怎樣才能做到這一點?我在 C# 中使用 Unity 和編碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreCountdownn : MonoBehaviour
{
float scoreReductionTime = 5.0f;
int scoreReductionAmount = 10;
int maxScore = 100;
int scoreReduction = 5;
float timeUntilScoreReduction;
int currentScore;
void Start()
{
// Set the time until the next score reduction
timeUntilScoreReduction = scoreReductionTime;
// Set the current score to the max score
currentScore = maxScore;
}
// Can be Update or FixedUpdate depending on your goal
void Update()
{
// Reduce the time until score reduction
// by the time interval that has just passed
timeUntilScoreReduction -= Time.deltaTime;
// If the timeUntilScoreReduction reaches 0 or below,
// the score is reduced and the timeUntilScoreReduction
// is reset to the next time at which score reduction occurs
if (timeUntilScoreReduction <= 0.0f)
{
currentScore -= scoreReduction;
timeUntilScoreReduction = scoreReductionTime;
ScoreCountdown.text = currentScore;
}
Debug.Log(timeUntilScoreReduction);
Debug.Log(currentScore);
}
}
uj5u.com熱心網友回復:
在您的UpdateorFixedUpdate函式中,您可以訪問Time.deltaTime. Update它是自上次呼叫orFixedUpdate函式以來的時間差(以秒為單位) 。因此,您可以通過將每次重復之間的這些時間間隔相加來跟蹤已經過去了多少時間。
由于您沒有包含任何代碼,因此我將嘗試為您提供一些通用代碼,您可以查看以供參考。
編輯:編輯原始問題以提供更多資訊時,代碼已更改。
using UnityEngine;
using UnityEngine.UI;
public class ExampleClass : MonoBehaviour
{
float scoreReductionTime = 5.0f;
int scoreReductionAmount = 10;
int maxScore = 100;
int scoreReduction = 5;
float timeUntilScoreReduction;
int currentScore;
// You can drag your UI gameobject from the hierarchy
// to this field in the editor to link them.
// That way Unity knows what the gameobject is without
// you having to specify it here in the code
public GameObject ScoreCountdown;
void Start()
{
// Set the time until the next score reduction
timeUntilScoreReduction = scoreReductionTime;
// Set the current score to the max score
currentScore = maxScore;
// The gameobject has components, one of which is the text component.
// You have to access this component and set its text value
// to the desired string. Since the currentScore is an integer,
// and text is a string, you have to make a string of the integer.
ScoreCountdown.GetComponent<Text>().text = currentScore.ToString();
}
// Can be Update or FixedUpdate depending on your goal
void Update()
{
// Reduce the time until score reduction
// by the time interval that has just passed
timeUntilScoreReduction -= Time.deltaTime;
// If the timeUntilScoreReduction reaches 0 or below,
// the score is reduced and the timeUntilScoreReduction
// is reset to the next time at which score reduction occurs
if (timeUntilScoreReduction <= 0.0f)
{
currentScore -= scoreReduction;
timeUntilScoreReduction = scoreReductionTime;
ScoreCountdown.GetComponent<Text>().text = currentScore.ToString();
}
}
}
請注意,在您的問題中,您說分數應該每 5 秒減少 10,但是您給出了分數減少為 95、90、85 的示例……我選擇考慮代碼中的第一個陳述句,第二個陳述句中的分數減少了 10 分而不是 5 分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/440949.html
