Unity 2D 游戏开发:从原型到多关卡游戏实现
1. 游戏结束状态的实现
在游戏开发中,告知玩家游戏结束是一个重要环节。传统做法是在屏幕上显著显示 “Game Over” 字样,并伴有悲伤的背景音乐,之后玩家可通过主菜单重新开始游戏。为实现这一功能,我们引入游戏状态变量,这里简化为两个状态:游戏进行中(gameplay)和游戏结束(game over)。
操作步骤如下:
1. 创建一个空的 GameObject 并将其重命名为 GameState。
2. 为 GameState 对象创建一个新脚本,命名为 GameState,并添加以下代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameState : MonoBehaviour
{
public static int state;
public const int gamePlay = 1;
public const int gameOver = 2;
// Start is called before the first frame update
void Start()
{
state = gamePlay;
}
}
- 在 donut.cs 中替换 OnCollisionEnter2D 函数:
超级会员免费看
订阅专栏 解锁全文
508

被折叠的 条评论
为什么被折叠?



