Unity High_Score徽章C# [英] Unity High_Score Badges C#
问题描述
你好,我想创建像飘扬的小鸟那样的徽章,我将图像放在UI图像中,例如GameObjects.但它只显示3张图片中的一张.请帮忙!而且HighScore可以正常工作,当我获得更高的分数时,它将重写,但是不知道为什么图像不会改变:(
Hello I want to create badges like in flappy bird , I put images in UI images like GameObjects. But it show me only one image of 3 images Please Please Help Out! And HighScore works , it rewrites when I get Higher score , but don't know why images won't change :(
对不起,英语不好
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
private float score = 0f;
public Text Scoretext;
public MenuController deathmenu;
public GameObject IntroGUI, DeathGUI, Canvas;
public GameObject[] medals;
public GameObject medale;
void Start()
{
medale.SetActive(false);
foreach(GameObject m in medals)
{
m.SetActive(true);
}
}
void Update()
{
//handle back key in Windows Phone
if (Input.GetKeyDown(KeyCode.Escape))
Application.Quit();
if (GameStateManager.GameState == GameState.Intro)
{if (WasTouchedOrClicked())
{
GameStateManager.GameState = GameState.Playing;
IntroGUI.SetActive(false);
Canvas.SetActive(true);
}
}
else if (GameStateManager.GameState == GameState.Playing)
{
score += Time.deltaTime;
Scoretext.text = ((int)score).ToString();
if (PlayerPrefs.GetFloat("Highscore") < score)
PlayerPrefs.SetFloat("Highscore", score);
if(PlayerPrefs.GetFloat("Scoretext") > 0)
{
medals[0].SetActive(true);
}
else if
(PlayerPrefs.GetFloat("Scoretext") > 2)
{
medals[1].SetActive(true);
}
else if
(PlayerPrefs.GetFloat("Scoretext") > 5)
{
medals[2].SetActive(true);
}
medale.SetActive(true);
deathmenu.ToggleEndMenu(score);
}
}
在图像中,您可以看到我的GameObjects和其他内容.
In Image you can see my GameObjects and Etc.
我更新了代码,但仍然无法正常工作
I updated the code but still doesnt work
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
private float score = 0f;
public Text Scoretext;
public MenuController deathmenu;
public GameObject IntroGUI, DeathGUI, Canvas;
public GameObject[] medals;
public GameObject medale;
void Start()
{
medale.SetActive(true);
}
void Update()
{
//handle back key in Windows Phone
if (Input.GetKeyDown(KeyCode.Escape))
Application.Quit();
if (GameStateManager.GameState == GameState.Intro)
{
if (WasTouchedOrClicked())
{
GameStateManager.GameState = GameState.Playing;
IntroGUI.SetActive(false);
Canvas.SetActive(true);
medale.SetActive(false);
}
}
else if (GameStateManager.GameState == GameState.Playing)
{
score += Time.deltaTime;
Scoretext.text = ((int)score).ToString();
if (PlayerPrefs.GetFloat("Highscore") < score)
PlayerPrefs.SetFloat("Highscore", score);
deathmenu.ToggleEndMenu(score);
}
}
void FixedUpdate()
{
//just jump up and down on intro screen
if (GameStateManager.GameState == GameState.Intro)
{
}
else if
(GameStateManager.GameState == GameState.Playing || GameStateManager.GameState == GameState.Dead)
{
}
}
bool WasTouchedOrClicked()
{
if (Input.GetButtonUp("Jump") || Input.GetMouseButtonDown(0) ||
(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
return true;
else
return false;
}
void OnCollisionEnter2D(Collision2D col)
{
if (GameStateManager.GameState == GameState.Playing)
{
if (col.gameObject.tag == "CARS")
{
PlayerDies();
}
}
}
void PlayerDies()
{
GameStateManager.GameState = GameState.Dead;
if (PlayerPrefs.GetFloat("Scoretext") > 0)
{
medals[0].SetActive(true);
}
else if
(PlayerPrefs.GetFloat("Scoretext") > 2)
{
medals[1].SetActive(true);
}
else if
(PlayerPrefs.GetFloat("Scoretext") > 5)
{
medals[2].SetActive(true);
}
}
}
推荐答案
要证明下面的内容不足以使您的内容正常工作,您需要更改badgee.SetActive(false);.到 medale.SetActive(true); ,因为这是您的奖牌所在的父对象.
Turns out the stuff below is not enough to let your stuff work, you need to change medale.SetActive(false); to medale.SetActive(true); because this is the parent object which your medals are in.
您没有在任何地方设置计分文字",因此请查看分数而不是计分文字".
You don't set the Scoretext anywhere so look at the score instead of the scoretext.
所以您得到:
score += Time.deltaTime;
Scoretext.text = ((int)score).ToString();
if (PlayerPrefs.GetFloat("Highscore") < score)
PlayerPrefs.SetFloat("Highscore", score);
if(score > 0)
{
medals[0].SetActive(true);
}
else if
(score > 2)
{
medals[1].SetActive(true);
}
else if
(score > 5)
{
medals[2].SetActive(true);
}
还有,为什么要一开始将它们设置为活动状态?
Also, why are you setting them active at the start?
这篇关于Unity High_Score徽章C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!