You need to enable JavaScript to run this app. Unity如何实现点击所有对象后自动切场景加关卡倒计时功能
优惠活动
大模型
产品
解决方案
定价
更多

Unity如何实现点击所有对象后自动切场景加关卡倒计时功能

Unity 2D气泡点击游戏功能实现方案

步骤1:修改现有气泡pop脚本

修改后的脚本新增了向全局管理器注册、点击通知的逻辑:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class pop : MonoBehaviour
{
    public AudioClip sound;
    AudioSource audio;
    public bool wasClicked = false;

    void Start()
    {
        audio = GetComponent<AudioSource>();
        GameManager.Instance.RegisterBubble(this);
    }

    private void OnMouseDown()
    {
        if (!wasClicked)
        {
            wasClicked = true;
            transform.Rotate(0, 0, 180);
            audio.PlayOneShot(sound);
            GameManager.Instance.OnBubbleClicked();
        }
    }
}

步骤2:新增全局GameManager脚本

该脚本负责管理全局进度、倒计时、场景切换和游戏结束逻辑,挂载到场景内的空对象上即可:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    [Header("功能配置")]
    public float countdownTime = 30f;
    public string nextSceneName;

    [Header("UI关联")]
    public Text countdownText;
    public GameObject gameOverPanel;

    private List<pop> allBubbles = new List<pop>();
    private int clickedBubbleCount = 0;
    private float currentTime;
    private bool isGameEnd = false;

    void Awake()
    {
        if (Instance == null) Instance = this;
        else Destroy(gameObject);
    }

    void Start()
    {
        currentTime = countdownTime;
        gameOverPanel.SetActive(false);
    }

    void Update()
    {
        if (isGameEnd) return;
        currentTime -= Time.deltaTime;
        countdownText.text = Mathf.Ceil(currentTime).ToString();
        if (currentTime <= 0) GameOver();
    }

    public void RegisterBubble(pop bubble)
    {
        allBubbles.Add(bubble);
    }

    public void OnBubbleClicked()
    {
        if (isGameEnd) return;
        clickedBubbleCount++;
        if (clickedBubbleCount >= allBubbles.Count)
        {
            SceneManager.LoadScene(nextSceneName);
        }
    }

    void GameOver()
    {
        isGameEnd = true;
        gameOverPanel.SetActive(true);
    }

    public void ReplayLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void ExitGame()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }
}

步骤3:场景与UI配置

  • 场景内新建空对象命名为GameManager,挂载上面的GameManager脚本,在Inspector面板填写下一关的场景名,确保场景已提前添加到Build Settings的场景列表中
  • 新建UI文本组件CountdownText,拖到GameManager的countdownText字段,调整位置到屏幕可见区域
  • 新建UI面板GameOverPanel,默认设置为隐藏,面板内添加文本显示Game Over,再添加两个按钮分别对应重玩和退出功能
  • 给重玩按钮绑定点击事件:选择GameManager对象→选择GameManager.ReplayLevel方法;给退出按钮绑定点击事件:选择GameManager对象→选择GameManager.ExitGame方法,将面板拖到GameManager的gameOverPanel字段
  • 确认所有气泡对象都挂载了CircleCollider2D组件,否则OnMouseDown点击事件无法触发

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.10 00:57:02