static void ReturnSource(AudioSourceData asd, List <AudioSourceData> list, int localInd)
    {
        list.RemoveAt(localInd);

        #region AudioSourceIsNull
        bool isDestroy = asd.GO == null;
        if (isDestroy || asd.AS == null)
        {
            if (!isDestroy)
            {
                GameObject.Destroy(asd.GO, 0f);
            }
            asd = null;
#if UNITY_EDITOR
            Debug.LogError(typeof(AudioController) + " error: ReturnSource is null");
#endif
            return;
        }
        #endregion

        asd.AS.Stop();
        if (asd.IsPool)
        {
            asd.pool.Return(asd);
        }
        else
        {
            if (asd.GO != null)
            {
                GameObject.Destroy(asd.GO, 0f);
            }
            asd = null;
        }
    }
    IEnumerator ResetPlaybackTimeAtEndOfClip(AudioSourceData audioSourceData)
    {
        float remainingTime = audioSourceData.audioSource.clip.length - audioSourceData.audioSource.time;

        yield return(new WaitForSecondsRealtime(remainingTime));

        ResetPlaybackTimeNow(audioSourceData);
    }
    static void DeepPauseList(List <AudioSourceData> list, bool pauseState)
    {
        for (int i = list.Count - 1; i >= 0; i--)
        {
            AudioSourceData asd = list[i];

            #region AudioSourceIsNull
            if (asd.AS == null)
            {
                list.RemoveAt(i);
                if (asd.GO != null)
                {
                    GameObject.Destroy(asd.GO);
                }
                asd = null;
#if UNITY_EDITOR
                Debug.LogError(typeof(AudioController) + " error: ReturnSource is null");
#endif
                continue;
            }
            #endregion

            if (asd.UnscaleTime)
            {
                if (pauseState)
                {
                    asd.AS.Pause();
                }
                else
                {
                    asd.AS.UnPause();
                }
            }
            else
            {
                if (pauseState)
                {
                    if (!paused)
                    {
                        asd.AS.Pause();
                    }
                }
                else
                {
                    if (!paused)
                    {
                        asd.AS.UnPause();
                    }
                }
            }
        }
    }
示例#4
0
    private void Start()
    {
        AudioSource[] audios = FindObjectsOfType <AudioSource>();
        audioSources = new AudioSourceData[audios.Length];

        for (int i = 0; i < audios.Length; i++)
        {
            AudioSourceData sourceData = new AudioSourceData();
            sourceData.defaultPitch = audios[i].pitch;
            sourceData.audioSource  = audios[i];
            audioSources[i]         = sourceData;
        }
    }
