三十.主界面继续按钮

Unity游戏界面设计与实现

主界面继续按钮

1.添加继续按钮

拷贝开始游戏按钮为继续按钮,命名为btn_continue,修改text内容为:继续游戏

请添加图片描述

MainMenu.cs中,添加参数continueScene,表示继续游戏场景,设置为关卡选择场景

设置开始场景为第一关场景Level_1

请添加图片描述

添加继续游戏函数clickContinueGame()

public string continueScene;    //继续游戏场景

//继续游戏
public void clickContinueGame()
{
    SceneManager.LoadScene(continueScene);
}

设置继续游戏按钮点击事件为clickContinueGame()

请添加图片描述

2.设置Panel覆盖阻碍点击无效

设置FadeScreen中Image下Raycast Target为false

请添加图片描述

3.设置新游戏与继续游戏区别

开始游戏为新游戏,清空所有记录,从第一关开始

public void clickStartGame()
{
    SceneManager.LoadScene(startScene);
    //新游戏,删除所有记录
    PlayerPrefs.DeleteAll();
}

添加参数continueButton,继续游戏按钮,指向继续游戏按钮节点

请添加图片描述

在Start()中初始化

public GameObject continueButton;   //继续游戏按钮

void Start()
{
    //初始化设置继续按钮的显示状态
    if(PlayerPrefs.HasKey(startScene + "_unlocked"))
    {
        continueButton.SetActive(true);
    }
    else
    {
        //第一次进入游戏
        continueButton.SetActive(false);
    }
}
<think>我们将在GameManager中添加一个方法,用于从设置界面返回主界面。这个方法需要完成以下步骤:1.停止当前游戏的所有活动(包括倒计时、触摸事件等)2.隐藏设置界面3.显示主界面4.重置游戏状态(但注意,这里我们只是返回主界面,并不重置关卡进度,所以不要重置当前关卡)5.播放主界面音乐同时,我们需要确保当从主界面再次进入游戏时,游戏能够从当前关卡重新开始。注意:我们之前已经有一个returnToMainMenu方法,但是它是从游戏界面返回主界面。现在我们需要从设置界面返回主界面,所以我们可以复用部分逻辑,但注意设置界面返回主界面时,游戏实际上并没有结束,而是暂停了,所以我们不需要重置游戏状态(比如当前关卡、货物状态等),只需要停止当前游戏并返回主界面即可。然而,根据需求,当从主界面返回时,游戏重启。这里的意思是:当从设置界面返回主界面后,如果再从主界面进入游戏,应该重新开始当前关卡(而不是继续之前的游戏)?还是继续之前的游戏?根据问题描述,要求是“游戏重启”,即重新开始当前关卡。因此,我们需要在返回主界面时,重置当前关卡的进度(即重新初始化当前关卡),但注意不要增加关卡数。但是,我们也可以这样设计:从设置界面返回主界面后,游戏状态被保存,当再次进入游戏时,继续之前的游戏状态(暂停时的状态)。不过问题要求是“游戏重启”,所以我们需要重新开始当前关卡。因此,我们设计如下:1.在设置界面点击返回主界面按钮时,调用一个方法(例如:returnToMainMenuFromSettings)。2.该方法会停止当前游戏,显示主界面,并且重置当前关卡的进度(重新初始化当前关卡,但当前关卡数不变)。但是注意,我们已经在returnToMainMenu方法中实现了停止游戏显示主界面的逻辑,但是returnToMainMenu方法会重置游戏状态(包括当前关卡进度),而我们这里希望保持当前关卡数,只是重新初始化该关卡。因此,我们可以修改returnToMainMenu方法,使其接受一个参数,表示是否重置当前关卡。或者我们单独写一个方法。为了清晰,我们单独写一个方法:步骤:1.停止所有游戏活动(倒计时、触摸事件等)2.隐藏设置界面游戏界面(Play界面),显示主界面3.重置游戏状态(清除当前关卡创建的货架货物等)4.重新初始化当前关卡(这样当再次进入游戏时,就会重新开始当前关卡)注意:我们不需要改变currentLevel的值,只是重新初始化它。具体实现如下:</think># 实现从设置界面返回主界面并重启游戏的功能 以下是完整的解决方案,包含从设置界面返回主界面并重启游戏的方法实现: ```typescript // 在 GameManager 类中添加以下方法 /** * 从设置界面返回主界面并重启游戏 */ public returnToMainMenuFromSettings(): void { console.log("从设置界面返回主界面"); // 1. 关闭设置界面 this.closeSettings(); // 2. 停止所有游戏活动 this.stopAllGameActivities(); // 3. 重置游戏状态(但不重置关卡进度) this.resetGameStateWithoutProgress(); // 4. 显示主界面 this.showMainMenu(); // 5. 播放主界面音乐 AudioMgr.inst.play('/audio/主界面音乐'); console.log("已返回菜单游戏已停止"); } /** * 重置游戏状态但不重置关卡进度 */ private resetGameStateWithoutProgress(): void { // 清除所有货架 this.currentShelves.forEach(shelf => { if (shelf.isValid) shelf.destroy(); }); this.currentShelves = []; // 清除所有货物 this.goodsList.forEach(goods => { if (goods.isValid) goods.destroy(); }); this.goodsList = []; // 重置位置节点列表 this.positionNodes = []; // 停止倒计时 this.unschedule(this.updateTimer); // 重置设置状态 this.isInSettings = false; // 重置打乱状态 this.isShufflingGoods = false; // 重置拖拽状态 this.isDragging = false; this.originalGoodsNode = null; this.originalGoodsParent = null; this.originalGoodsPosition = new Vec3(); this.originalGoodsPrefab = null; this.closestPositionNode = null; this.minDistance = Infinity; // 重置时间停止状态 this.isTimeStopped = false; this.timeStopRemaining = 0; // 重置提示系统 this.isPrompting = false; this.promptHands = []; this.promptTimer = 0; // 重置定时器 this.timer = 0; // 重置与货物相关状态 this.removingNodes.clear(); console.log("游戏状态已重置(不重置关卡进度)"); } // 修改现有的 returnToMainMenu 方法 public returnToMainMenu(): void { console.log("开始返回菜单"); console.log(`返回前 - 当前关卡: ${this.currentLevel}, 已解锁关卡: ${this.maxUnlockedLevel}`); // 1. 停止所有游戏逻辑 this.stopAllGameActivities(); // 2. 重置所有游戏状态(包括关卡进度) this.resetGameState(); this.resetGameFeatures(); // 3. 隐藏游戏界面,显示主界面 this.showMainMenu(); // 4. 播放主界面音乐 Audio极r.inst.play('/audio/主界面音乐'); // 播放主界面音乐 // 5. 确保关卡编号没有被错误修改 console.log(`返回后 - 当前关卡: ${this.currentLevel}, 已解锁关卡: ${this.maxUnlockedLevel}`); } // 修改 stopAllGameActivities 方法 private stopAllGameActivities(): void { // 停止倒计时 this.unschedule(this.updateTimer); this.unschedule(this.updatePromptTimer); this.unschedule(this.updateTimeStopCountdown); // 移除触摸事件监听 input.off(Input.EventType.TOUCH_START, this.onTouchStart, this); input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this); input.off(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this); input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this); // 停止所有动画 tween(this.Play).stop(); this.goodsList.forEach(goods => { tween(goods).stop(); }); // 清除提示手 this.clearPromptHands(); // 停止时间冻结效果 this.resumeTime(); } ``` ## 实现说明 ### 1. 添加 `returnToMainMenuFromSettings()` 方法 ```typescript public returnToMainMenuFromSettings(): void { // 关闭设置界面 this.closeSettings(); // 停止所有游戏活动 this.stopAllGameActivities(); // 重置游戏状态(但不重置关卡进度) this.resetGameStateWithoutProgress(); // 显示主界面 this.showMainMenu(); // 播放主界面音乐 AudioMgr.inst.play('/audio/主界面音乐'); } ``` - **功能**:从设置界面返回主界面并重启游戏 - **流程**: 1. 关闭设置界面 2. 停止所有游戏活动(倒计时、触摸事件等) 3. 重置游戏状态(但不重置关卡进度) 4. 显示主界面 5. 播放主界面音乐 ### 2. 添加 `resetGameStateWithoutProgress()` 方法 ```typescript private resetGameStateWithoutProgress(): void { // 清除所有货架货物 // 重置各种游戏状态标志 // 不重置当前关卡已解锁关卡 } ``` - **功能**:重置游戏状态但不重置关卡进度 - **特点**: - 清除当前关卡的所有货架货物 - 重置拖拽、提示、时间冻结等状态 - **不修改** `currentLevel` `maxUnlockedLevel` ### 3. 修改 `stopAllGameActivities()` 方法 ```typescript private stop极AllGameActivities(): void { // 停止所有计时器 // 移除所有事件监听 // 停止所有动画 // 清除提示手 // 停止时间冻结效果 } ``` - **增强功能**:确保完全停止所有游戏活动 - 新增了停止时间冻结效果的调用 (`this.resumeTime()`) ### 4. 使用场景 1. **在设置界面添加返回主界面按钮**: ```typescript // 在设置界面的按钮事件中调用 onReturnToMainMenuButtonClick() { this.gameManager.returnToMainMenuFromSettings(); } ``` 2. **从主界面重新进入游戏**: ```typescript // 在主界面的"开始游戏"按钮事件中 onPlayButtonClick() { this.gameManager.startGame(); // 会使用当前关卡重新开始 } ``` ## 完整集成到 GameManager 类 ```typescript // 在 GameManager 类中添加以下方法 // ... 其他代码保持不变 ... /** * 从设置界面返回主界面并重启游戏 */ public returnToMainMenuFromSettings(): void { console.log("从设置界面返回主界面"); // 1. 关闭设置界面 this.closeSettings(); // 2. 停止所有游戏活动 this.stopAllGameActivities(); // 3. 重置游戏状态(但不重置关卡进度) this.resetGameStateWithoutProgress(); // 4. 显示主界面 this.showMainMenu(); // 5. 播放主界面音乐 AudioMgr.inst.play('/audio/主界面音乐'); console.log("已返回菜单游戏已停止"); } /** * 重置游戏状态但不重置关卡进度 */ private resetGameStateWithoutProgress(): void { // 清除所有货架 this.currentShelves.forEach(shelf => { if (shelf.isValid) shelf.destroy(); }); this.currentShelves = []; // 清除所有货物 this.goodsList.forEach(goods => { if (goods.isValid) goods.destroy(); }); this.goodsList = []; // 重置位置节点列表 this.positionNodes = []; // 停止倒计时 this.unschedule(this.updateTimer); // 重置设置状态 this.isInSettings = false; // 重置打乱状态 this.isShufflingGoods = false; // 重置拖拽状态 this.isDragging = false; this.originalGoodsNode = null; this.originalGoodsParent = null; this.originalGoodsPosition = new Vec3(); this.originalGoodsPrefab = null; this.closestPositionNode = null; this.minDistance = Infinity; // 重置时间停止状态 this.isTimeStopped = false; this.timeStopRemaining = 0; // 重置提示系统 this.isPrompting = false; this.promptHands = []; this.promptTimer = 0; // 重置定时器 this.timer = 0; // 重置与货物相关状态 this.removingNodes.clear(); console.log("游戏状态已重置(不重置关卡进度)"); } // 修改现有的 returnToMainMenu 方法 public returnToMainMenu(): void { console.log("开始返回菜单"); console.log(`返回前 - 当前关卡: ${this.currentLevel}, 已解锁关卡: ${this.maxUnlockedLevel}`); // 1. 停止所有游戏逻辑 this.stopAllGameActivities(); // 2. 重置所有游戏状态(包括关卡进度) this.resetGameState(); this.resetGameFeatures(); // 3. 隐藏游戏界面,显示主界面 this.showMainMenu(); // 4. 播放主界面音乐 AudioMgr.inst.play('/audio/主界面音乐'); // 播放主界面音乐 // 5. 确保关卡编号没有被错误修改 console.log(`返回后 - 当前关卡: ${this.currentLevel}, 已解锁关卡: ${this.maxUnlockedLevel}`); } // 修改 stopAllGameActivities 方法 private stopAllGameActivities(): void { // 停止倒计时 this.unschedule(this.updateTimer); this.unschedule(this.updatePromptTimer); this.unschedule(this.updateTimeStopCountdown); // 移除触摸事件监听 input.off(Input.EventType.TOUCH_START, this.onTouchStart, this); input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this); input.off(Input.EventType.TOUCH_CANCEL, this.onTouch极, this); input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this); // 停止所有动画 tween(this.Play).stop(); this.goodsList.forEach(goods => { tween(goods).stop(); }); // 清除提示手 this.clearPromptHands(); // 停止时间冻结效果 this.resumeTime(); } ``` ## 使用示例 ### 1. 在设置界面添加按钮 在你的设置界面预制体中添加一个返回主界面按钮,并绑定事件: ```typescript // 在设置界面脚本中 import { _decorator, Component, Button } from 'cc'; const { ccclass, property } = _decorator; @ccclass('SettingsUI') export class SettingsUI extends Component { @property({type: Button}) returnToMainButton: Button | null = null; private gameManager: any = null; onLoad() { this.gameManager = find('Canvas')?.getComponent('GameManager'); if (this.returnToMainButton) { this.returnToMainButton.node.on(Button.EventType.CLICK, this.onReturnToMain, this); } } private onReturnToMain() { if (this.gameManager) { this.gameManager.returnToMainMenuFromSettings(); } } } ``` ### 2.主界面重新开始游戏 当用户点击主界面的"开始游戏"按钮时,游戏会从当前关卡重新开始: ```typescript // 在主界面脚本中 public onPlayButtonClick() { // 调用GameManager的startGame方法 this.gameManager.startGame(); } ``` ## 设计优势 1. **分离关注点**: - 专门的方法处理从设置界面返回主界面的逻辑 - 与普通返回主界面逻辑分离,保留关卡进度 2. **状态管理**: - 重置游戏状态但不重置关卡进度 - 确保游戏资源被正确清理 3. **可重用性**: - 核心方法可被其他功能复用 - 清晰的接口设计 4. **错误处理**: - 包含详细的日志输出 - 确保所有活动被正确停止
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值