GUI和完善飞机大战

欢迎来到 unity 学习 、 unity 培训 、 unity 企业培训 教育专区,这里有很多 U3D 资源 、 U3D 培训视频 U3D 教 程 、 U3D 常见问题 、 U3D 项目源码 , 我们致力于打造业内 unity3d 培训 、学习第一品牌

我们将了一些GUI的基本事件和用法:

  现在总结一下有几种啊

1、Label

Label (position : Rect, content : GUIContent) : void

Label (position : Rect, text : string, style : GUIStyle) : void

public Texture2D img;

void OnGUI() {

        GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");

        GUI.Label(new Rect(10,50,img.width,img.height),img);

}

2.Box:

public Texture2D img;

void OnGUI() {

        GUI.Box(new Rect(10, 10, 100, 20), "Hello World!");

        GUI.Box(new Rect(10,50,img.width,img.height),img);

}

3.Button:

public Texture2D img;

 

void OnGUI() {

        GUI.Button(new Rect(10, 10, 100, 20), "Hello World!");

        GUI.Button(new Rect(10,50,img.width,img.height),img);

}

GUI.Button(new Rect(10, 80, 150, 20), new GUIContent("我有提示", "恭喜你中奖了!"));

// 在旁边的位置显示提示信息。

GUI.Label(new Rect(130, 40, 150, 40), GUI.tooltip);

 

//按钮上面有图片

GUI.Button(new Rect(10, 180, 150, 20), new GUIContent("我有提示", img));

4.RepeatButton

5、TextField 

string str="Hello World"

str = GUI.TextField(new Rect(10, 10, 200, 20), str);

str = GUI.TextField(new Rect(10, 10, 200, 20), str, 25);

6 PasswordField


"*"[0]等价于'*'

string str= "请输入密码:";

str= GUI.PasswordField(new Rect(10, 10, 200, 20), str, "*"[0]);

str= GUI.PasswordField(new Rect(10, 10, 200, 20), str, "*"[0],10);

7.TextArea

然后用把这写东西运用到了飞机大战中,可以显示分数和生命值

在空对象上加了如下代码:

public Player player;

public int lifetime=0;

public int scroce=0;

public static GameMange instance;

// Use this for initialization

void Start () {

instance = this;

player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();

//find找的那个是标签,getcomponent找的是那个类啊。

 

}

// Update is called once per frame

void Update () {

}

void OnGUI() {

lifetime = player.a;

GUI.Label(new Rect(20,30,100,20),"生命值为:"+lifetime);

GUI.Label(new Rect(20, 70, 100, 20), "分数:"+scroce );

}

然后又在那个触发器上面加上一行代码:

  GameMange.instance.scroce++;

最后又在另一个空对象上加上下面的代码完成敌机的无限个啊 

public GameObject [] enemy;

int number=5;

// Use this for initialization

void Start () {

StartCoroutine ("Spwn");

}

// Update is called once per frame

void Update () {

}

IEnumerator  Spwn(){

yield return  new WaitForSeconds (3.0f);//开始等待3

while (true) {

while (number>0) {

GameObject go = enemy [Random .Range (0, enemy .Length)];

Vector3 pos = new Vector3 (Random.Range (-10f,10f),21.55587f,-28f);

//控制敌机随机出现的范围

GameObject.Instantiate (go,pos,transform .rotation );

yield return new WaitForSeconds (2.0f);//每隔两秒克隆一个啊

number--;

}

number =5;

yield return new WaitForSeconds (4.0f);//每隔4秒开始下一波

 

}

 

 

}

这里面用到了数组,协程 和克隆的技术。

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值