unity3d游戏开发之GUI基础.doc_第1页
已阅读1页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

今天为之前做的飞机大战添加显示生命值和分数的功能,用到了GUI,下面我来简单梳理一下GUI的知识点 /文本 /GUI.Label(new Rect(10,10,100,20),GUI练习 ); /图片 /GUI.Label(new Rect(10,40,img.width,img.height), img); /盒子边框 /GUI.Box(new Rect(10, 10, 100, 20), GUI练习); GUI.Box(new Rect(10, 40, img.width, img.height), img); /有点击效果 / GUI.Button(new Rect(10, 10, 100, 20), GUI练习); / GUI.Button(new Rect(10, 40, img.width, img.height), img);/图片效果同上 /点击效果有提示 /GUI.Button(new Rect(10, 10, 100, 20), new GUIContent(GUI练习,GG) ); /GUI.Label (new Rect (130,30,50,20),GUI.tooltip ); /按钮上面有图片 / GUI .Button (new Rect (10,10,100,20),new GUIContent(GUI练习,img2) ); /RepeatButton双击按钮 / GUI.RepeatButton(new Rect(10, 10, 100, 20), Hello World!); / GUI.RepeatButton(new Rect(10, 50, img.width, img.height), img); /点击效果 / GUI.RepeatButton(new Rect(10, 10, 100, 20), new GUIContent(GUI练习, GUI); / GUI.Label(new Rect(130,30,50,20),GUI .tooltip ); /按钮上面有图片 / GUI.RepeatButton(new Rect(10,10,100,20),new GUIContent(GUI练习,img); /TextField输入框 string类型 / stc = GUI.TextField(new Rect(10,10,100,20),stc ); / stc = GUI.TextField(new Rect (10,10,100,50),stc,10);/限制只能输入十个字符 / PasswordField 密码框 *0等价于* 单引号内的*可以更改为其他字符 / stc = GUI.PasswordField(new Rect(10, 10, 100, 20), stc, *); / TextArea/多行输入框 /stc = GUI.TextArea(new Rect(10, 10, 200, 200), stc); 暂时就这些下面就来介绍项目中用到的GUI知识吧!先来看效果图 下面我们就来看看如何实现 我们要新建一个脚本来管理它们GameManage.cs 1. using UnityEngine;2. using System.Collections;3.4. public class GameManage : MonoBehaviour5. 6.7. public int lifeTime = 0;/我方生命值8. public int score = 0;/分数9.10. public static GameManage instance;11.12. public Player player;13.14. void Start()15. 16. instance = this;17. player = GameObject.FindGameObjectWithTag(Player).GetComponent();18. 19.20.21. void Update()22. 23.24. 25.26. void OnGUI()27. 28. lifeTime = player.lifeTime;29. GUI.Label(new Rect(20, 30, 100, 20), 生命值为: + lifeTime);30.31. GUI.Label(new Rect(20, 70, 100, 20), 分数: + score);32. 33. 34. 然后我们要获取Player的生命值和分数 在Player.cs里触碰事件里加入 GameManage.instance.lifeTime =lifeTime;/获取生命值 在Enemy.cs里触碰事件里加入 GameManage.instance.score+; 这样就可以啦! 下面在来介绍一下如何让敌机每隔几秒更新出新的飞机 创建新的脚本EnemySpawn.cs 1. using UnityEngine;2. using System.Collections;3.4. public class EnemySpawn : MonoBehaviour 5.6. /1、对象数组2、协程7. public GameObject enemy;8.9. int number = 5;10. void Start () 11. StartCoroutine(Spawn);/开启一个协同程序Spawn12. / StartCoroutine(Spawn();/这个方法也可以13. 14. 15. 16. void Update () 17. 18. 19.20. IEnumerator Spawn() 21. yield return new WaitForSeconds(1f);/开始等待1秒22. while (true)23. 24. while (number 0)25. 26. GameObject go = enemyRandom.Range(0, enemy.Length);27. Vector3 poss = new Vector3(Random.Range(-40, 30), 96f, 40f);28. GameObject.Instantiate(go, poss, transform.rotation);29.30. yield return

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论