简体   繁体   English

c# unity 中的音乐播放器不适用于其他场景

[英]Music player in c# unity doesn't work with other scenes

Okay, so I have a toggle button, which controlls the music volume.好的,所以我有一个切换按钮,用于控制音乐音量。 And it works fine, but when i go to an other scene, and go back - the toggle doesn't work(doesn't change music volume).它工作正常,但是当我 go 到另一个场景时,go 回来 - 切换不起作用(不改变音乐音量)。 Here is the code:这是代码:

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

public class Music : MonoBehaviour
{
    AudioSource audioSource;
    private float volume = 1f;
    Toggle toggle;
    private void Awake()
    {
        SetMusicSingletone();
    }
    private void Start()
    {
        audioSource = GetComponent<AudioSource>();
        toggle = FindObjectOfType<Toggle>();
    }
    private void Update()
    {
        audioSource.volume = this.volume;
    }
    public void ToggleMusic()
    {
        if (!toggle.isOn)
        {
            this.volume = 0f;
        }
        else if (toggle.isOn)
        {
            this.volume = 1f;
        }
    }
    private void SetMusicSingletone()
    {
        var music = FindObjectsOfType<Music>();
        if (music.Length > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }
}

The toggle uses method ToggleMusic()切换使用方法 ToggleMusic()

Alright so the problem is: You have referenced the ToggleMusic method from this instance of Music in your toggle.. then you change scenes and come back.. but due to the singleton thing you destroy the Music instance linked to this new Toggle -> Toggle does nothing now.好吧,问题是:您在切换中引用了此音乐实例的 ToggleMusic 方法。然后您更改场景并返回..但由于 singleton 的事情,您破坏了与此新切换链接的音乐实例 -> 切换现在什么都不做。

You already have a singleton pattern so why not directly make use of it:您已经有一个 singleton 模式,为什么不直接使用它:

public class Music : MonoBehaviour
{
    private static Music _singleton;
    public static Music Singleton => _singleton;

    [SerializeField] private AudioSource audioSource;

    private bool volumeOn;
    public bool MusicIsOn => volumeOn;
    
    private void Awake()
    {
        if (_singleton)
        {
            Destroy(gameObject);
            return;
        }
        
        DontDestroyOnLoad(gameObject);
        _singleton = this;

        if(!audioSource) audioSource = GetComponent<AudioSource>();

        ToggleMusic(true);
    }

    public void ToggleMusic(bool on)
    {
        audioSource.volume = on ? 1f : 0f;
        volumeOn = on;
    }

    public void ToggleMusic()
    {
        ToggleMusic(!volumeOn);        
    }
}

And then on your toggle instead of assigning a callback via the Inspector rather do然后在您的切换上而不是通过检查器分配回调而不是

public class MusicToggle : MonoBehaviour
{
    [SerializeField] private Toggle toggle;

    private void Awake ()
    {
        if(!toggle) toggle = GetComponent<Toggle>();

        toggle.onValueChanged.AddListener(OnValueChanged);

        toggle.isOn = Music.Singleton.MusicIsOn;
    }

    private void OnValueChanged (bool on)
    {
        Music.Singleton.ToggleMusic(on);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM