CustomAssets
Askowl Custom Assets
Constant/AudioClips.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 using System;
4 using Askowl;
5 using UnityEngine;
6 
7 namespace CustomAsset.Constant {
8  /// <a href="http://bit.ly/2RcqaCt">Support class for audio clip playing</a> <inheritdoc />
9  [Serializable] public sealed class AudioClipSet : Set<AudioClip> {
10  [SerializeField, Header("Audio")] private Range volume = new Range(1, 1);
11  [SerializeField, RangeBounds(0, 2)] private Range pitch = new Range(1, 2);
12  [SerializeField, RangeBounds(0, 999)] private Range distance = new Range(1, 999);
13 
14  /// <a href="http://bit.ly/2RcqaCt">Find an AudioSource to use for playing the sounds</a>
15  public void Play(UnityEngine.GameObject gameObject) => Play(gameObject.GetComponent<AudioSource>());
16 
17  /// <a href="http://bit.ly/2RcqaCt">Play a random, exhaustive random or sequential sound - with random variations of volume, pitch and distance heard</a>
18  public void Play(AudioSource source) {
19  source.clip = Pick();
20  source.pitch = pitch.Pick();
21  source.volume = volume.Pick();
22  source.minDistance = distance.Min;
23  source.maxDistance = distance.Max;
24  source.Play();
25  }
26  }
27 
28  /// <a href="http://bit.ly/2RcqaCt">Create an asset to store a list of sounds and play one randomly or cyclically</a>
29  [CreateAssetMenu(menuName = "Custom Assets/Constant/Audio Clips", fileName = "AudioClips")]
30  public sealed class AudioClips : OfType<AudioClipSet> {
31  /// <a href="http://bit.ly/2RcqaCt">Retrieve a reference to a named AudioClips asset</a>
32  public new static AudioClips Instance(string name) => Instance<AudioClips>(name);
33 
34  /// <a href="http://bit.ly/2RcqaCt">Audio Clip Player <see cref="AudioClipSet"/></a>
35  public AudioClipSet Picker => Value;
36  }
37 }
Support class for audio clip playing
void Play(AudioSource source)
Play a random, exhaustive random or sequential sound - with random variations of volume, pitch and distance heard
Base class for a custom asset. Provides getters and setters for the contained value and templates for...