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

来源:互联网 发布:matlab 矩阵最小值 编辑:程序博客网 时间:2024/05/23 21:24

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 (3030120120), t_Arrary[0])) {
            user=0;
            Ran();
        }
        if (GUI.Button (new Rect (16030120120), t_Arrary[1])) {
            user=1;
            Ran();
        }
        if (GUI.Button (new Rect (29030120120), 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;
        }
    }
}



0 0
原创粉丝点击