关卡游戏需要根据游戏的进度调整难易程度,确保玩家能持续从游戏中获得更好的体验。
在游戏设计中,以下内容都属于关卡的内容:
- 游戏的难易程度
- 游戏中的地图(舞台)
本章的小游戏以难易程度来设计关卡。与难易程序有关的 3 个参数:
- 道具生成的时间间隔
- 道具的下落速度
- 苹果炸弹的产生比例
这三个参数的设置方法已经在生成器脚本中实现了。现在我们在调度器脚本中调用。
关卡设计:
关卡 | 剩余时间 | 道具生成的时间间隔 | 道具的下落速度 | 炸弹的占比 |
第一关 | 60秒 | 1秒 | -0.03 | 20% |
第二关 | 50秒 | 0.7秒 | -0.04 | 40% |
第三关 | 30秒 | 0.5秒 | -0.06 | 60% |
每一关得分在 500 以上认为过关,即进入下一关,第三关过关后游戏重新开始。每一关倒计时结束后没有过关的情况下在本关继续重新开始。
7.1增加关卡的显示 UI在层级窗口中选择 Create -> UI -> Text,命名为 Pass。调整 Pass 让它显示在画面右上角。
选中 Pass,在检视器窗口中设置:
锚点:右上角
Rect Transform 项的 PosX、PosY、PosZ 依次为 -70、-115、0,Width、Height 为 160、40
Text(Script) 项的 Text 为 ”No.1“,FontSize 为 32,Alignment 横纵向均为 居中
7.2实现关卡设计功能代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
GameObject timerText;
GameObject pointText;
GameObject passText;
GameObject itemGenerator;
float time = 60.0f;
int point = 0;
int pass = 1;
public void GetApple()
{
point = 100;
}
public void GetBomb()
{
point /= 2;
}
// Start is called before the first frame update
void Start()
{
timerText = GameObject.Find("Time");
pointText = GameObject.Find("Point");
passText = GameObject.Find("Pass");
itemGenerator = GameObject.Find("ItemGenerator");
passText.GetComponent<Text>().text = "No." pass.ToString();
}
// Update is called once per frame
void Update()
{
time -= Time.deltaTime;
timerText.GetComponent<Text>().text = time.ToString("F1");
pointText.GetComponent<Text>().text = point.ToString() " points";
passText.GetComponent<Text>().text = "No." pass.ToString();
// 关卡
if (point >= 500 || time <= 0)
{
// 本次关卡结束
if (point >= 500)
{
if (pass < 3) pass ; else pass = 1;
}
point = 0;
if (pass == 1)
{
itemGenerator.GetComponent<ItemGenerator>().SetParameters(1, 2, -0.03f);
time = 60.0f;
}
else if (pass == 2)
{
itemGenerator.GetComponent<ItemGenerator>().SetParameters(0.7f, 4, -0.04f);
time = 50.0f;
}
else if (pass == 3)
{
itemGenerator.GetComponent<ItemGenerator>().SetParameters(0.5f, 6, -0.06f);
time = 30.0f;
}
}
}
}

















