直接上代码啦:
先搞一个老鼠类 模型一定要先构思好
using UnityEngine; using System.Collections; public class Mouse { //老鼠所在的位置 public int x; public int y; //当前地鼠是否是随机到的位置 public bool isCurrentRandom = false; //记录地鼠对象的生成时间 public float createTime; //记录地鼠对象的消失速度 public int level = 30; //求出当前的地鼠应该哪第几张图片 public int Index() { if (!isCurrentRandom) return 0; int result = (int)((Time.time - createTime) * level) % 27; //当result = 0,1,2,3,4,5,6,7,8,9,10,11,12,13时,result不变 //当result = 14,15,16,17,18,19,20,21,22,23,24,25,26时, //result = 13,12,11,10,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1 int count = MouseManager.GetInstance ().textureList.Length; if (result < count) { isAscending = true; } else { isAscending = false; result = (count * 2 - 1) - result; } return result; } public bool isAscending = true; public Mouse() { x = 0; y = 0; isCurrentRandom = false; } public Mouse(int px,int py) { x = px; y = py; } //重置地鼠的状态... public void Reset(bool random) { createTime = Time.time; isCurrentRandom = random; } }
老鼠模型的单例模式:
using UnityEngine; using System.Collections; public class MouseManager { //单例 private MouseManager() { } static MouseManager s_MouseManager = null; public static MouseManager GetInstance() { if (null == s_MouseManager) { s_MouseManager = new MouseManager (); } return s_MouseManager; } //老鼠运动的所有图片 public Texture2D[] textureList; //老鼠图片的宽度 private float texWidth = 75f; //老鼠图片的高度 private float texHeight = 75f; //是否有老鼠正在运动 public bool hasMouse = false; //获取一个老鼠对象应该放的位置... public Rect GetMouseRect(Mouse sender) { Rect rect = new Rect(); rect.width = texWidth; rect.height = texHeight; rect.x = texWidth * sender.y; rect.y = texHeight * sender.x; return rect; } //获取一个老鼠对象应该现实的图片... public Texture2D GetMouseTexture(Mouse sender) { int index = sender.Index (); if (index == 1 && !sender.isAscending) { hasMouse = false; sender.Reset (false); } return textureList [index]; } //获取所有老鼠对象应该摆放的区域 public Rect GetMouseArea(int row,int column) { Rect rect = new Rect (); rect.x = 20f; rect.y = 100f; rect.width = column * texWidth; rect.height = row * texHeight; //Debug.Log (rect); return rect; } }
gamemanager:
using UnityEngine; using System.Collections; public class GameManager : MonoBehaviour { //背景纹理图片 public Texture2D backgroundTex; //地鼠对象队列 private Mouse[] mouseList; //地鼠对象用的纹理队列 public Texture2D[] mouseTexList; //打中图片 public Texture2D[] hitTexList; public int level; //分数计算结果 public float score; //计时 private float time = 60f; private bool isHit; private int temp_index; private Texture2D temp_tex; private float temp_time = 0f; private int temp_count = 0; void OnGUI() { //添加纹理背景... DrawBackground (); //修正游戏级别 level = level >= 30 && level <= 110 ? level : 20; //获取单例对象 MouseManager mger = MouseManager.GetInstance (); GUILayout.BeginArea (mger.GetMouseArea(row,column)); //添加一个地鼠 for (int index = 0; index < mouseList.Length; index++) { //修改每个地鼠对象的级别 mouseList[index].level = level; Rect mouseRect = mger.GetMouseRect (mouseList [index]); Texture2D mouseTex = mger.GetMouseTexture (mouseList [index]); Mouse theMouse = mouseList[index]; if (GUI.Button (mouseRect, mouseTex)) { if(theMouse.isCurrentRandom){ score += 10; isHit = true; temp_index = index; } } } GUILayout.EndArea (); //重新设置鼠标纹理 ResetMouseTexture(); if(isHit){ //添加打中效果 print("打中效果"); DrawHitTexture(); } //设置文字颜色 GUI.color = Color.blue; //添加分数显示label DrawScoreLable(); //添加时间显示label DrawTimeLable(); } void DrawHitTexture(){ MouseManager mger = MouseManager.GetInstance (); Rect mouseRect = mger.GetMouseRect (mouseList [temp_index]); Rect rect = new Rect(mouseRect.x + 5f, mouseRect.y + 85f, mouseRect.width + 30f, mouseRect.height+ 30f); GUI.DrawTexture(rect, temp_tex); HitTime(); } //打击动画计时 void HitTime(){ if(Time.time - temp_time > 5*Time.deltaTime){ temp_count++; temp_tex = hitTexList[temp_count]; temp_time = Time.time; } if(temp_count == 2){ temp_count = 0; isHit = false; } } void DrawScoreLable() { Rect rect = new Rect(); rect.x = 0f; rect.y = 0f; rect.width = 60f; rect.height = 30; GUI.Label(rect, "分数:" + score.ToString()); } void DrawTimeLable() { Rect rect = new Rect(); rect.x = 0f; rect.y = 40f; rect.width = 60f; rect.height = 30; ShowTextTime(); GUI.Label(rect, "时间:" + time.ToString()); } //计时方法 void ShowTextTime() { time = 60 - (int)Time.time; } void JudgeTime() { if(time == 0f){ Time.timeScale = 0; } //每过5秒增加2的速度 if(Time.time % 5 < Time.deltaTime){ level += 2; } } private Texture2D mouseCurrentTexture; public Texture2D mouseRaiseTexture; public Texture2D mouseDownTexture; void ResetMouseTexture() { Screen.showCursor = false; Rect cursorRect = new Rect(); cursorRect.x = Input.mousePosition.x - 10f; cursorRect.y = Input.mousePosition.y + 20f; cursorRect.y = Screen.height - cursorRect.y; // cursorRect.width = mouseCurrentTexture.width; // cursorRect.height = mouseCurrentTexture.height; cursorRect.width = 75f; cursorRect.height = 75f; //绘制当前使用的鼠标纹理 GUI.DrawTexture(cursorRect, mouseCurrentTexture); } //绘制背景用的函数方法... void DrawBackground() { Rect rect = new Rect (0, 0, backgroundTex.width, backgroundTex.height); //在rect变量的范围内,绘制背景纹理... GUI.DrawTexture (rect, backgroundTex); } //行数 public int row = 1; //列数 public int column = 1; //游戏初始化方法... void GameInit() { row = row >= 4 && row <= 6 ? row : 4; column = column >= 4 && column <= 15 ? column : 4; //设置鼠标默认纹理 是抬起的图片 mouseCurrentTexture = mouseRaiseTexture; // temp_tex = hitTexList[0]; //将纹理图片的地址赋值给单例对象 MouseManager.GetInstance ().textureList = this.mouseTexList; MouseManager.GetInstance ().hasMouse = false; //通过(行数 * 列数)得到老鼠的个数 mouseList = new Mouse[row * column]; //通过2维数组来计算每个老鼠对象的位置... for (int x = 0; x < row; x++) { for (int y = 0; y < column; y++) { //创建一个老鼠对象 Mouse m = new Mouse (x, y); mouseList[x * column + y] = m; } } } //随机一个老鼠,特别厉害的老鼠 void RandomMouse() { if (MouseManager.GetInstance ().hasMouse) return; int x = Random .Range(0, row); int y = Random .Range(0, column); int index = x * column + y; Mouse m = mouseList [index]; //重置老鼠对象的值为真... m.Reset (true); //当有一个老鼠提起来之后,MouseManager的hasMouse设置为true MouseManager.GetInstance ().hasMouse = true; } // Use this for initialization void Start () { //游戏初始化... GameInit (); } // Update is called once per frame void Update () { RandomMouse (); if(Input.GetMouseButtonDown(0)){ mouseCurrentTexture = mouseDownTexture; } if(Input.GetMouseButtonUp(0)){ mouseCurrentTexture = mouseRaiseTexture; } JudgeTime(); } }
感觉上用了单例模式反倒麻烦了呢 可能是还没有写比较麻烦的游戏吧