コード例 #1
0
ファイル: SoundSource.cs プロジェクト: hww/varp_sounds
        // ===============================================================================
        // Play event with given clip name
        // ===============================================================================

        public void Play(AudioEvent audioEvent, string clipName = null, OnEndDelegate onChangeState = null)
        {
            Debug.Assert(audioEvent != null);

            state         = ESoundSourceState.Initial;
            OnChangeState = onChangeState;

            SoundSystem.Add(Link);
            audioEvent.Add(linkForEvent);

            Source.gameObject.SetActive(true);
            Source.outputAudioMixerGroup = audioEvent.MixerGroup;
            Source.priority            = 0;
            Source.mute                = false;
            Source.playOnAwake         = false;
            Source.ignoreListenerPause = true;
            Source.rolloffMode         = AudioRolloffMode.Linear;
            Source.minDistance         = 0f;
            Source.maxDistance         = 100000f;

            if (clipName == null)
            {
                audioEvent.Play(Source, false); // do not play, just set clip, pitch, volume
            }
            else
            {
                audioEvent.Play(Source, clipName, false); // do not play, just set clip, pitch, volume
            }
            EventName = audioEvent.name;
            ClipName  = Source.clip.name;
            Name      = $"SND:{EventName}#{ClipName}";
            ClipLen   = Source.clip.length;

            var clipLen05 = ClipLen * 0.5f;

            Is3D        = false;
            IsLooped    = audioEvent.IsLooped;
            IsPausable  = audioEvent.IsPausable;
            DelayTime   = audioEvent.DelayTime.Random;
            FadeInTime  = Mathf.Min(audioEvent.FadeInTime.Random, clipLen05);
            FadeOutTime = Mathf.Min(audioEvent.FadeOutTime.Random, clipLen05);

            DoFadeIn      = FadeInTime > 0;
            DoFadeOut     = FadeOutTime > 0;
            EventVolume   = audioEvent.Volume.Random;
            FadeVolume    = 0;
            FadeOutAtTime = ClipLen - FadeOutTime;
            Volume        = 1f;
            Play();
        }