场景描述
两个场景:场景一和场景二
当从场景一点击“去场景二”按钮时,可以切换至场景二,音乐可不间断播放。当从场景二点击“去场景一”按钮时,切换回场景一。两个场景中均可控制音乐资源的音量
场景布局及层级目录
场景一(SceneOne)
其中,ShiftButton用于切换场景二,volumeSlider用于控制音量大小。
GameMusic(空物体)上挂载了AudioSource组件(声音资源)及AlwaysPlay脚本(使用DontDestroyOnLoad方法在跨场景过程中不销毁GameMusic物体)。
GameManager(空物体)上挂载了Controller脚本。
场景二(SceneTwo)
其中,ShiftButton2用于切换场景一,volumeSlider2用于控制音量大小。
GameManager2(空物体)上挂载了Controller2脚本。
脚本
GameConst.cs
用于保存游戏中需要保存的常量,这里保存音乐资源的音量。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameConst : MonoBehaviour
{
public enum PlayerPrefsStr
{
soundValue,
}
}
AlwaysPlay.cs
使用DontDestroyOnLoad方法使得音乐资源所在空物体在场景二中不被销毁。 需要考虑再次进入场景的重复问题。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AlwaysPlay : MonoBehaviour
{
static AlwaysPlay _instance;
public static AlwaysPlay instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<AlwaysPlay>();
DontDestroyOnLoad(_instance.gameObject);
}
return _instance;
}
}
private void Awake()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(this);
}
else if (this != _instance)
{
Destroy(gameObject);
}
}
}
Controller.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Controller : MonoBehaviour
{
private Button ShiftButton;
private Slider volumeSlider;
private AudioSource audioSource;
void Awake()
{
ShiftButton = GameObject.Find("ShiftButton").GetComponent<Button>();
volumeSlider = GameObject.Find("volumeSlider").GetComponent<Slider>();
audioSource = GameObject.Find("GameMusic").GetComponent<AudioSource>();
ShiftButton.onClick.AddListener(OnShiftClick);
}
void Start()
{
Init();
audioSource.Play();
}
public void Init()
{
if(PlayerPrefs.HasKey(GameConst.PlayerPrefsStr.soundValue.ToString()))
{
float vol = PlayerPrefs.GetFloat(GameConst.PlayerPrefsStr.soundValue.ToString());
audioSource.volume = vol;
volumeSlider.value = vol;
}
else
{
audioSource.volume = 1;
PlayerPrefs.SetFloat(GameConst.PlayerPrefsStr.soundValue.ToString(),1);
volumeSlider.value = 1;
}
volumeSlider.onValueChanged.AddListener(delegate (float vol)
{
PlayerPrefs.SetFloat(GameConst.PlayerPrefsStr.soundValue.ToString(),vol);
audioSource.volume = vol;
});
}
void OnShiftClick()
{
SceneManager.LoadScene("SceneTwo");
}
}
Controller2.cs
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Controller2 : MonoBehaviour
{
private Button ShiftButton;
private AudioSource audioSource;
private Slider volumeSlider;
void Awake()
{
audioSource = GameObject.Find("GameMusic").GetComponent<AudioSource>();
ShiftButton = GameObject.Find("ShiftButton2").GetComponent<Button>();
volumeSlider = GameObject.Find("volumeSlider2").GetComponent<Slider>();
ShiftButton.onClick.AddListener(OnShiftClick);
}
void Start()
{
if(PlayerPrefs.HasKey(GameConst.PlayerPrefsStr.soundValue.ToString()))
{
float vol = PlayerPrefs.GetFloat(GameConst.PlayerPrefsStr.soundValue.ToString());
audioSource.volume = vol;
volumeSlider.value = vol;
}
else
{
audioSource.volume = 1;
PlayerPrefs.SetFloat(GameConst.PlayerPrefsStr.soundValue.ToString(),1);
volumeSlider.value = 1;
}
volumeSlider.onValueChanged.AddListener(delegate (float vol)
{
PlayerPrefs.SetFloat(GameConst.PlayerPrefsStr.soundValue.ToString(),vol);
audioSource.volume = vol;
});
}
void OnShiftClick()
{
SceneManager.LoadScene("SceneOne");
}
}
|