Skip to content

Commit b0d28bf

Browse files
committed
Added constructor to create a sound effect.
1 parent 436fecc commit b0d28bf

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

Runtime/SoundEffect.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,106 @@ public sealed class SoundEffect : ScriptableObject
2121

2222
#endregion
2323

24+
#region Constructors
25+
26+
/// <summary>
27+
/// Create a new sound effect instance via code.
28+
/// </summary>
29+
/// <param name="clips">A random clip is chosen when the sound effect is played.</param>
30+
/// <param name="smartRandom">Prevents the same clip from being played twice in a row.</param>
31+
/// <param name="output">Group that the audio playback should output to.</param>
32+
/// <param name="minVolume">Minimum possible volume of the audio playback [0.0 - 1.0]. Volume is chosen randomly.</param>
33+
/// <param name="maxVolume">Maximum possible volume of the audio playback [0.0 - 1.0]. Volume is chosen randomly.</param>
34+
/// <param name="minPitch">Minimum pitch of the audio playback [-3.0 - 3.0]. Pitch is chosen randomly.</param>
35+
/// <param name="maxPitch">Maximum pitch of the audio playback [-3.0 - 3.0]. Pitch is chosen randomly.</param>
36+
/// <param name="loop">Whether the audio playback should loop.</param>
37+
/// <param name="priority">Priority of the audio playback [0 - 256].</param>
38+
/// <param name="stereoPan">Pan the location of a stereo or mono audio playback [-1.0 (left) - 1.0 (right)].</param>
39+
/// <param name="spatialBlend">Amount that the audio playback is affected by spatialisation calculations [0.0 (2D) - 1.0 (3D)].</param>
40+
public SoundEffect(AudioClip[] clips, bool smartRandom = true, AudioMixerGroup output = null,
41+
float minVolume = 1f, float maxVolume = 1f, float minPitch = 1f, float maxPitch = 1f,
42+
bool loop = false, int priority = 128, float stereoPan = 0f, float spatialBlend = 0f)
43+
{
44+
Clips = clips;
45+
SmartRandom = smartRandom;
46+
Output = output;
47+
MinVolume = Mathf.Clamp01(minVolume);
48+
MaxVolume = Mathf.Clamp01(maxVolume);
49+
MinPitch = Mathf.Clamp(minPitch, -3f, 3f);
50+
MaxPitch = Mathf.Clamp(maxPitch, -3f, 3f);
51+
Loop = loop;
52+
Priority = Mathf.Clamp(priority, 0, 256);
53+
StereoPan = Mathf.Clamp(stereoPan, -1f, 1f);
54+
SpatialBlend = Mathf.Clamp01(spatialBlend);
55+
}
56+
57+
#endregion
58+
2459
#region Properties
2560

61+
/// <summary>
62+
/// A random clip is chosen when the sound effect is played.
63+
/// </summary>
2664
[field: Tooltip("A random clip is chosen when the sound effect is played."), SerializeField]
2765
public AudioClip[] Clips { get; private set; }
2866

67+
/// <summary>
68+
/// Prevents the same clip from being played twice in a row.
69+
/// </summary>
2970
[field: Tooltip("Prevents the same clip from being played twice in a row."), SerializeField]
3071
public bool SmartRandom { get; private set; } = true;
3172

73+
/// <summary>
74+
/// Group that the audio playback should output to.
75+
/// </summary>
3276
[field: Space, SerializeField]
3377
public AudioMixerGroup Output { get; private set; }
3478

79+
/// <summary>
80+
/// Minimum possible volume of the audio playback [0.0 - 1.0]. Volume is chosen randomly.
81+
/// </summary>
3582
[field: Header("Volume"), SerializeField, Range(0f, 1f)]
3683
public float MinVolume { get; private set; } = 1f;
3784

85+
/// <summary>
86+
/// Maximum possible volume of the audio playback [0.0 - 1.0]. Volume is chosen randomly.
87+
/// </summary>
3888
[field: SerializeField, Range(0f, 1f)]
3989
public float MaxVolume { get; private set; } = 1f;
4090

91+
/// <summary>
92+
/// Minimum pitch of the audio playback [-3.0 - 3.0]. Pitch is chosen randomly.
93+
/// </summary>
4194
[field: Header("Pitch"), SerializeField, Range(-3f, 3f)]
4295
public float MinPitch { get; private set; } = 1f;
4396

97+
/// <summary>
98+
/// Maximum pitch of the audio playback [-3.0 - 3.0]. Pitch is chosen randomly.
99+
/// </summary>
44100
[field: SerializeField, Range(-3f, 3f)]
45101
public float MaxPitch { get; private set; } = 1f;
46102

103+
/// <summary>
104+
/// Whether the audio playback should loop.
105+
/// </summary>
47106
[field: Space, SerializeField]
48107
public bool Loop { get; private set; }
49108

109+
/// <summary>
110+
/// Priority of the audio playback [0 - 256].
111+
/// </summary>
50112
[field: SerializeField, Range(0, 256)]
51113
public int Priority { get; private set; } = 128;
52114

115+
/// <summary>
116+
/// Pan the location of a stereo or mono audio playback [-1.0 (left) - 1.0 (right)].
117+
/// </summary>
53118
[field: SerializeField, Range(-1f, 1f)]
54119
public float StereoPan { get; private set; }
55120

121+
/// <summary>
122+
/// Amount that the audio playback is affected by spatialisation calculations [0.0 (2D) - 1.0 (3D)].
123+
/// </summary>
56124
[field: SerializeField, Range(0f, 1f)]
57125
public float SpatialBlend { get; private set; }
58126

@@ -64,9 +132,10 @@ public sealed class SoundEffect : ScriptableObject
64132
/// Play the sound effect.
65133
/// </summary>
66134
/// <param name="position">Position of the audio playback in 3D world-space.</param>
67-
public void Play(Vector3 position = default)
135+
/// <returns>Instance for controlling audio playback.</returns>
136+
public Playback Play(Vector3 position = default)
68137
{
69-
Audio.PlaySfx(this, position);
138+
return Audio.PlaySfx(this, position);
70139
}
71140

72141
internal AudioClip GetClipAtRandom()

0 commit comments

Comments
 (0)