Unity3D --//简单的音频播放脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour {
    public static AudioManager Instance;
    // Use this for initialization
    public AudioSource bg_music_player;//背景音乐播放
    public AudioSource action_music_player;//音效播放

    private void Awake()
    {
        Instance = this;
    }
    void Start () {
		
	}
	
	void Update () {
		
	}

    public void PlayBgMusic(string name)
    {
        if (!bg_music_player.isPlaying)
        {
            AudioClip audioClip = Resources.Load(name);
            bg_music_player.clip = audioClip;
            bg_music_player.Play();
        }
        else
        {
            Debug.LogError("正在播放中不能切换");
        }
      
    }

    public void StopBgMusic()
    {
        if (bg_music_player.isPlaying)
        {
            bg_music_player.Stop();
        } 
    }

    //播放音效
    public void PlayActionSound(string name)
    {
        AudioClip audioClip = Resources.Load(name);
        action_music_player.PlayOneShot(audioClip);
    }


    public void StopActionSound(string name)
    {
        action_music_player.Stop();
    }
}

 

你可能感兴趣的:(Unity)