一、新建一个项目
打开unity软件——点击new——填写项目名称,改为2D模式,选择保存位置,点击创建项目。
二、导入资源、添加场景
- 新建一个文件夹,命名为sprites,把图片导入进去
- 新建一个audio文件夹,把声音导入进去
- 把ground图片和hole图片的Texture Type改为Sprite(2D and UI)后拉入场景中,如果场景中不显示hole的图片,证明hole的图片在ground图片的下一层,如果ground图片中 Sprite Render组件中Order in Layer为0,则hole图片为1。
或者ground图片的Layer中添加一个Sorting Layers,为Ground,把Sprite Render组件中sorting Layer改为Ground,hole图片也是如此。
三、调整Game窗口的大小
1.查看ground图片的大小
2.在Game窗口的Free Aspect 点击加号,命名为打地鼠,并且修改像素为ground图片的像素。
四、点击地鼠
1.把Gophers图片和Gophers_Beaten图片拉入场景中,添加一个Sorting Layer为mole,之后把Gophers_Beaten拉为预支体
2.给Gophers添加一个碰撞体Capsule Collider 2D,修改大小和Gophers一样大
3.新建一个Scripts文件夹,再新建一个script脚本,命名为s1,当鼠标点击就变为Gophers_Beaten。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class S1 : MonoBehaviour {
public GameObject mouseyun;
// Use this for initialization
void Start () {
}
void OnMouseDown()
{
Instantiate(mouseyun, transform.position, Quaternion.identity);
Destroy(gameObject);
}
// Update is called once per frame
}
- 把Gophers_Beaten拉到Mouseyun
五、单个地鼠被击中后消失
1.把Gophers_Beaten拉入场景
2.在script文件夹中添加一c#脚本,命名为s2,目的是为了在出现0.5秒后消失
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class S2 : MonoBehaviour {
// Use this for initialization
void Start () {
Destroy(gameObject, 0.5f);
}
// Update is called once per frame
void Update () {
}
}
六、随机位置生成老鼠
1.在场景中新建一个空物体
2.新建一个脚本为Shuiji,实现在9个位置随机1秒生成一个老鼠
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShuiJi : MonoBehaviour {
public GameObject gameObject;
//public Timer timer;
// Use this for initialization
void Start () {
//Create();
InvokeRepeating("Create", 0, 1);//1秒生成一个
//timer.CountDown(true);
}
void Create()
{
Vector3 pos = Vector3.zero;
int id = 0;
id = Random.Range(1, 10);
if (id == 1)
pos = new Vector3(-2, -1, 0);
//Instantiate(gameObject, pos, Quaternion.identity);
if (id == 2)
pos = new Vector3(-2, 0, 0);
//Instantiate(gameObject, pos, Quaternion.identity);
if (id == 3)
pos = new Vector3(-2, 1, 0);
//Instantiate(gameObject, pos, Quaternion.identity);
if (id == 4)
pos = new Vector3(0, -1, 0);
//Instantiate(gameObject, pos, Quaternion.identity);
if (id == 5)
pos = new Vector3(0, 0, 0);
//Instantiate(gameObject, pos, Quaternion.identity);
if (id == 6)
pos = new Vector3(0, 1, 0);
//Instantiate(gameObject, pos, Quaternion.identity);
if (id == 7)
pos = new Vector3(2, -1, 0);
//Instantiate(gameObject, pos, Quaternion.identity);
if (id == 8)
pos = new Vector3(2, 0, 0);
//Instantiate(gameObject, pos, Quaternion.identity);
if (id == 9)
pos = new Vector3(2, 1, 0);
Instantiate(gameObject, pos, Quaternion.identity);
}
}
3.把脚本拉到空物体上,并且把Gophers拉到GameObject上
七、得分
1.添加UI——Text,文字写为得分,设置大小,修改颜色
2.在S1脚本中添加代码,给Gophers一个被击中一次的得分
3.添加C#脚本Score,来实现分数的显示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public static int score;
Text text;
// Use this for initialization
void Start()
{
text = GetComponent<Text>();
score = 0;
}
// Update is called once per frame
void Update()
{
text.text = "得分:" + score;
}
}
八、添加声音组件
1.找到预支体Gophers,添加Audio Sorce组件,把声音拉到audio clip上,Gophers_Beaten相同。
2.最后保存。完成此游戏。
问题及解决办法:
- 问题:在调整Game窗口的画布上出现问题,不能全部显示ground图片。
解决办法:修改摄像机的大小:用像素高/2/100就为摄像机的大小,100为ground图片的分辨率。 - 问题:在做老鼠的随机生成的时候,老鼠与洞口不一致
解决办法:修改每一个老鼠随机出现的位置,与自己游戏的位置相对应。
收获:
- 懂得了如何用摄像机大小来调整Game窗口的大小
- 复习了随机产生的运用和InvokeRepeating的使用
- 巩固了计分代码的使用。