![](/img/trans.png)
[英]Is there a way and how can i make the program increment the obstacleSpeed by one every time the score increments by one?
[英]How can I update my GUI score one point at a time?
如果一名玩家在我的游戲中獲得X分數用於做某事,我希望記分牌能夠更新顯示0到X中的每個數字。 我正在尋找一個模擬風格的評分系統,就像那些可以觀察數字變化的舊時鍾,除了變化很快就會發生,因為可以同時添加數百個點。
目前,當獲得積分時,X點數會立即更新:
/****************************** Platform Collision ***************************/
void OnCollisionEnter2D(Collision2D coll)
{
foreach(ContactPoint2D contact in coll.contacts)
{
newPlayerHeight = transform.position.y;
// Don't count the first jump
if(newPlayerHeight < 0){
newPlayerHeight = 0;
}
// If the player jumps down, don't reduce points
// Add a tolerance for variable collision positions on same platform
if(newPlayerHeight < oldPlayerHeight + -0.05f){
newPlayerHeight = oldPlayerHeight;
}
// Send the height to the Score class
Score.SP.updateScore(newPlayerHeight);
oldPlayerHeight = newPlayerHeight;
}
}
/******************************* Score class *********************************/
public void updateScore (float newScore)
{
// Display GUI score
score = (int)(newScore * 76);
guiText.text = "Score" + score;
}
我用一個for循環搞砸了試圖實現這個但是無處接近。
我剛剛創建了這個代碼,它解決了你的問題。 我目前正在自己的游戲中使用它。
ScoreManager
類的主要思想是保持得分並在每一幀更新它。 它使用Stack
來跟蹤要添加的分數,這樣我們就沒有增加分數的幻數。
因此,當玩家獲得新點時,只需調用addScore()
函數,然后ScoreManager
類將自行處理更新。
它會有一個始終運行的線程,它會newScore
位從currentScore
增加到newScore
,這樣就可以像你想要的那樣由用戶觀察到這種變化。
添加了這個主題是為了減少你在發布的另一個問題中遇到的“滯后” : 為什么當我調用方法時我的游戲會滯后?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
public class ScoreManager : MonoBehaviour
{
public GUIText score;
private int currentScore = 0;
public int scoreToUpdate = 0;
private Stack<int> stack;
private Thread t1;
private bool isDone = false;
void Awake()
{
stack = new Stack<int>();
t1 = new Thread(updateScore);
t1.Start();
}
private void updateScore()
{
while(true)
{
if(stack.Count > 0)
{
int newScore = stack.Pop() + currentScore;
for(int i = currentScore; i <= newScore; i++)
{
scoreToUpdate = i;
Thread.Sleep(100); // Change this number if it is too slow.
}
currentScore = scoreToUpdate;
}
if(isDone)
return;
}
}
void Update()
{
score.text = scoreToUpdate + "";
}
public void addScore(int point)
{
stack.Push(point);
}
public void OnApplicationQuit()
{
isDone = true;
t1.Abort();
}
}
我計划將此代碼轉換為Singleton,以便在我的游戲中只有一個此類的實例。 我強烈建議你這樣做。
你嘗試過使用Unity協程嗎? 它可能更容易實現,啟動協程並讓分數的每次增加之間的幀更新將是一個簡單的方法。
協程允許訪問yield return null
這將等到當前幀結束以繼續該函數。
...
StartCoroutine(AddtoScore(ValuetoAdd));
...
...
public IEnumerator AddtoScore(int value) {
for(int i = 0; i < value; i++) {
currentScore++;
guiText.text = "Score: " + currentScore;
yield return null;
}
}
...
然而,這樣做的缺點是,如果你在添加點時再次得分,可能會產生一些圖形問題。
另一方面,您可以將它放在更新功能中相同的樣式。 Unity每幀調用一次Update函數。
...
void Update(){
if(currentScore < scoreToUpdate) {
currentScore++;
guiText.text = "Score: " + currentScore;
}
}
...
但是,這兩種方法都要求它們所在的類繼承自monodevelop:
public class Scoring : MonoBehaviour
快速而骯臟的解決方案,使用HOTween(免費和開源)。 請注意,它不會顯示所有數字,只能在transitionTime
秒內顯示所有數字。
using Holoville.HOTween;
...
int displayScore = 0;
public void updateScore (int newScore)
{
if (oldScore != newScore)
{
float transitionTime = .2f;
HOTween.To(this, transitionTime, "displayScore", newScore);
}
}
public void Update() {
guiText.text = "" + displayScore
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.