Ejemplo n.º 1
0
    //public void PlaySound(AudioClip audioClip, float volume, float delay, PlayAudioDelegate endDel, object userData)
    //{
    //	PlaySound(audioClip, volume, false, false, delay, endDel, userData);
    //}

    //public void PlaySound(AudioClip audioClip, float volume, float delay)
    //{
    //	PlaySound(audioClip, volume, delay, null, null);
    //}

    //public void PlaySound(AudioClip audioClip, float delay)
    //{
    //	PlaySound(audioClip, 1, delay, null, null);
    //}

    public void PlaySound(string audioName, float volume = SOUND_DEFAULT_VOLUME, bool ignoreListenerPause = false, bool ignoreListenerVolume = false,
                          float delay = 0, PlayAudioDelegate endDel = null, object userData = null)
    {
        AudioSource audioSource = null;

        if (persistentAudios.ContainsKey(audioName))
        {
            audioSource = persistentAudios[audioName].audioSource;
        }

        AudioClip audioClip = null;

        if (audioSource != null)
        {
            audioClip = audioSource.clip;
        }
        else
        {
            audioClip = ResourceManager.Instance.LoadAsset <AudioClip>(AssetType.Sound, audioName);
        }

        if (audioClip != null)
        {
            audioClip.name = audioName;
        }
        PlaySound(audioClip, volume, ignoreListenerPause, ignoreListenerVolume, delay, endDel, userData);
    }
Ejemplo n.º 2
0
    public void PlaySound(AudioClip audioClip, float volume, bool ignoreListenerPause, bool ignoreListenerVolume,
                          float delay, PlayAudioDelegate endDel, object userData)
    {
        if (audioClip == null)
        {
            return;
        }


        if (IsPreLoadSound(audioClip.name))
        {
            persistentAudios[audioClip.name].audioSource.volume = SoundVolume;            //= volume; 目前改成参数volume无效,使用属性,从表中读取音量大小
            persistentAudios[audioClip.name].audioSource.ignoreListenerPause  = ignoreListenerPause;
            persistentAudios[audioClip.name].audioSource.ignoreListenerVolume = ignoreListenerVolume;
            persistentAudios[audioClip.name].audioSource.PlayDelayed(delay);

            if (endDel != null)
            {
                endDel.Invoke(userData);
            }

            return;
        }

        // Combine the sound played in the same frame
        var batchingSound = GetBatchingSound(audioClip, delay, Time.frameCount, userData);

        if (batchingSound != null)
        {
            batchingSound.volume  = Mathf.Max(SoundVolume, batchingSound.volume);           //Mathf.Max(volume, batchingSound.volume);
            batchingSound.endDel += endDel;

            batchingSound.UpdateVolume(CurrentSoundVolume);
        }
        else
        {
            var audioData = SpawnAudio();
            audioData.isMusic          = false;
            audioData.audioSource.clip = audioClip;
            audioData.audioSource.ignoreListenerPause  = ignoreListenerPause;
            audioData.audioSource.ignoreListenerVolume = ignoreListenerVolume;
            audioData.audioSource.loop         = false;
            audioData.audioSource.spatialBlend = 0;
            audioData.createdFrameCount        = Time.frameCount;
            audioData.createdTime = Time.time;
            audioData.volume      = SoundVolume;       //= volume;
            audioData.delay       = delay;
            audioData.endDel     += endDel;
            audioData.userData    = userData;

            audioData.UpdateVolume(CurrentSoundVolume);

            if (!NeedPlaySound)   //播放声音接口太多 先加这 有空这整理一下
            {
                return;
            }

            audioData.audioSource.PlayDelayed(delay);
        }
    }
Ejemplo n.º 3
0
 public LoadingAudio(WWW www, string audioName, bool useStream, bool isSound, PlayAudioDelegate playAudioDelegate)
 {
     this.www               = www;
     this.audioName         = audioName;
     this.useStream         = useStream;
     this.isSound           = isSound;
     this.audioClip         = null;
     this.playAudioDelegate = playAudioDelegate;
 }
Ejemplo n.º 4
0
    public void PlaySound(string audioName, float volume, bool ignoreListenerPause, bool ignoreListenerVolume,
                          float delay, PlayAudioDelegate endDel, object userData)
    {
        // Load sound clip
        var audioClip = ResourceManager.Instance.LoadAsset <AudioClip>(PathUtility.Combine(baseSoundPath, audioName));

        if (audioClip != null)
        {
            audioClip.name = audioName;
        }

        PlaySound(audioClip, volume, ignoreListenerPause, ignoreListenerVolume, delay, endDel, userData);
    }
