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

Unity游戏设置选项开关失效,请求技术排查与解决

Unity设置功能异常修复方案

问题根源分析

你的脚本存在三个核心逻辑错误:

  • MuzicaOn方法重复翻转布尔值:if分支内已经执行了一次MuzicaBool = !MuzicaBool,方法末尾又重复执行,导致布尔值两次翻转后回到初始状态,音乐永远无法关闭。
  • 开关状态与UI显示逻辑颠倒:所有开关方法中,布尔值为true(开启状态)时隐藏图标,false(关闭状态)时显示图标,和常规的“开启显示、关闭隐藏”逻辑完全相反,音效播放判断也随之颠倒。
  • 部分变量初始值不符合需求:OftatBool默认未赋值(等价于false),但你期望它初始为开启状态。

修复后的完整代码

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

public class SettingsScript : MonoBehaviour
{
    public GameObject Muzica;
    public GameObject Oftat;
    public GameObject SFX;
    public GameObject Fullscreen;
    public GameObject VolumObject;
    public bool MuzicaBool = true;
    public bool OftatBool = true; // 修正初始值为开启状态
    public bool SFXBool = true;
    public bool FullscreenBool = true;
    public float VolumFloat = 1f;
    public AudioSource on;
    public AudioSource off;
    public static SettingsScript instance;

    void Awake()
    {
        instance = this;
        // 初始化UI状态,确保启动时显示与变量一致的开关图标
        UpdateUIState();
    }

    void Update()
    {
        UnityEngine.Debug.Log(MuzicaBool + " " + OftatBool + " " + SFXBool + " " + FullscreenBool + " " + VolumFloat);
    }

    public void Menu()
    {
        SceneManager.LoadScene("Meniu");
    }

    public void Volum()
    {
        AudioListener.volume = VolumFloat;
    }

    // 统一UI状态更新方法,避免重复代码
    private void UpdateUIState()
    {
        Muzica.GetComponent<SpriteRenderer>().color = MuzicaBool ? new Color(1,1,1,1) : new Color(1,1,1,0);
        Oftat.GetComponent<SpriteRenderer>().color = OftatBool ? new Color(1,1,1,1) : new Color(1,1,1,0);
        SFX.GetComponent<SpriteRenderer>().color = SFXBool ? new Color(1,1,1,1) : new Color(1,1,1,0);
        Fullscreen.GetComponent<SpriteRenderer>().color = FullscreenBool ? new Color(1,1,1,1) : new Color(1,1,1,0);
    }

    public void MuzicaOn()
    {
        MuzicaBool = !MuzicaBool;
        UpdateUIState();
        
        if(MuzicaBool)
            on.Play();
        else
            off.Play();
        
        UnityEngine.Debug.Log("Muzica: " + MuzicaBool);
    }

    public void OftatOn()
    {
        OftatBool = !OftatBool;
        UpdateUIState();
        
        if(OftatBool)
            on.Play();
        else
            off.Play();
        
        UnityEngine.Debug.Log("Oftat: " + OftatBool);
    }

    public void SFXOn()
    {
        SFXBool = !SFXBool;
        UpdateUIState();
        
        if(SFXBool)
            on.Play();
        else
            off.Play();
        
        UnityEngine.Debug.Log("SFX: " + SFXBool);
    }

    public void FullscreenOn()
    {
        FullscreenBool = !FullscreenBool;
        UpdateUIState();
        
        // 补充实际切换全屏的逻辑(原代码遗漏此步骤)
        Screen.fullScreen = FullscreenBool;
        
        if(FullscreenBool)
            on.Play();
        else
            off.Play();
        
        UnityEngine.Debug.Log("Fullscreen: " + FullscreenBool);
    }
}

额外优化说明

  1. 新增UpdateUIState方法,减少代码冗余,便于后续维护。
  2. 在Awake中初始化UI状态,确保游戏启动时UI与变量状态一致。
  3. 给FullscreenOn补充了实际切换全屏的代码,原代码仅修改UI未真正改变全屏状态。
  4. 调整Debug日志格式,增加状态值显示,方便调试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 07:45:48