示例#5
0
        int IAudioPlayer.PlayAudioClip3D(AudioClip clip, Vector3 position, float maxSoundDistance, float volumeProportion, bool looped)
        {
            if (volumeProportion > 1)
            {
                volumeProportion = 1;
            }
            else if (volumeProportion < 0)
            {
                volumeProportion = 0;
            }

            ScanForEndedSources();
            audioCodeIndex++;

            var go = new GameObject("Audio 3D source");

            go.transform.SetParent(presenter.transform);
            go.transform.position = position;
            var source = go.AddComponent <AudioSource>();

            source.clip          = clip;
            source.loop          = looped;
            source.spatialBlend  = 1;
            source.dopplerLevel  = 0;
            source.reverbZoneMix = 0;
            source.pitch         = 1 + Random.Range(-0.1f, 0.1f);
            source.minDistance   = 0.4f;
            source.rolloffMode   = AudioRolloffMode.Logarithmic;

            source.maxDistance = maxSoundDistance;

            var data = new AudioSourceData()
            {
                Is3Dsound          = true,
                IsMusic            = false,
                OnPause            = false,
                RequestedVolume    = volumeProportion,
                Source             = source,
                SourceRequestedPos = position,
                AudioCode          = audioCodeIndex,
                CachedTransform    = source.transform,
            };

            source.volume = soundEnabled.Value ? volumeProportion * soundVolume.Value : 0;
            source.Play();
            sourceMedia.Add(data.AudioCode, data);
            return(audioCodeIndex);
        }
    void InitMissingVars()
    {
        if (_audioSourceDataList == null)
        {
            _audioSourceDataList = new List <AudioSourceData>();
            foreach (AudioSource audioSource in GetComponents <AudioSource>())
            {
//                Debug.Log("AudioSource: " + audioSource.clip.name);
                AudioSourceData audioSourceData = new AudioSourceData
                {
                    audioSource = audioSource
                };

                _audioSourceDataList.Add(audioSourceData);
//                Debug.Log("Added!");
            }
        }
    }
    public static void RemoveSource(int source)
    {
        if (source == NULL_ID)
        {
            return;
        }
        for (int i = ActiveElements.Count - 1; i >= 0; i--)
        {
            AudioSourceData asd = ActiveElements[i];
            if (asd.ID == source)
            {
                ReturnSource(asd, ActiveElements, i);
                return;
            }
        }
#if UNITY_EDITOR
        Debug.LogError(typeof(AudioController) + " error: Sound's ID not found");
#endif
    }
    public static void UnRegisterSource(ref int source)
    {
        if (source == NULL_ID)
        {
            return;
        }
        for (int i = registeredElements.Count - 1; i >= 0; i--)
        {
            AudioSourceData asd = registeredElements[i];
            if (asd.ID == source)
            {
                registeredElements.RemoveAt(i);
                source = NULL_ID;
                //ReturnSource(asd, registeredElements, ref countRegElems, i);
                return;
            }
        }
        source = NULL_ID;
#if UNITY_EDITOR
        Debug.LogError(typeof(AudioController) + " error: Sound's ID not found");
#endif
    }
    public static int RegisterSource(AudioSource source, bool unscaleTime = false)
    {
        if (source == null)
        {
#if UNITY_EDITOR
            Debug.LogError(typeof(AudioController) + " error: RegisterSource Register Audio is null");
#endif
            return(NULL_ID);
        }
        AudioSourceData asd = new AudioSourceData();
        asd.AS                      = source;
        asd.GO                      = source.gameObject;
        asd.TF                      = asd.GO.transform;
        asd.UnscaleTime             = unscaleTime;
        asd.AS.ignoreListenerVolume = false;//пока такой костыль для звука(не музыки)
        asd.AS.ignoreListenerPause  = unscaleTime;
        asd.ID                      = (maxID + 1) == NULL_ID ? 0 : (++maxID);
        asd.pool                    = ASDFree;
        asd.IsPool                  = true;
        registeredElements.Add(asd);
        return(asd.ID);
    }
    public static void RemoveSource(AudioSource auSo)
    {
        if (auSo == null)
        {
#if UNITY_EDITOR
            Debug.LogError(typeof(AudioController) + " error: RemoveSource AudioSource is null");
#endif
            return;
        }
        for (int i = ActiveElements.Count - 1; i >= 0; i--)
        {
            AudioSourceData asd = ActiveElements[i];
            if (asd.AS == auSo)
            {
                ReturnSource(asd, ActiveElements, i);
                return;
            }
        }
#if UNITY_EDITOR
        Debug.LogError(typeof(AudioController) + " error: Sound's ID not found");
#endif
    }
示例#11
0
        int IMusicPlayer.PlayMusicClip(AudioClip clip, float volumeProportion)
        {
            if (volumeProportion > 1)
            {
                volumeProportion = 1;
            }
            else if (volumeProportion < 0)
            {
                volumeProportion = 0;
            }

            ScanForEndedSources();
            audioCodeIndex++;

            var source = presenter.gameObject.AddComponent <AudioSource>();

            source.clip         = clip;
            source.loop         = true;
            source.priority     = 0;
            source.spatialBlend = 0;
            source.minDistance  = 0.06f;

            var data = new AudioSourceData()
            {
                Is3Dsound          = false,
                IsMusic            = true,
                OnPause            = false,
                RequestedVolume    = volumeProportion,
                Source             = source,
                SourceRequestedPos = Vector3.one,
                AudioCode          = audioCodeIndex,
            };

            source.volume = musicEnabled.Value ? volumeProportion * musicVolume.Value : 0;
            source.Play();
            sourceMedia.Add(data.AudioCode, data);
            return(audioCodeIndex);
        }
