Exemplo n.º 1
0
 /**
  * Implementation for events of type "Stop"
  */
 private void PostEventStop_Implementation(SoundEvent soundEvent)
 {
     RemoveEvents(
         (target, hashCode) =>
     {
         SoundEventHandle handle = target.GetComponent <SoundEventHandle>();
         return(handle.EventID == hashCode);
     },
         soundEvent.EventToStop.GetHashCode());
 }
Exemplo n.º 2
0
    /**
     * Finds an event target from an ID
     * @param soundEventID The id of the sound target to find
     * @return A reference on the target or null if not found
     */
    private GameObject GetTargetFromID(int soundEventID)
    {
        foreach (GameObject soundObject in Targets)
        {
            SoundEventHandle handle = soundObject.GetComponent <SoundEventHandle>();

            if (handle.EventID == soundEventID)
            {
                return(soundObject);
            }
        }

        return(null);
    }
Exemplo n.º 3
0
    /**
     * Returns the number of instances of a specific events
     * @param hashCode
     * @return the number of instances, could be zero
     */
    private int GetEventInstanceCount(int hashCode)
    {
        int instanceCount = 0;

        foreach (GameObject soundObject in Targets)
        {
            SoundEventHandle handle = soundObject.GetComponent <SoundEventHandle>();

            if (handle.EventID == hashCode)
            {
                instanceCount += 1;
            }
        }

        return(instanceCount);
    }
Exemplo n.º 4
0
    /**
     * Implementation for events of type "Play"
     */
    private void PostEventPlay_Implementation(SoundEvent soundEvent)
    {
        // Pre-condition
        if (GetEventInstanceCount(soundEvent.EventID) >= soundEvent.EventMaxInstance)
        {
            DisplayWarningMessage("AudioEventManager : Too many instances to instantiate " + soundEvent.EventName);
            return;
        }

        // Pre-condition
        if (soundEvent.EventTargets.Count == 0)
        {
            DisplayWarningMessage("AudioEventManager : The event " + soundEvent.EventName + " has no audio targets.");
            return;
        }

        // Instianting the sound object
        // Setting up the object
        GameObject go = Instantiate(SoundObject, this.transform) as GameObject;

        go.name = soundEvent.EventName;

        AudioSource      source = go.GetComponent <AudioSource>();
        SoundEventHandle handle = go.GetComponent <SoundEventHandle>();

        handle.EventID = soundEvent.EventID;


        if (soundEvent.EventIsRandom)
        {
            // We should add only one audio target
            int targetIndex = Random.Range(0, soundEvent.EventTargets.Count);
            source.clip = soundEvent.EventTargets[targetIndex];
        }
        else
        {
            source.clip = soundEvent.EventTargets[0];

            int sourcesToAdd = soundEvent.EventTargets.Count - 1;
            for (int nTarget = 0; nTarget < sourcesToAdd; ++nTarget)
            {
                AudioSource newSource = go.AddComponent <AudioSource>();
                newSource.clip = soundEvent.EventTargets[nTarget + 1];
            }
        }

        // General audio source settings
        AudioSource[] sources      = go.GetComponents <AudioSource>();
        int           sourcesCount = sources.Length;

        for (int nSource = 0; nSource < sourcesCount; ++nSource)
        {
            // Mixer setting
            AudioMixerGroup mixer = null;
            if (soundEvent.EventType == SoundEvent.EEventType.Music)
            {
                mixer = MusicMixer;
            }
            else
            {
                mixer = SFXMixer;
            }

            sources[nSource].outputAudioMixerGroup = mixer;

            float pitch  = soundEvent.EventPitch;
            float volume = soundEvent.EventVolume;

            // Volume and Pitch settings
            if (soundEvent.EventIsPitchRandom)
            {
                pitch += Random.Range(soundEvent.EventPitchRandomRange.x, soundEvent.EventPitchRandomRange.y);
            }

            if (soundEvent.EventIsVolumeRandom)
            {
                volume += Random.Range(soundEvent.EventVolumeRandomRange.x, soundEvent.EventVolumeRandomRange.y);
            }

            sources[nSource].pitch  = pitch;
            sources[nSource].volume = volume;

            if (soundEvent.EventIsLooping)
            {
                sources[nSource].loop = true;
            }

            // Plays all sources
            sources[nSource].Play();
        }

        // Buffers the go
        Targets.Add(go);
    }