Ejemplo n.º 5
0
 public void Reset()
 {
     isMusic            = false;
     audioSource.clip   = null;
     audioSource.volume = 0;
     audioSource.ignoreListenerPause  = false;
     audioSource.ignoreListenerVolume = false;
     audioSource.loop  = false;
     createdFrameCount = 0;
     createdTime       = 0;
     playState         = PlayState.NotStart;
     volume            = 0;
     delay             = 0;
     fadeTime          = 0;
     fadeStartTime     = 0;
     fadeVolume        = 0;
     endDel            = null;
     userData          = null;
 }
Ejemplo n.º 6
0
    public void PlaySound(AudioClip audioClip, float volume, bool ignoreListenerPause, bool ignoreListenerVolume,
                          float delay, PlayAudioDelegate endDel, object userData)
    {
        if (audioClip == null)
        {
            return;
        }

        // Combine the sound played in the same frame
        var batchingSound = GetBatchingSound(audioClip, delay, Time.frameCount, userData);

        if (batchingSound != null)
        {
            batchingSound.volume  = Mathf.Max(volume, batchingSound.volume);
            batchingSound.endDel += endDel;

            batchingSound.UpdateVolume(CurrentSoundVolume);
        }
        else
        {
            var audioData = SpawnAudio();
            audioData.isMusic          = false;
            audioData.audioSource.clip = audioClip;
            audioData.audioSource.ignoreListenerPause  = ignoreListenerPause;
            audioData.audioSource.ignoreListenerVolume = ignoreListenerVolume;
            audioData.loop = false;
            audioData.createdFrameCount = Time.frameCount;
            audioData.createdTime       = Time.time;
            audioData.volume            = volume;
            audioData.delay             = delay;
            audioData.endDel           += endDel;
            audioData.userData          = userData;

            audioData.UpdateVolume(CurrentSoundVolume);

            if (delay == 0)
            {
                audioData.audioSource.Play();
            }
        }
    }
Ejemplo n.º 7
0
 public void PlaySound(string soundName, float volume, float delay, PlayAudioDelegate endDel, object userData)
 {
     PlaySound(soundName, volume, false, false, delay, endDel, userData);
 }
Ejemplo n.º 8
0
 public void PlaySound(AudioClip audioClip, float volume, float delay, PlayAudioDelegate endDel, object userData)
 {
     PlaySound(audioClip, volume, false, false, delay, endDel, userData);
 }
Ejemplo n.º 9
0
 public LoadingAudio(WWW www, string audioName, bool isSound, PlayAudioDelegate playAudioDelegate)
     : this(www, audioName, true, isSound, playAudioDelegate)
 {
 }
Ejemplo n.º 10
0
//#if UNITY_EDITOR
//	[MenuItem("Tools/PlaySelectedAudioSource %3")]
//	public static void PlayEditor()
//	{
//		AudioSource source = null;
//		if (Selection.activeGameObject != null)
//			source = Selection.activeGameObject.GetComponent<AudioSource>();
//
//		if (source == null)
//			source = FindObjectOfType<AudioSource>();
//
//		if (source != null)
//			source.Play();
//	}
//
//	[MenuItem("Tools/StopSelectedAudioSource")]
//	public static void StopEditor()
//	{
//		Selection.activeGameObject.GetComponent<AudioSource>().Stop();
//	}
//#endif

    public void Play3DSound(AudioClip audioClip, Vector3 worldPosition, float volume, float delay, float minDistance = 1, float maxDistance = 500, PlayAudioDelegate endDel = null, object userData = null)
    {
        if (audioClip == null)
        {
            return;
        }

        AudioData audioData;

        if (persistentAudios.ContainsKey(audioClip.name))
        {
            audioData = persistentAudios[audioClip.name];
        }
        else
        {
            audioData = SpawnAudio();
        }

        audioData.isMusic          = false;
        audioData.audioSource.clip = audioClip;
        audioData.audioSource.ignoreListenerPause  = false;
        audioData.audioSource.ignoreListenerVolume = false;
        audioData.audioSource.loop = false;
        //Logarithmic效果会更好,然而Unity不能在代码中控制衰减曲线,用Logarithmic不管多远都能听到声音。
        //http://forum.unity3d.com/threads/changing-audiosources-roll-off-via-scripting.70705/#post-2132241
        //使用Linear替代。
        audioData.audioSource.rolloffMode        = AudioRolloffMode.Linear;
        audioData.audioSource.spatialBlend       = 1;
        audioData.audioSource.transform.position = worldPosition;
        audioData.audioSource.minDistance        = minDistance;
        audioData.audioSource.maxDistance        = maxDistance;
        audioData.createdFrameCount = Time.frameCount;
        audioData.createdTime       = Time.time;
        audioData.volume            = SoundVolume;//= volume;
        audioData.delay             = delay;
        audioData.endDel           += endDel;
        audioData.userData          = userData;

        audioData.UpdateVolume(CurrentSoundVolume);

        if (!NeedPlaySound)
        {
            return;
        }

        audioData.audioSource.PlayDelayed(delay);
    }