Repository navigation
Expand file tree
/
Copy pathAudioDatabase.cs
More file actions
107 lines (91 loc) · 2.86 KB
/
Copy pathAudioDatabase.cs
File metadata and controls
107 lines (91 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using UnityEngine;
using FMODUnity;
[CreateAssetMenu(
fileName = "AudioDatabase",
menuName = "Audio/Audio Database"
)]
public class AudioDatabase : ScriptableObject
{
[SerializeField]
private List<AudioDefinition> entries = new List<AudioDefinition>();
private void OnValidate()
{
ValidateEntries();
}
[ContextMenu("Validate Audio Database")]
private void ValidateDatabase()
{
if (ValidateEntries())
{
Debug.Log($"AudioDatabase '{name}' validation passed ({entries.Count} entries).", this);
}
}
private bool ValidateEntries()
{
bool isValid = true;
var firstEntryByID = new Dictionary<AudioID, int>();
for (int i = 0; i < entries.Count; i++)
{
AudioDefinition entry = entries[i];
string entryLabel = $"AudioDatabase '{name}', entry {i + 1}";
if (entry == null)
{
Debug.LogWarning($"{entryLabel} is null. Remove it or configure an audio entry.", this);
isValid = false;
continue;
}
if (entry.ID == AudioID.None || !Enum.IsDefined(typeof(AudioID), entry.ID))
{
Debug.LogWarning($"{entryLabel} has an invalid AudioID '{entry.ID}'. Select a valid sound ID.", this);
isValid = false;
}
if (firstEntryByID.TryGetValue(entry.ID, out int firstIndex))
{
Debug.LogWarning(
$"{entryLabel} duplicates AudioID '{entry.ID}' from entry {firstIndex + 1}. Each ID must be unique.",
this
);
isValid = false;
}
else
{
firstEntryByID.Add(entry.ID, i);
}
if (entry.EventReference.IsNull)
{
Debug.LogWarning($"{entryLabel} ('{entry.ID}') has no FMOD event assigned.", this);
isValid = false;
}
if (!entry.ValidateSettings(this, entryLabel))
{
isValid = false;
}
}
return isValid;
}
public bool TryGetEvent(AudioID id, out EventReference eventReference)
{
if (TryGetDefinition(id, out AudioDefinition definition))
{
eventReference = definition.EventReference;
return true;
}
eventReference = default;
return false;
}
public bool TryGetDefinition(AudioID id, out AudioDefinition definition)
{
foreach (AudioDefinition entry in entries)
{
if (entry != null && entry.ID == id)
{
definition = entry;
return !entry.EventReference.IsNull;
}
}
definition = null;
return false;
}
}