Unity High_Score徽章C#

时间:2016-07-07 11:59:43

标签: c# unity3d unity5

你好我想创建像飞鸟一样的徽章,我把图像放在像游戏对象这样的UI图像中。但它只显示了3张图片中的一张图片请帮帮忙! HighScore有效,当我获得更高的分数时会重写,但不知道为什么图像不会改变:(

对不起英文不好

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等等。

Unity

我更新了代码,但仍然无法正常工作

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

        }
    }

1 个答案:

答案 0 :(得分:0)

原来以下的东西不足以让你的东西工作,你需要改变medale.SetActive(false);至 medale.SetActive(true); ,因为这是您的奖牌所在的父对象。

您没有在任何地方设置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);
            }

另外,为什么要在开始时将它们设置为活动状态?