1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 ///5 /// 背景音乐脚本 6 /// 7 public class AudioBackground : MonoBehaviour { 8 9 static AudioBackground StaticObject;10 11 private bool LastMusicOn = true;12 13 void Start()14 {15 16 }17 public static AudioBackground instance18 {19 get20 {21 if (StaticObject == null)22 {23 StaticObject = FindObjectOfType();24 DontDestroyOnLoad(StaticObject.gameObject);25 }26 return StaticObject;27 }28 }29 void Awake()30 {31 if (StaticObject == null)32 {33 StaticObject = this;34 DontDestroyOnLoad(this);35 }36 else if (this != StaticObject)37 {38 Destroy(gameObject);39 }40 41 }42 void Update()43 {44 if (MainController.musicOn != LastMusicOn)45 {46 LastMusicOn = MainController.musicOn;47 OnMusicChanged();48 }49 }50 private void OnMusicChanged()51 {52 if (MainController.musicOn)53 {54 gameObject.GetComponent ().Play();55 }56 else57 {58 gameObject.GetComponent ().Pause();59 }60 }61 }