Example #1
0
        /// <summary>
        /// Spawn a SoundToolKitSpatialSource playing a single given SoundToolKitSample at a given location.
        /// The default position is that of component's parent GameObject.
        /// </summary>
        /// <param name="sampleToPlay">
        /// The VolumeControlledSample to be played by the spawned source. Pass an element of the Samples List or a VolumeControlledSample object.
        /// </param>
        /// <param name="positionOfSpawn">
        /// Position at which the Source is spawned. Defaults to the parent GameObject's position.
        /// </param>
        public void SpawnSound(VolumeControlledSample sampleToPlay, Vector3?positionOfSpawn = null)
        {
            if (sampleToPlay != null && sampleToPlay.Sample != null)
            {
                var spawnPosition = DeterminePosition(positionOfSpawn);

                var sourceObject = new GameObject("SpawnedSoundSource");
                sourceObject.transform.position = spawnPosition;
                SourceObjects.Add(sourceObject);

                var source = sourceObject.AddComponent <SoundToolKitSpatialSource>();
                SetupSource(source);

                PlayOneShot(source, sampleToPlay);

                StartCoroutine(TimedDestroy(sourceObject, sampleToPlay.Sample.AudioClip.length));
            }
            else
            {
                WarnSampleInvalid();
            }
        }
Example #2
0
 private void PlayOneShot(SoundToolKitSpatialSource source, VolumeControlledSample sample)
 {
     source.Playbacks.Add(new SoundToolKitPlayback(sample.Sample, sample.Volume));
     source.Play();
     source.Playbacks.Last().OnEnded += OnPlaybackEnded;
 }