在多个场景之间传输数据

Transfering data between multiple scenes

我是 C# 和 Unity 的新手,我尝试实现音量滑块。我现在可以调节音量,但每次尝试加载新场景时音量都会重置。

谁能告诉我如何将音量转移到其他场景?我使用的是 Unity 2018.3.14f1,我使用的代码是:

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

public class SetVolume : MonoBehaviour
{
    public AudioMixer mixer;

    public void SetLevel(float sliderValue)
    {
        mixer.SetFloat("MusicVol", Mathf.Log10(sliderValue) * 20);
    }
}

将其保存在 PlayerPrefs,然后在“开始”或您需要的任何位置加载它。

public class SetVolume : MonoBehaviour
{
    public AudioMixer mixer;
     
    
    void Start()
    {
        SetLevel(PlayerPrefs.GetFloat("MusicVol"));
    }

    public void SetLevel(float sliderValue)
    {
        PlayerPrefs.SetFloat("MusicVol", sliderValue);
        mixer.SetFloat("MusicVol", Mathf.Log10(sliderValue) * 20);
    }
}