Ejemplo n.º 1
0
    public AudioSource Play(Actor instigator, SoundCue sound, double time, float random, float random2, float random3, float random4, float random5)
    {
        if ((instigator != null) && (sound != null) && (sound.audioSourcePrefab != null))
        {
            MixerGroupState groupState;
            PlayingSource   playingSource;

            if (!CanPlay(instigator, instigator.go, sound, time, out groupState, out playingSource))
            {
                return(null);
            }

            var clip = sound.RandomClip(random, random2);
            if (clip != null)
            {
                AudioSource source = GameObject.Instantiate(sound.audioSourcePrefab);
                source.gameObject.name         = sound.name + "(" + clip.name + ")";
                source.transform.parent        = instigator.go.transform;
                source.transform.localPosition = Vector3.zero;
                source.transform.localRotation = Quaternion.identity;
                source.clip          = clip;
                source.volume        = sound.RandomVolume(random3);
                source.pitch         = sound.RandomPitch(random4);
                source.reverbZoneMix = sound.RandomReverb(random5);
                source.loop          = sound.loop;
                source.Play();

                if (groupState != null)
                {
                    groupState.hasPlayed    = true;
                    groupState.lastTime     = time;
                    playingSource.hasPlayed = true;
                    playingSource.lastTime  = time;
                    var sourcePair = new SourcePair();
                    sourcePair.audioSource   = source;
                    sourcePair.playingSource = playingSource;
                    playingSource.sources.Add(sourcePair);
                    groupState.activeSources.Add(sourcePair);
                    ++groupState.currentVoiceCount;
                }
                else
                {
                    source.gameObject.AddComponent <AudioSourceGC>();
                }

                return(source);
            }
        }

        return(null);
    }
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        EditorGUILayout.PropertyField(_playChance);

        _volume.vector2Value = GUILayoutHelpers.MinMaxSlider("Volume", _volume.vector2Value, 0, 1);
        _pitch.vector2Value  = GUILayoutHelpers.MinMaxSlider("Pitch", _pitch.vector2Value, -3, 3);
        _reverb.vector2Value = GUILayoutHelpers.MinMaxSlider("Reverb", _reverb.vector2Value, 0, 1.1f);

        EditorGUILayout.PropertyField(_loop);
        EditorGUILayout.PropertyField(_clipRef);

        var newClip = _clip.Load();

        if (newClip != _loaded)
        {
            _loaded = newClip;
            StopPlaying();
        }

        var oldPrefab = _audioSourcePrefab.objectReferenceValue;

        EditorGUILayout.PropertyField(_audioSourcePrefab, true);
        if (!ReferenceEquals(oldPrefab, _audioSourcePrefab.objectReferenceValue))
        {
            StopPlaying();
        }

        SoundCue self = (SoundCue)target;

        var wasEnabled = GUI.enabled;

        if (_source == null)
        {
            GUI.enabled = (_loaded != null) && (self.audioSourcePrefab != null);
            if (GUILayout.Button("Play"))
            {
                string unused;
                var    clipToPlay = _loaded.RandomClip(Random.value, out unused);
                if (clipToPlay != null)
                {
                    var go = (GameObject)GameObject.Instantiate(self.audioSourcePrefab.gameObject, Vector3.zero, Quaternion.identity);
                    go.hideFlags = HideFlags.HideAndDontSave;

                    _source = go.GetComponent <AudioSource>();
                    go.AddComponent <AudioSourceGC>();

                    _source.spatialBlend  = 0f;
                    _source.clip          = clipToPlay;
                    _source.volume        = self.RandomVolume(Random.value);
                    _source.pitch         = self.RandomPitch(Random.value);
                    _source.reverbZoneMix = self.RandomReverb(Random.value);
                    _source.loop          = _loop.boolValue;
                    _source.Play();

                    EditorApplication.update += Update;
                }
            }
        }
        else
        {
            GUI.enabled = true;
            if (GUILayout.Button("Stop"))
            {
                StopPlaying();
            }
        }
        GUI.enabled = wasEnabled;

        serializedObject.ApplyModifiedProperties();
    }