Unity5.x的GUIText被UI Text所取代--增加头文件UnityEngine.UI

来源:互联网 发布:vb中cancel是什么意思 编辑:程序博客网 时间:2024/06/05 14:56

using UnityEngine;

using System.Collections;


//使用UI->Text 必须要增加头文件 UnityEngine.UI;

using UnityEngine.UI;


public class GameManager : MonoBehaviour {


    public static GameManager Instance = null;
    int m_ammo = 100; //弹药数量
    int m_score = 0;//游戏得分
    static int m_hiscore = 0;//游戏最高得分


    Player m_player; //游戏主角


    //UI文字
    Text txt_ammo;
    Text txt_hiscore;
    Text txt_life;
    Text txt_score;


// Use this for initialization
void Start () {
        Instance = this;
        //获得主角
        m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();


        /* 

         书上的旧方法
         //获得设置的文字
         foreach (Transform t in this.transform.GetComponentsInChildren<Transform>())
         {
             if (t.name.CompareTo("txt_ammo") == 0)
             {
                 txt_ammo = t.GetComponent<GUIText>();


             }
             else if (t.name.CompareTo("txt_hiscore") == 0)
             {
                 txt_hiscore = t.GetComponent<GUIText>();
                 txt_hiscore.text = "High Score" + m_hiscore;
             }
             else if (t.name.CompareTo("txt_life") == 0)
             {
                 txt_life = t.GetComponent<GUIText>();
             }
             else if (t.name.CompareTo("txt_score") == 0)
             {
                 txt_score = t.GetComponent<GUIText>();              
             }
         }
        */

        //新方法
        //获得设置的UI文字
        //使用UI后,都会产生一个Canvas的父对象,所以要逐层查找物体(根据自己的项目表如上图)
        //增加UnityEngine。UI头文件后,就可以有效的使用GetComponent<Text>()来操作Text中要显示的文字
        txt_ammo = this.transform.FindChild("Canvas").FindChild("weapon").FindChild("txt_ammo").GetComponent<Text>();
        txt_hiscore = this.transform.FindChild("Canvas").FindChild("txt_hiscore").GetComponent<Text>();
        txt_hiscore.text = "High Score" + m_hiscore;
        txt_life = this.transform.FindChild("Canvas").FindChild("heath").FindChild("txt_life").GetComponent<Text>();
        txt_score = this.transform.FindChild("Canvas").FindChild("txt_hiscore").FindChild("txt_score").GetComponent<Text>();

       
    }

// Update is called once per frame
void Update () {

}


    void OnGUI()
    {
        if (m_player.m_life <= 0)
        {
            //居中显示文字
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            //改变文字大小
            GUI.skin.label.fontSize = 40;


            //显示game over 文字
            GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "Game Over");
            //设置按钮文字大小
            GUI.skin.label.fontSize = 30;
            //显示重新游戏按钮
            if (GUI.Button(new Rect(Screen.width * 0.5f - 150, Screen.height * 0.75f, 300, 40), "Try again"))
            {
                //重新读入当前关卡
                Application.LoadLevel(Application.loadedLevelName);
            }
        }
    }




    //更新分数
    public void SetScore(int score)
    {
        m_score = m_score + score;
        if (m_score > m_hiscore)
        {
            m_hiscore = m_score;
            //<color = yellow> 分数</color> 使用标记改变分数的颜色
            txt_score.text = "Score <color = yellow>" + m_score + "</color>";
            txt_hiscore.text = "High Score " + m_hiscore;
        }
    }


    //更新弹药
    public void SetAmmo(int ammo)
    {
        m_ammo = m_ammo - ammo;


        //如果弹药为负数。重新填充
        if (m_ammo < 0)
        {
            m_ammo = 100 - m_ammo;
        }
        txt_ammo.text = m_ammo.ToString() + "/100";
    }


    //更新生命
    public void SetLife(int life)
    {
        txt_life.text = life.ToString();
    }


}
1 0
原创粉丝点击