Unity5.x的GUIText被UI Text所取代

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


}

我的例子:

 

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Unity5.x升级到Unity2017.x的过程中常见的异常问题,可以参考整理的经验共享文档。另外,如果您需要下载Unity5.x的32位版本,可以通过修改下载链接中的数字64为32来获取相应的安装程序。此外,您还可以关注Unity官方的GitHub页面,那里会发布一些示例工程以及处于开发阶段的新功能,可以帮助您了解Unity的发展方向并提升开发技能。希望这些信息对您有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Unity4.x/5.x/2017.x升级差异总结](https://blog.csdn.net/weixin_34206899/article/details/86085382)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [最全UnityHub国际版下载链接Unity2023~2017各版本+Unity5.x【间歇性更新】](https://blog.csdn.net/qq_36829186/article/details/123847081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [unity 5.x从入门到精通_Unity学习中值得收藏的学习资料(书籍)与博客](https://blog.csdn.net/u013712343/article/details/123378759)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值