public AudioController Create(Vector3 position, int clipId, Transform parent = null, bool isPlay = true, bool isDelete = true) { if (clipId <= 0) { return(null); } SESoundBuff buff = SESoundBuff.GetSESoundData(clipId); if (buff == null) { Debug.LogError("Can't find sound : " + clipId); return(null); } AudioController au = AudioController.GetAudio(buff, position, parent ? parent : transform, isPlay, isDelete); au.DestroyEvent += OnDelete; m_allAudios.Add(au); return(au); }
void InitData(SESoundBuff buff, bool isAutoDel) { if (mAudio == null) { mAudio = GetComponent <AudioSource>(); } if (mAudio == null) { mAudio = gameObject.AddComponent <AudioSource>(); } mAutoDel = isAutoDel; mClipName = buff.mName; mType = (AudioType)buff.mAudioType; mVolumeScale = buff.mVolume; if (mAudio != null) { mAudio.loop = buff.mLoop; mAudio.dopplerLevel = buff.mDoppler; mAudio.spatialBlend = buff.mSpatial; mAudio.rolloffMode = buff.mMode; mAudio.minDistance = buff.mMinDistance; mAudio.maxDistance = buff.mMaxDistance; mAudio.playOnAwake = false; } CancelInvoke(); StopAllCoroutines(); StartCoroutine(LoadClip()); }
IEnumerator IdleAudio(int[] sounds, float distance) { List <int> _tmpSounds = new List <int>(); while (sounds != null && sounds.Length > 0) { yield return(new WaitForSeconds(Random.Range(20f, 35f))); _tmpSounds.Clear(); PeEntity player = PeCreature.Instance.mainPlayer; if (player != null && m_Animator != null) { float sqrDis = PETools.PEUtil.SqrMagnitude(player.position, Entity.position); if (sqrDis > distance * distance) { if (Random.value < 0.2f) { for (int i = 0; i < sounds.Length; i++) { SESoundBuff buff = SESoundBuff.GetSESoundData(sounds[i]); if (buff != null && sqrDis < buff.mMaxDistance * buff.mMaxDistance * 0.81f) { _tmpSounds.Add(sounds[i]); } } if (_tmpSounds.Count > 0) { int soundID = _tmpSounds[Random.Range(0, _tmpSounds.Count)]; AudioManager.instance.Create(Entity.position, soundID); } } } } } }
public static AudioController GetAudio(SESoundBuff buff, Vector3 pos, Transform parent, bool bPlay, bool bAutoDel) { AudioController au = null; if (_audioPool.Count > 0) { au = _audioPool.Pop(); } if (au == null) //au might be destroyed in somewhere. { GameObject obj = new GameObject(); au = obj.AddComponent <AudioController> (); } au.InitData(buff, bAutoDel); au.name = "Au" + buff.mID; au.transform.position = pos; au.transform.parent = parent; if (bPlay) { au.PlayAudio(); } return(au); }