You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中C#暂停菜单脚本无法工作,不知正确挂载位置?

解决Unity暂停菜单脚本挂载与生效问题

你的脚本逻辑本身没问题,下面说下具体挂载方式和生效的关键检查点:

一、脚本挂载的两种常用方式

1. 挂载到专门的Game Manager对象(推荐)

  • 在Hierarchy面板右键创建空对象,命名为GameManager
  • 将你的ResumeMenu脚本拖到这个空对象上
  • 选中GameManager,在Inspector面板里把你做好的暂停菜单UI对象拖到脚本的pauseMenu字段中

2. 直接挂载到暂停菜单UI对象上

  • 把ResumeMenu脚本直接拖到你的暂停菜单GameObject上
  • 在Inspector面板里,将脚本的pauseMenu字段关联为该菜单对象本身(直接拖这个对象进去即可)

二、确保脚本生效的关键检查

  • 提前把暂停菜单UI设置为禁用状态(脚本Start方法里会自动处理,但提前禁用能避免初始显示问题)
  • 测试时按Esc键,可在Inspector面板查看脚本的isPaused值,或在Game视图右上角打开Stats面板确认Time.timeScale是否变化
  • 如果UI没反应,排查以下点:
    • 暂停菜单必须在Canvas对象下,且Canvas的Render Mode设置正确(3D游戏推荐用Screen Space - Overlay)
    • 有没有其他脚本修改了Time.timeScale,导致逻辑冲突
    • 检查输入设置:打开Edit > Project Settings > Input Manager,确认Escape键的映射正常

你的脚本参考

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResumeMenu : MonoBehaviour
{
    public GameObject pauseMenu;

    public bool isPaused;

    void Start()
    {
        pauseMenu.SetActive(false);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (isPaused)
            {
                ResumeGame();
            }
            else
            {
                PauseGame();
            }
        }
    }

    public void PauseGame()
    {
        pauseMenu.SetActive(true);
        Time.timeScale = 0f;
        isPaused = true;
    }

    public void ResumeGame()
    {
        pauseMenu.SetActive(false);
        Time.timeScale = 1f;
        isPaused = false;
    }
}

内容的提问来源于stack exchange,提问作者Wolfiksk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 01:30:10