一. 创建场景
更改摄影机类型。点击camera面板,更改projection
素材箱内新建一个文件夹 改名为Sprites
导入图片素材到Sprites文件夹中
选中素材分别在对应面板更改texture type为2D 然后apply
将图片ground拖入场景中,根据图片的分辨率如下图:
在game面板下的free aspect下添加跟上图一致的分辨率,标签命名为dadishu
适当调大scale的值
调整相机大小 选中camera,在inspector面板中调整size为3.7
保存场景:在素材箱内新建文件夹Scenes,保存场景S1在Scenes中。
选中ground,添加layer,取名为Ground,再次选中ground,更改sorting
layer为Ground。
将Hole、Mole添加到场景中,同理,添加layer,更改sorting layer。
将Mole_Beaten添加到场景中,直接更改sorting layer为Mole.
新建一个预制体文件夹Prefabs,将Hole、Mole、Mole_Beaten拖入,然后将场景中的删除。
二.生成洞口
新建文件夹Script,在文件夹中新建脚本GameController,编辑脚本,把脚本拖给ground,最后把预制体Hole拖到Hole
Obj。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public struct Hole {
public bool isAppear;
public int holeX;
public int holeY;
}
public Hole[]holes;
private float intervalPosX = 2,
intervalPosY = 1;
public GameObject holeObj;
void Start ()
{
InitMap();
}
private void InitMap()
{
Vector2 originalPos = new Vector2(-2, -2);
holes = new
Hole[9];
for(int i = 0; i < 3;i++)
{
for(int j = 0; j < 3; j++)
{
holes[i * 3 + j] = new Hole();
holes[i * 3 + j].holeX = (int)(originalPos.x + j* intervalPosX);
holes[i * 3 + j].holeY = (int)(originalPos.y + i* intervalPosY);
oles[i * 3 + j].isAppear = false;
Instantiate(holeObj, new Vector3(holes[i * 3+ j].holeX, holes[i * 3 + j].holeY, 0), Quaternion.identity); ;
}
}
}
void Update
() {
}
}
运行,生成洞口
三.地鼠随机生成和销毁
编写代码控制地鼠生成的频率,在GameController中添加代码:
private void MoleAppear()
{
Debug.Log(“MoleAppear”);
}
在start中添加代码:
InvokeRepeating(“MoleAppear”, 3f, 0.5f);
在GameController中添加代码实现地鼠随机产生:
public GameObject moleObj;
private void MoleAppear()
{
int id =UnityEngine.Random.Range(0,9);
while
(holes[id].isAppear==true)
{ id = UnityEngine.Random.Range(0,9);
}
Instantiate(moleObj, new Vector3(holes[id].holeX, holes[id].holeY,
0),Quaternion.identity);
holes[id].isAppear = true;
Debug.Log(“MoleAppear”);
}
控制地鼠在规定时间内的销毁。在GameController中添加代码:
在Hole中添加:public GameObject mole;
更改代码:holes[id].mole=Instantiate(moleObj, new
Vector3(holes[id].holeX, holes[id].holeY, 0),Quaternion.identity);
新增代码:private void CleanHoleState()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (holes[i*3+j].mole==null) {
holes[i * 3 + j].isAppear =
false;
}
}
}
}
在update中调用CleanHoleState函数:
void Update
() {
CleanHoleState();
}
新增一个脚本命名为Mole:
void Start
()
{
Destroy(gameObject, 3f);
}
选中ground,在其添加的脚本上添加预制体
四.打击地鼠
实现鼠标左键点击地鼠,使它变成打击后地鼠的样子。
首先给预制体mole添加碰撞体polygon collider2D
编辑Mole脚本:
public GameObject beatenMole;
void
OnMouseDown()
{
Instantiate(beatenMole,gameObject.transform.position,
Quaternion.identity);
Destroy(gameObject);
Debug.Log(“OnMouseDown”);
}
将预制体Mole_Beaten拖到脚本中
实现被打击后的地鼠一秒钟消失
新建脚本BeatenMole编写代码:
void Start () {
Destroy(gameObject, 1f);
}
把脚本BeatenMole给预制体Mole_Beaten
再次编辑Mole代码
public int id;
public GameController gameController;
在start中添加
gameController =
GameObject.FindObjectOfType();
更改代码:
gameController.holes[id].mole=Instantiate(beatenMole,gameObject.transform.position,
Quaternion.identity);
GameController代码:在MoleAppear函数中添加
holes[id].mole.GetComponent().id
= id;
选中canvas设置如下图:
新建脚本Timer,编写代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public Text
timerText;
public float time = 30.0f;
private bool canCountDown = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void
Update()
{
if(canCountDown == true)
{
time -= Time.deltaTime;
timerText.text = “Time:” +time.ToString(“F1”);
Debug.Log(time);
}
}
public void CountDown(bool countDown)
{
this.canCountDown
= countDown;
}
}
常见一个空物体名为Timer把Timer脚本拖给这个空物体,然后将TimeCountDownText拖给Timer text。
右键Canvas新建ui中的text改名为ScoreText
在Timer脚本中声名:public Text timerText;
添加Mole的代码
using UnityEngine.UI;
定义:public Text ScoreText;
public static int score = 0;
Start函数中添加:ScoreText = GameObject.Find(“ScoreText”).GetComponent
在OnMouseDown函数中添加:
score++;
ScoreText.text =“Score:” + score;
运行如图
五.游戏结束
在GameController脚本中添加update函数的代码:
if (timer.time < 0)
{
GameOver();
}
private void GameOver()
{
timer.CountDown(false);
CancelInvoke();
}
在Timer脚本中的CountDown函数中添加代码:
if(canCountDown==false)
{
time = 0;
timerText.text=“Game Over!”;}
六.更改鼠标图标为锤子
创建一个image名为HammerImage,更改其的Source Image为Hammer。
新建一个ChangeCursor脚本,拖给HammerImage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using
UnityEngine.UI;
public class ChangeCursor : MonoBehaviour {
public Sprite
normalCursor;
public Sprite
hitCursor;
public Image
hammerImage;
void Start () {
Cursor.visible
= false;
}
void Update () {
if(Input.GetMouseButton(0))
{
hammerImage.sprite = hitCursor;
}
else
{
hammerImage.sprite = normalCursor;
}
hammerImage.rectTransform.position =
Input.mousePosition;
}
}
按如图设置
七.添加音效
新建一个文件夹Audios,将音效素材导入
选中Mole在它的inspector面板添加audio source组件,将appear音效拖入,如图设置
同理给Mole_Beaten添加audio source组件
八.添加重新开始按钮
右键canvas创建一个button改名为RestartButton,调整位置,编辑text的内容Restart。
新建一个Restart脚本,把脚本拖给RestartButton
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Restart : MonoBehaviour {
void Start () {
}
void Update () {
}
public void RestartGame()
{
SceneManager.LoadScene(“S1”);
}
}
如图操作: