Introduction to various management---audio management: Audio management classes for more audio, more categories of items, often the main process to write, so that the programmer can directly find the audio clips want to play, although slightly complex, but easy to organize and later add, if a project in the late request to add additional sound effects, So it's just a matter of adding an enumeration value to the admin class and avoiding the hassle of repeatedly modifying the code. First write a can inherit a single example script, two kinds of writing, all the single case class to inherit it, omit seven lines of code, often used in a single example of more projects. (1) Inherit from Monobehaviour
Using Unityengine;
Using System.Collections;
Inheriting Monobehaviour's single case template, other scripts can be used directly, omitting seven lines of code public
abstract class monosingleton<t>: Monobehaviour where T: Monosingleton<t>
{
#region single case
private static T instance;
public static T Instance {get
{
if (Instance = = null) {
Gameobject obj = new Gameobject (typeof (T). Name);
Instance = obj. Addcomponent<t> ();
}
return instance
}
}
#endregion
//Overridable awake virtual method for instantiating an object
protected virtual void awake ()
{
instance = this as T;
}
}
(2) Single example of not inheriting Monobehaviour
Using Unityengine;
Using System.Collections;
Public abstract class Singleton<t>: System.IDisposable where T:new ()
{
private static T instance;
public static T Instance {get
{
if (Instance = = null) {
Instance = new T ();
}
return instance
}
}
public virtual void Dispose ()
{
}
}
(3) An enumerated list of all music names, which must be written, do not bother, if trouble, the internet has a lot of great God to write a file to get all the list of plug-ins, we can check, if the code base is better, the use of terminals can get all the list, here is not too much introduction. This is all the audio inside of my project.
Enumeration list for all music names public class Musictype {public enum Feeds {buyitem, Feed01, FEED02, GIFTBOX01, GiftBox02, GiftBox03, PetChange01, PetChange02, PetSound00, P
ETSOUND01, PETSOUND02, PetSound03, PETSOUND10, PetSound11, PetSound12,} public enum Items {Ach, BGMusic01, BGMusic02, BGMUSIC03, BGMUSIC06, B GMUSIC11, BGMUSIC12, Countdown, Crash, Cutwhale, Finalwave, go, Lo SE, MC, MenuSelect, Object, Perfect, Selectfault, Shootselect, Tow Erbulid, Towerdeselect, Towerselect, Towersell, Towerupdata, Bomb, CARROT1
, Carrot2, CARROT3, Clock,} public enum Main {accomplish, bgmusic,
Select,Unlock,} public enum Monsters {bigboss, Fat141, Fat142, Fat143, Fat
241, Fat242, Fat243, Fat341, Fat342, Fat343, Fat641, Fat642,
Fat643, Fly151, Fly152, Fly161, Fly162, Fly163, Fly251, Fly252, Fly253, Fly261, Fly262, Fly263, Fly351, Fly352, Fly361, F
ly362, Fly363, Fly651, Fly652, Fly653, Fly661, Fly662, Fly663,
Land111, Land112, Land113, Land121, Land122, Land131, Land132, Land133, Land211, Land221, Land222, Land231, Land232, Land311, L and312, Land321, Land322, Land323, Land331, Land332, Land333, land
611, Land612, Land613, Land621, Land622, Land623, Land631, Land632, Land633, Littleboss, Whale,} public enum Towers {Anchor, Arrow, Ball, Bot
Tle, Buff, Cuttle, Fan, Firebottle, Fish, mushroom, Pstar, Pin, Plane, rocket, shit, Snow, Star, Sun,}}
(4) Then write a single instance script that can find the path directly through the audio name and dynamically load, which is equivalent to the resource loading class, and other resources can also use it to complete loading
Using Unityengine;
Using System.Collections;
An enumeration value passed in by the user is used to automatically analyze which path the resource of the enumeration value corresponds to.
public class Resourcesmgr:singleton<resourcesmgr> {//user-oriented approach: Method name <T> specified type///<summary>
Returns the resources that the user needs. </summary>///<param name= "Enumname" >enum name.</param>///<typeparam name= "T" >the 1 St type parameter.</typeparam> public T load<t> (object enumname) where T:object {//Get enum type String enumtype = Enumname.gettype ().
Name; Empty string FilePath = string.
Empty;
Switch (enumtype) {case "Feeds": {filePath = "music/feeds/" + enumname.tostring ();
Break
Case "Items": {filePath = "music/items/" + enumname.tostring ();
Break
Case "Main": {filePath = "music/main/" + enumname.tostring (); Break
Case "Monsters": {FilePath = "music/monsters/" + enumname.tostring ();
Break
Case "Towers": {FilePath = "music/towers/" + enumname.tostring ();
Break
Case "Wintype": {filePath = "uiwin/" + enumname.tostring ();
Break
} default: {break;
} return resources.load<t> (FilePath); }
}
(5) Then the most important role of the audio management script
Using Unityengine;
Using System.Collections; Music management class, inheriting a single instance class public class Musicmgr:monosingleton<musicmgr> {//music source for playing background music private Audiosource M_bgmusi
C
Music source for playing sound effects private audiosource m_effectmusic;
Control background music volume size public float Bgvolume {get{return m_bgmusic.volume;
} set{M_bgmusic.volume = value;
///control sound volume size public float Effectvolmue {get{return m_effectmusic.volume;
} set{M_effectmusic.volume = value; }//Override virtual method protected override void Awake () {base.
Awake ();
Materialized music Source M_bgmusic = gameobject.addcomponent<audiosource> (); M_bgmusic.loop = true; Open loop M_bgmusic.playonawake = false;
Start playing//materialized music source M_effectmusic = gameobject.addcomponent<audiosource> ();
M_effectmusic.loop = true; M_effectmusic.playonaWake = FALSE;
//Play background music, pass in an audio clip's name private void Playbgbase (object bgname, bool restart = false) {//define an empty string String curbgname = String.
Empty; If the audio clip for this music source is not empty, if (m_bgmusic.clip!= null) {//Get the name Curbgname of this audio clip = m_bgmusic.cl
Ip.name; ///According to the user's audio fragment name, find Auioclip, then play,//resourcesmgr is a predefined script to find the corresponding path of audio clips, and dynamically load out audioclip clip =
Resourcesmgr.instance.load<audioclip> (Bgname); If found, not empty if (clip!= null) {//If this audio clip has been copied to the class audio source, cut is playing, then jump directly out of the IF (Clip.name = = Curbgnam
E &&!restart) {return;
//Otherwise, assign the audio clip to the audio source and play M_bgmusic.clip = clip;
M_bgmusic.play ();
else {//no direct error//exception found, invoke write Log tool class.
UnityEngine.Debug.Log ("No audio clips found"); ///Play various audio clips of the call method, Musictype is written in advance to store a variety of music names enumerated class, easy to call outside the Public void Playbg (Musictype.feeds bgname, bool restart = false) {Playbgbase (bgname, restart);
public void Playbg (Musictype.items bgname, bool restart = false) {Playbgbase (bgname, restart);
public void Playbg (Musictype.main bgname, bool restart = false) {Playbgbase (bgname, restart);
public void Playbg (musictype.monsters bgname, bool restart = false) {Playbgbase (bgname, restart);
public void Playbg (musictype.towers bgname, bool restart = false) {Playbgbase (bgname, restart);
//play sound effects private void Playeffectbase (object effectname, bool Defaudio = true, float volume = 1f) {
Load the corresponding audio clips according to the lookup path AudioClip clip = resourcesmgr.instance.load<audioclip> (effectname);
If it is an empty picture, make a direct error and then jump out of if (clip = null) {UnityEngine.Debug.Log ("No Sound fragment Found");
Return
//Otherwise, the clip is not empty, if defaudio=true, play directly if (Defaudio) {//playoneshot (audio clip, volume size) m_effectmusic.playoneshot (clip, volume);
else {//point point playback audiosource.playclipatpoint (clip, Camera.main.transform.position, volume); //play a variety of audio clips of the call method, Musictype is written in advance to store a variety of music names of the enumeration class, to facilitate the outside directly call the public void Playeffect (Musictype.feeds effec
Tname, bool Defaudio = true, float volume = 1f) {playeffectbase (Effectname, Defaudio, volume); public void Playeffect (Musictype.items effectname, bool Defaudio = true, float volume = 1f) {Playeffe
Ctbase (Effectname, Defaudio, volume); public void Playeffect (Musictype.main effectname, bool Defaudio = true, float volume = 1f) {PLAYEFFEC
Tbase (Effectname, Defaudio, volume); public void Playeffect (Musictype.monsters effectname, bool Defaudio = true, float volume = 1f) {Playe
Ffectbase (Effectname, Defaudio, volume); } public void PlayeffECT (musictype.towers effectname, bool Defaudio = true, float volume = 1f) {playeffectbase (Effectname, Defau
Dio, volume); }
}
(6) Example application can directly point out the desired audio clips
Code, the script hangs directly on to the empty object Gamecontroller
Using Unityengine;
Using System.Collections;
public class Gamecontroller:monobehaviour
{
void Update ()
{
if (Input.getkeydown (keycode.b)) {
MusicMgr.Instance.PlayBg (MusicType.Main.BGMusic);
}
if (Input.getkeydown (KEYCODE.E)) {
MusicMgr.Instance.PlayEffect (MusicType.Towers.Bottle);
}