首页 > 代码库 > Unity3d 背景、音效 播放 简单demo

Unity3d 背景、音效 播放 简单demo

 

仅实现功能,AudioListener在MainCamera中

using UnityEngine;using System.Collections;using System.Collections.Generic;public class MusicManger : MonoBehaviour {    AudioSource _backMusicSource = null;    AudioSource _SoundSource = null;    //public string musicName = "";    // Use this for initialization    void Start () {        _backMusicSource = gameObject.AddComponent<AudioSource>();        _backMusicSource.loop = true;        _SoundSource = gameObject.AddComponent<AudioSource>();        PlayMusic("Music/test");        PlaySound("Music/test1");    }    void PlayMusic(string musicPath)    {        AudioClip clip = Resources.Load(musicPath) as AudioClip;        _backMusicSource.clip = clip;        _backMusicSource.Play();    }    void PlaySound(string soundPath)    {        AudioClip clip = Resources.Load(soundPath) as AudioClip;        _SoundSource.PlayOneShot(clip);    }}