示例#12
0
        /// <summary>
        /// 监听歌曲是否播放完毕
        /// </summary>
        internal static void ListeningSongOver()
        {
            ScenesDatas scenesDatas = ModelManager.Instance.GetScenesDatas;

            if (scenesDatas.AudioSource.clip == null)
            {
                return;
            }

            if (SongTimeTools.IsSongOver(AudioSourceData.GetCurrentSongTime(scenesDatas.AudioSource), AudioSourceData.GetCurrentSongLength(scenesDatas.AudioSource)))
            {
                switch (ModelManager.Instance.GetLogicDatas.CycleType)
                {
                case CycleType.SingleCycle:
                    SongControl.OnSingleCycle();
                    break;

                case CycleType.PlayInOrder:
                    SongControl.OnPlayInOrder();
                    break;

                case CycleType.LoopPlayback:
                    SongControl.OnLoopPlayback();
                    break;

                case CycleType.RandomPlay:
                    SongControl.OnRandomPlay();
                    break;

                case CycleType.SinglePlay:
                    SongControl.OnSinglePlay();
                    break;

                default:
                    break;
                }
            }
        }
    static void InitAudioSourceData(AudioSourceData asd, AudioSourceInfo info)
    {
        asd.TF.position = info.Pos;
        asd.AS.clip     = info.Clip;

        asd.AS.volume               = info.SettingsSource.Volume;
        asd.AS.spatialBlend         = info.SettingsSource.Blend3DSound;
        asd.AS.loop                 = (info.SettingsSource.Type & AS_Loop) != 0;
        asd.AS.pitch                = info.SettingsSource.Pitch;
        asd.AS.minDistance          = info.SettingsSource.MinDist;
        asd.AS.maxDistance          = info.SettingsSource.MaxDist;
        asd.AS.ignoreListenerVolume = false;//пока такой костыль для звука(не музыки)
        asd.AS.ignoreListenerPause  = (info.SettingsSource.Type & AS_Unscale) != 0;
        float lt = info.SettingsSource.LifeTime;

        asd.UnLimit     = lt < 0f && ((info.SettingsSource.Type & AS_Loop) != 0);
        asd.CurLifeTime = asd.LifeTime = lt > 0f ? lt : info.Clip.length;
        asd.UnscaleTime = (info.SettingsSource.Type & AS_Unscale) != 0;
        asd.ID          = (maxID + 1) == NULL_ID ? 0 : (++maxID);
#if UNITY_EDITOR
        asd.GO.name = info.Clip.name;
#endif
    }
    public static int UnRegisterSource(AudioSource auSo)
    {
        if (auSo == null)
        {
            return(NULL_ID);
        }
        int hasCode = auSo.GetInstanceID();

        for (int i = registeredElements.Count - 1; i >= 0; i--)
        {
            AudioSourceData asd = registeredElements[i];
            if (asd.AS.GetInstanceID() == hasCode)
            {
                registeredElements.RemoveAt(i);
                //ReturnSource(asd, registeredElements, ref countRegElems, i);
                return(NULL_ID);
            }
        }
#if UNITY_EDITOR
        Debug.LogError(typeof(AudioController) + " error: AudioSource not found");
#endif
        return(NULL_ID);
    }
 void ResetPlaybackTimeNow(AudioSourceData audioSourceData)
 {
     audioSourceData.audioSource.time           = 0;
     audioSourceData.resetPlaybackTimeCoroutine = null;
 }
示例#16
0
        int IAudioPlayer.PlayAudioClip2D(AudioClip clip, float volumeProportion, bool looped)
        {
            if (volumeProportion > 1)
                volumeProportion = 1;
            else if (volumeProportion < 0)
                volumeProportion = 0; 

            ScanForEndedSources();
            audioCodeIndex++;

            var source = presenter.gameObject.AddComponent<AudioSource>();
            source.clip = clip;
            source.loop = looped;
            source.spatialBlend = 0;
            source.minDistance = 0.06f;
            var data = new AudioSourceData()
            {
                Is3Dsound = false,
                IsMusic = false,
                OnPause = false,
                RequestedVolume = volumeProportion,
                Source = source,
                SourceRequestedPos = Vector3.one,
                AudioCode = audioCodeIndex,
            };

            source.volume = soundEnabled.Value ? volumeProportion*soundVolume.Value : 0;
            source.Play();
            sourceMedia.Add(data.AudioCode, data);
            return audioCodeIndex;
        }
示例#17
0
        int IAudioPlayer.PlayAudioClip3D(AudioClip clip, Vector3 position, float maxSoundDistance, float volumeProportion, bool looped)
        {
            if (volumeProportion > 1)
                volumeProportion = 1;
            else if (volumeProportion < 0)
                volumeProportion = 0; 

            ScanForEndedSources();
            audioCodeIndex++;

            var go = new GameObject("Audio 3D source");
            go.transform.SetParent(presenter.transform);
            go.transform.position = position;
            var source = go.AddComponent<AudioSource>();
            source.clip = clip;
            source.loop = looped;
            source.spatialBlend = 1;
            source.dopplerLevel = 0;
            source.reverbZoneMix = 0;
            source.pitch = 1 + Random.Range(-0.1f, 0.1f);
            source.minDistance = 0.4f;
            source.rolloffMode = AudioRolloffMode.Logarithmic;

            source.maxDistance = maxSoundDistance;

            var data = new AudioSourceData()
            {
                Is3Dsound = true,
                IsMusic = false,
                OnPause = false,
                RequestedVolume = volumeProportion,
                Source = source,
                SourceRequestedPos = position,
                AudioCode = audioCodeIndex,
                CachedTransform = source.transform,
            };

            source.volume = soundEnabled.Value ? volumeProportion * soundVolume.Value : 0;
            source.Play();
            sourceMedia.Add(data.AudioCode, data);
            return audioCodeIndex;
        }