您的位置:首页 > 移动开发 > Unity3D

unity中使用GUI按钮实现简单的猜拳小游戏:

2015-02-01 23:45 507 查看
GUI的学习应用:

使用GUI按钮实现简单的猜拳小游戏:

using UnityEngine;
using System.Collections;

public class GUISelect : MonoBehaviour {

    public Texture2D[] t_Arrary;
    public int user;
    public int computer;

    public Texture2D pk;

    public Rect rect1=new Rect(100,100,450,250);
    public Rect rect2=new Rect(100,350,450,250);

    void Start () {
        user = 0;
        computer = 0;
    }
    

    void Update () {
    
    }
    void OnGUI(){

        rect1=GUI.Window (0,rect1,window1,"user");
        rect2=GUI.Window (1,rect2,window2,"pk");

    }

    void window1(int windowID){
        if (GUI.Button (new Rect (30, 30, 120, 120), t_Arrary[0])) {
            user=0;
            Ran();
        }
        if (GUI.Button (new Rect (160, 30, 120, 120), t_Arrary[1])) {
            user=1;
            Ran();
        }
        if (GUI.Button (new Rect (290, 30, 120, 120), t_Arrary[2])) {
            user=2;
            Ran();
        }

        GUI.DragWindow (new Rect(0,0,10000,10000));
    }

    void window2(int windowID){
        GUI.Box (new Rect(30,30,120,120),t_Arrary[computer]);//computeer
        GUI.Box (new Rect(160,30,120,120),pk);
        GUI.Box (new Rect(290,30,120,120),t_Arrary[user]);//user

        GUI.DragWindow (new Rect(0,0,10000,10000));//使按钮框可以拖动
    }

    void Ran(){

        computer = Random.Range (0,3);
        panding ();

    }
    void panding(){
        if(user-computer==1 || user-computer==-2){
            Debug.Log("You shu le!");
            return;
        }
        if(user-computer==0){
            Debug.Log("ping ju");
            return;
        }
        if(user-computer==-1 || user-computer==2){
            Debug.Log("You win");
            return;
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gui c# unity 脚本