美文网首页
unity 全局音频播放

unity 全局音频播放

作者: Walk_In_Jar | 来源:发表于2018-02-01 18:40 被阅读0次

    EffectAudioManager.cs

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EffectAudioManager : MonoBehaviour
    {
        public static EffectAudioManager _instacne;
        private AudioSource audioSource;
        public static EffectAudioManager Instance
        {
            get
            {
                if (_instacne == null)
                {
                    _instacne = FindObjectOfType<EffectAudioManager>();
                    DontDestroyOnLoad(_instacne.gameObject);
                }
                return _instacne;
            }
        }
    
        internal void PlayAudioSource(string name)
        {
            if (!audioSource)
            {
                audioSource = GetComponent<AudioSource>();
            }
            if (audioSource.clip == null)
            {
                audioSource.clip = Resources.Load<AudioClip>("Sounds/" + name);
            }
            else
            {
                if (audioSource.clip.name != name)
                    audioSource.clip = Resources.Load<AudioClip>("Sounds/" + name);
            }
            audioSource.Play();
        }
        private void Awake()
        {
            if (_instacne == null)
            {
                _instacne = this;
                DontDestroyOnLoad(this);
            }
            else if (this != _instacne)
            {
                Destroy(gameObject);
            }
        }
    }
    

    调用 EffectAudioManager.Instance.PlayAudioSource("count");

    相关文章

      网友评论

          本文标题:unity 全局音频播放

          本文链接:https://www.haomeiwen.com/subject/zwcjzxtx.html