Exemplo n.º 1
0
 public SoundPlayer(int _ID, string _groupID, SourseKeeper _sourse, int _index)
 {
     ID            = _ID;
     groupID       = _groupID;
     sourseKeeper  = _sourse;
     sourseIndex   = _index;
     crossFadeLoop = false;
 }
Exemplo n.º 2
0
    IEnumerator LoopCrossFade(SoundPlayer soundPlayer, float startTime, float crossFadeDuration)
    {
        soundPlayer.sourseKeeper.sourse.Play();
        SourseKeeper[] twoKeepers = new SourseKeeper[2];
        twoKeepers[0] = soundPlayer.sourseKeeper;
        twoKeepers[1] = soundPlayer.secondSourseKeeper;

        float percent         = 0;
        float length          = soundPlayer.sourseKeeper.sourse.clip.length / soundPlayer.sourseKeeper.sourse.pitch;
        float timeWhenFree    = Time.unscaledTime + soundPlayer.duration;
        int   activeInd       = 0;
        float startVolumeMult = soundPlayer.sourseKeeper.VoulumeMultiplier;

        yield return(new WaitForSecondsRealtime(Mathf.Clamp(length - crossFadeDuration, 0, 10000)));

        float waitTime = Mathf.Clamp(length - 2 * crossFadeDuration - startTime, 0, 10000);

        while (timeWhenFree > Time.unscaledTime)
        {
            twoKeepers[1 - activeInd].sourse.time = startTime;
            twoKeepers[1 - activeInd].sourse.Play();
            percent = 0;
            while (percent < 1)
            {
                percent += Time.unscaledDeltaTime / crossFadeDuration;
                twoKeepers[activeInd].SetMultiplier(Mathf.Lerp(startVolumeMult, 0, percent));
                twoKeepers[1 - activeInd].SetMultiplier(Mathf.Lerp(0, startVolumeMult, percent));
                yield return(null);
            }
            twoKeepers[activeInd].SetMultiplier(0);
            twoKeepers[1 - activeInd].SetMultiplier(startVolumeMult);
            twoKeepers[activeInd].sourse.Stop();
            activeInd = 1 - activeInd;
            yield return(new WaitForSecondsRealtime(waitTime));
        }
        StopSound(soundPlayer);
    }
Exemplo n.º 3
0
    void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;
        //DontDestroyOnLoad(gameObject);

        library = FindObjectOfType <SoundLibrary>();
        if (library == null)
        {
            Debug.LogError("Sound library not found!");
        }
        library.ImportSoundGroups();

        masterVolumePercent = 1;    // PlayerPrefs.GetFloat("masterVolumePercent", 1f);
        musicVolumePercent  = 0.5f; //PlayerPrefs.GetFloat("musicVolumePercent", 1f);
        sfxVolumePercent    = PlayerPrefs.GetFloat("sfxVolumePercent", 1f);

        GameObject   newAudioSourceGO;
        AudioSource  newAudioSource;
        SourseKeeper newSourseKeeper;

        allSourseKeepers = new List <SourseKeeper>();

        int musicSoursesNumber = 2;

        musicKeepers = new SourseKeeper[musicSoursesNumber];
        for (int i = 0; i < musicSoursesNumber; i++)
        {
            newAudioSourceGO           = new GameObject("Music source " + (i + 1));
            newAudioSource             = newAudioSourceGO.AddComponent <AudioSource>();
            newAudioSource.loop        = true;
            newAudioSource.playOnAwake = false;
            newSourseKeeper            = new SourseKeeper(AudioSourseType.Music, newAudioSource, masterVolumePercent * musicVolumePercent, newAudioSourceGO);
            musicKeepers[i]            = newSourseKeeper;
            allSourseKeepers.Add(newSourseKeeper);
            newAudioSourceGO.transform.parent = transform;
        }

        sourseKeepers = new SourseKeeper[cyclingSoursesNumber];
        for (int i = 0; i < cyclingSoursesNumber; i++)
        {
            newAudioSourceGO           = new GameObject("Sound player source " + (i + 1));
            newAudioSource             = newAudioSourceGO.AddComponent <AudioSource>();
            newAudioSource.playOnAwake = false;
            newSourseKeeper            = new SourseKeeper(AudioSourseType.SFX, newAudioSource, masterVolumePercent * sfxVolumePercent, newAudioSourceGO);
            allSourseKeepers.Add(newSourseKeeper);
            sourseKeepers[i] = newSourseKeeper;
            newAudioSourceGO.transform.parent = transform;
            newAudioSourceGO.SetActive(false);
        }

        newAudioSourceGO      = new GameObject("sfx source");
        sfxSource             = newAudioSourceGO.AddComponent <AudioSource>();
        sfxSource.playOnAwake = false;
        newSourseKeeper       = new SourseKeeper(AudioSourseType.SFX, sfxSource, masterVolumePercent * sfxVolumePercent, newAudioSourceGO);
        allSourseKeepers.Add(newSourseKeeper);
        newAudioSourceGO.transform.parent = transform;

        soundPlayersDictionary = new Dictionary <int, SoundPlayer>();

        soundPlayersOfGroupCount = new Dictionary <string, int>();
        nextTimeAllowed          = new Dictionary <string, float>();

        foreach (string key in library.GetKeys())
        {
            soundPlayersOfGroupCount.Add(key, 0);
            nextTimeAllowed.Add(key, 0);
        }
    }