Unity-3d Day05 打地鼠小游戏
用GUI写了一个打地鼠的小游戏 hiahia
摄像机的脚本里加代码:
using UnityEngine; using System.Collections; public class MoleScript : MonoBehaviour { public GUISkin skin; public Texture2D bgImg; public int num; public Texture2D[] susliks = new Texture2D[12]; public Rect[] pos = new Rect[9]; public int id; public float time; public bool flag; // Use this for initialization void Start () { pos[0] = new Rect(150f, 170f, 100f, 100f); pos[1] = new Rect(300f, 170f, 100f, 100f); pos[2] = new Rect(450f, 170f, 100f, 100f); pos[3] = new Rect(150f, 270f, 100f, 100f); pos[4] = new Rect(300f, 270f, 100f, 100f); pos[5] = new Rect(450f, 270f, 100f, 100f); pos[6] = new Rect(150f, 370f, 100f, 100f); pos[7] = new Rect(300f, 370f, 100f, 100f); pos[8] = new Rect(450f, 370f, 100f, 100f); time = 0; flag = false; } // Update is called once per frame void Update () { } void OnGUI() { GUI.skin = skin; GUI.Label(new Rect(0, 0, Screen.width, Screen.height), bgImg); for (int i = 0; i < pos.Length; i++) { GUI.Label(pos[i], susliks[0]); } Game(); } //控制游戏流程 void Game() { time += Time.deltaTime; if (time > 2.5f) { //随机一个位置 id = Random.Range(0, 9); time = 0; num = 0; flag = false; } //判断出来还是回去 if (flag) { SusliksDown(pos[id]); } else { SusliksUp(pos[id]); } } //出地鼠 void SusliksUp(Rect position) { if (num < 11) { num += 1; } else { num = 11; } if (GUI.Button(position, susliks[num])) { flag = true; time = 2.2f; } } //地鼠下去 void SusliksDown(Rect position) { print(num); if (num > 0) { num -= 2; } else { num = 0; } GUI.Button(position, susliks[num]); } }
效果图:地鼠什么的都是找的图片