- 最后登录
- 2017-4-1
- 注册时间
- 2011-7-26
- 阅读权限
- 90
- 积分
- 24690
  
- 纳金币
- 24658
- 精华
- 6
|
unity3d播放音乐还算挺方便的。
主要要用到三个类:
AudioListener
AudioSource
AudioClip
每个场景都只能有一个AudioListener,多于1个就会提示错误。
现在遇到的情况是,想要多个场景共享一个背景音乐。
写了一个单例用来管理音乐:
using UnityEngine;
using System.Collections;
public class AudioManager : MonoBehaviour {
private static AudioManager instance = null;
public AudioSource audioMgr;
private AudioClip ac;
private string curMusicName = "";
public static AudioManager Instance
{
get
{
return instance;
}
}
void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject);
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
public void Play(string fileName)
{
if (!fileName.Equals(curMusicName))
{
ac = Resources.Load("Audio/"+fileName) as AudioClip;
audioMgr.clip = ac;
audioMgr.Play();
curMusicName = fileName;
}
}
public void Stop()
{
audioMgr.Stop();
curMusicName = "";
Debug.Log("Stop background music");
}
圣典上有个插件:
http://game.ceeger.com/f***m/read.php?tid=789
转自:http://www.cnblogs.com/gameprogr ... /05/30/2526509.html
|
|