/// <summary> /// 在指定物体上播放音效 /// </summary> /// <param name="soundType"></param> /// <param name="pageName"></param> /// <param name="isLoop"></param> /// <param name="pitch">加速倍数</param> /// <param name="playTime">循环播放时播放的时长,isLoop为true时有效</param> public AudioSource PlayTempSound(AudioSoundType soundType, string pageName = null, bool isLoop = false, float pitch = 1, float playTime = 0) { if (string.IsNullOrEmpty(pageName)) { pageName = PageManager.Instance.CurrentPage.GetType().ToString(); } AudioSource ase; if (sound_clipDict.ContainsKey(soundType)) { ase = tempAudioParent.Find(soundType.ToString()).GetComponent <AudioSource>(); } else { ase = CreateSoundSource(soundType); } ase.clip = GetAudioByType(soundType, pageName); ase.loop = isLoop; ase.pitch = pitch; ase.volume = _sound; ase.mute = _soundSource.mute; ase.Play(); if (isLoop && playTime != 0) { StartCoroutine(DelayStop(ase, playTime)); } return(ase); }
public void StopTargetTempSound(AudioSoundType soundType) { Transform tf = tempAudioParent.Find(soundType.ToString()); if (tf) { AudioSource ase = tf.GetComponent <AudioSource>(); ase.Stop(); } }
/// <summary> /// 创建一个音效源 /// </summary> /// <param name="soundType">音效类型</param> /// <returns></returns> AudioSource CreateSoundSource(AudioSoundType soundType) { GameObject go = new GameObject(soundType.ToString()); go.transform.SetParent(tempAudioParent); AudioSource ase = go.AddComponent <AudioSource>(); ase.playOnAwake = false; return(ase); }
AudioClip GetAudioByType(AudioSoundType soundType, string pageName) { AudioClip ac = null; if (sound_clipDict.ContainsKey(soundType)) { ac = sound_clipDict[soundType]; } else { ac = BundleManager.Instance.GetAudioClip(soundType.ToString(), "Sound", pageName); sound_clipDict.Add(soundType, ac); } return(ac); }
AudioClip GetAudioByType(AudioSoundType soundType, string pageName) { AudioClip ac = null; if (sound_clipDict.ContainsKey(soundType)) { ac = sound_clipDict[soundType]; } else { AssetBundle ab = BundleManager.Instance.GetBundle((string.IsNullOrEmpty(pageName) ? "" : pageName.ToLower() + "/") + "sound/sound/" + soundType.ToString()); if (ab != null) { ac = ab.LoadAsset <AudioClip>(soundType.ToString().ToLower()); ab.Unload(false); } else { ac = Resources.Load <AudioClip>("Sound/Sound/" + soundType.ToString()); } sound_clipDict.Add(soundType, ac); } return(ac); }
/// <summary> /// 在指定物体上播放音效 /// </summary> /// <param name="soundType"></param> /// <param name="pageName"></param> /// <param name="ob"></param> /// <param name="isLoop"></param> public AudioSource PlayTempSound(AudioSoundType soundType, string pageName = null, bool isLoop = false) { AudioSource ase; if (sound_clipDict.ContainsKey(soundType)) { ase = tempAudioParent.Find(soundType.ToString()).GetComponent <AudioSource>(); } else { ase = CreateSoundSource(soundType); } ase.clip = GetAudioByType(soundType, pageName); ase.loop = isLoop; ase.volume = _sound; ase.mute = _soundSource.mute; ase.Play(); return(ase); }