First of all, here we just make a prefab of this game manager and then write a generic template class (including turning off music and turning on music, turning off the sound and opening sound, and playing the game archive to turn off the music) on top of this precast piece.
Generic Template classes:
Using Unityengine;
Using System.Collections;
public class audioplayer:monobehaviour{
static public Audioplayer s_instance = null;
static public bool Isretargetok=true;
Play sound Once
static public void Playaudioonce (string name) {
if (Playerprefs.haskey ("audio") && Playerprefs.getint ("audio") = = 0) {
Return
}//This is an archive to turn on music
if (s_instance! = null) {
audiosource[] Audiosource = s_instance.gameobject.getcomponents<audiosource> ();
bool Isplayok = true;
foreach (var item in Audiosource) {
if (item.clip! = null && Item.clip.name = = name&&item.isplaying) {
Isplayok = false;
Break
}
}
if (Isplayok) {
Audiosource[s_instance.currentaudioindex].clip = util.loadresource<audioclip> ("Sounds/" + Name + ". mp3"); Here is the loading of your game music, here you write a dynamic load of resources, easy to package the resources behind
Audiosource[s_instance.currentaudioindex]. Play ();
s_instance.currentaudioindex++;
if (S_instance.currentaudioindex > Audioplayer.kaudiocount) {
S_instance.currentaudioindex = 1;
}
}else{
}
}
}
Play sound
static public void Playaudio (string name) {
if (Playerprefs.haskey ("audio") && Playerprefs.getint ("audio") = = 0) {
Return
}
if (s_instance! = null) {
audiosource[] Audiosource = s_instance.gameobject.getcomponents<audiosource> ();
Audiosource[s_instance.currentaudioindex].clip = util.loadresource<audioclip> ("Sounds/" + Name + ". mp3");
Audiosource[s_instance.currentaudioindex]. Play ();
s_instance.currentaudioindex++;
if (S_instance.currentaudioindex > Audioplayer.kaudiocount)
{
S_instance.currentaudioindex = 1;
}
}
}
Play Music
static public void Playmusic (string name) {
if (Playerprefs.haskey ("Music") && Playerprefs.getint ("music") = = 0) {
Return
}
if (s_instance! = null) {
audiosource[] Audiosource = s_instance.gameobject.getcomponents<audiosource> ();
Audiosource[0].clip = util.loadresource<audioclip> ("sounds/" + Name + ". mp3");
Audiosource[0]. Play ();
}
}
Make it easier for other scripts to call this method of playing music
static public void Playcurrentmusic () {
if (s_instance! = null) {
Playmusic (S_instance.musicname);
}
}
Turn off music
static public void Closemusic () {
if (s_instance! = null) {
audiosource[] Audiosource = s_instance.gameobject.getcomponents<audiosource> ();
Audiosource[0]. Stop ();
}
}
private int currentaudioindex = 1;
public const int kaudiocount = 31;
public string musicname = "";
Single-Case mode
void Awake () {
S_instance = this;
}
When the game is just installed, play the music before you come in.
void Start () {
Audioplayer.playmusic (Musicname);
}
}
Game Archive Code:
Using Unityengine;
Using System.Collections;
Using System;
public class Setcanvas:monobehaviour
{
Public Gameobject Btonmusic;
Public Gameobject Btoffmusic;
Public Gameobject Btonaudio;
Public Gameobject Btoffaudio;
void Start ()
{
if (Playerprefs.haskey ("Music") && Playerprefs.getint ("music") = = 0)
{
Btoffmusic.setactive (TRUE);
Btonmusic.setactive (FALSE);
}
else if (Playerprefs.haskey ("Music") && Playerprefs.getint ("music") = = 1)
{
Btonmusic.setactive (TRUE);
Btoffmusic.setactive (FALSE);
}
Else
{
Btonmusic.setactive (TRUE);
Btoffmusic.setactive (FALSE);
}
if (Playerprefs.haskey ("audio") && Playerprefs.getint ("audio") = = 0)
{
Btoffaudio.setactive (TRUE);
Btonaudio.setactive (FALSE);
}
else if (Playerprefs.haskey ("audio") && Playerprefs.getint ("audio") = = 1)
{
Btoffaudio.setactive (FALSE);
Btonaudio.setactive (TRUE);
}
Else
{
Btonaudio.setactive (TRUE);
Btoffaudio.setactive (FALSE);
}
}
public void Closemusic ()
{
Playerprefs.setint ("Music", 0);
Audioplayer.closemusic ();
Btoffmusic.setactive (TRUE);
Btonmusic.setactive (FALSE);
}
public void OpenMusic ()
{
Playerprefs.setint ("Music", 1);
Audioplayer.playcurrentmusic ();
Btoffmusic.setactive (FALSE);
Btonmusic.setactive (TRUE);
}
public void Closeaudio ()
{
Playerprefs.setint ("Audio", 0);
Btoffaudio.setactive (TRUE);
Btonaudio.setactive (FALSE);
}
public void Openaudio ()
{
Playerprefs.setint ("Audio", 1);
Btoffaudio.setactive (FALSE);
Btonaudio.setactive (TRUE);
}
public void Colseui ()
{
Gameobject.setactive (FALSE);
}
}
Now anywhere you want to turn off or turn on music sound, you can call this game management class.
Audioplayer.playcurrentmusic (); Turn on the music
Audioplayer.closemusic (); Turn off the music
This article is from "Cool Little Joe" blog, please be sure to keep this source http://5152481.blog.51cto.com/5142481/1686345
The management of game music after unity3d5.0