欢迎来到天天文库
浏览记录
ID:33286271
大小:605.90 KB
页数:12页
时间:2019-02-23
《unity3d gui 简单用户图形界面设计》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.Unity2d飞机大战项目讲授一、游戏场景 游戏场景我们采用2D的界面,如图,我们创建一个蓝天白云的平面场景,该场景垂直于摄像机并采取正交投影方式。如果对在Unity3D中创建2D场景尚存在疑惑的朋友可以参考文章《[Unity3D]Unity3D游戏开发之《经典打砖块》游戏的实现》和《Unity3D游戏开发之使用Unity3D开发2D游戏(一)》两篇文章。 我们这里在场景中添加GUIText对象来显示玩家得分等基本信息,脚本定义如下:usingUnityEngine;usingSystem.Collections;publicclas
2、sGameManager:MonoBehaviour{//玩家得分privateTransformGrade;//玩家生命privateTransformHP;//游戏结束privateTransformText;//玩家...privateGameObjectPlayer;voidStart(){//初始化界面Grade=transform.Find("Grade");HP=transform.Find("HP");Text=transform.Find("Text");Text.guiText.enabled=false;//获取玩家对象P
3、layer=GameObject.Find("Player");}voidUpdate(){if(Player!=null){//更新UIGrade.guiText.text="得分:"+Player.GetComponent().Grade.ToString();HP.guiText.text="生命:"+Player.GetComponent().MaxHP.ToString();}if(HP.guiText.text=="生命:0"){Text.guiText.enabled=true;//立即复活if(I
4、nput.GetKey(KeyCode.Y)){Application.LoadLevel("Main");}//退出if(Input.GetKey(KeyCode.N)){Application.Quit();}}}...} 二、玩家飞机 玩家飞机的创建和背景的创建时相同的,这里我们不再多说。玩家飞机需要完成移动、发射、碰撞的事件处理。为此我们编写下面的脚本:usingUnityEngine;usingSystem.Collections;publicclassPlayer:MonoBehaviour{//飞机的移动速度publicf
5、loatMoveSpeed;//飞机的最大生命值publicintMaxHP=100;//定义子弹对象publicGameObjectBullet;//定义子弹发射位置privateTransformBulletPosL;privateTransformBulletPosR;[HideInInspector]publicintGrade=0;voidStart(){BulletPosL=this.transform.Find("BulletPosL");BulletPosR=this.transform.Find("BulletPosR");}
6、voidUpdate(){if(Input.GetKey(KeyCode.A)){transform.Translate(Vector3.left*Time.deltaTime*(-MoveSpeed));...}if(Input.GetKey(KeyCode.D)){transform.Translate(Vector3.left*Time.deltaTime*MoveSpeed);}if(Input.GetKey(KeyCode.W)){transform.Translate(Vector3.forward*Time.deltaTime*(
7、-MoveSpeed));}if(Input.GetKey(KeyCode.S)){transform.Translate(Vector3.forward*Time.deltaTime*MoveSpeed);}//按下空格键,发射子弹if(Input.GetKey(KeyCode.Space)){Instantiate(Bullet,BulletPosL.transform.position,Quaternion.Euler(newVector3(0,1,0)));Instantiate(Bullet,BulletPosR.transform.
8、position,Quaternion.identity);}}#region减血publicvoidHit(intValue){if(MaxHP>0
此文档下载收益归作者所有