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();
}
}
- Unity5.x的GUIText被UI Text所取代--增加头文件UnityEngine.UI
- Unity5.x的GUIText被UI Text所取代--增加头文件UnityEngine.UI
- Unity5.x的GUIText被UI Text所取代
- Unity5 UI.Text 引发Canvas.SendWillRenderCanvases很卡的问题
- Unity3d 加载UnityEngine.UI.dll失败的解决方案(2017.2)
- Cocos2d-x 找不到UI控件所在的头文件(解决方案)
- 我所理解的Cocos2d-x UI树
- Qt Ui 头文件研究
- Qt Ui 头文件研究
- Qt Ui 头文件研究
- QT(2):添加ui文件及构建ui文件的头文件说明
- 关于UnityEngine.UI.Button.ButtonClickedEvent事件列表
- QT的学习之路 Ui 头文件研究
- cocos使用UI控件需要包含的头文件
- QT UI文件生成头文件
- QT UI文件生成头文件
- cocos2d-x里的UI
- ui文件的使用
- Eclipse之properties文件编辑器中将Unicode码显示为翻译后的字符
- PHP请求远程地址设置超时时间
- 广告投放方式:CPC、CPM、CPA、CPS、CPT
- 视频编解码原理
- 将java project 打包成跨平台的jar包
- Unity5.x的GUIText被UI Text所取代--增加头文件UnityEngine.UI
- 安卓常用框架系列之butterknife
- Redis集群方案
- GitHub上深度学习Caffe开源程序代码
- 获取U盘序列号
- 欧拉道路与欧拉回路算法
- yii2框架-yii2的web应用属性(六)
- iOS如何获得年月日时分秒具体的值
- IT运维之Linux服务器监控方案