|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using Code.Infrastructure.AudioVibrationFX.Services.Music; |
| 6 | +using Code.Infrastructure.AudioVibrationFX.Services.Sound; |
| 7 | +using Code.Infrastructure.AudioVibrationFX.StaticData; |
| 8 | +using Sirenix.OdinInspector; |
| 9 | +using Sirenix.OdinInspector.Editor; |
| 10 | +using UnityEditor; |
| 11 | +using UnityEngine; |
| 12 | + |
| 13 | +namespace Code.Infrastructure.AudioVibrationFX.Editor |
| 14 | +{ |
| 15 | + public class SoundLibraryEditorWindow : OdinEditorWindow |
| 16 | + { |
| 17 | + private const string SoundPath = "Assets/Code/Infrastructure/AudioVibrationFX/Services/Sound/"; |
| 18 | + private const string MusicPath = "Assets/Code/Infrastructure/AudioVibrationFX/Services/Music/"; |
| 19 | + |
| 20 | + private const string NameSpaceSound = "namespace Code.Infrastructure.AudioVibrationFX.Services.Sound"; |
| 21 | + private const string NameSpaceMusic = "namespace Code.Infrastructure.AudioVibrationFX.Services.Music"; |
| 22 | + |
| 23 | + [MenuItem("Tools/AudioVibrationKit/Sound Library")] |
| 24 | + private static void OpenWindow() |
| 25 | + { |
| 26 | + GetWindow<SoundLibraryEditorWindow>().Show(); |
| 27 | + } |
| 28 | + |
| 29 | + private SoundsData _soundsData; |
| 30 | + |
| 31 | + [BoxGroup("Existing Enums"), ReadOnly] |
| 32 | + [MultiLineProperty(4)] |
| 33 | + [SerializeField] |
| 34 | + private string _sound2DTypes; |
| 35 | + |
| 36 | + [BoxGroup("Existing Enums"), ReadOnly] |
| 37 | + [MultiLineProperty(4)] |
| 38 | + [SerializeField] |
| 39 | + private string _sound3DTypes; |
| 40 | + |
| 41 | + [BoxGroup("Existing Enums"), ReadOnly] |
| 42 | + [MultiLineProperty(4)] |
| 43 | + [SerializeField] |
| 44 | + private string _musicTypes; |
| 45 | + |
| 46 | + [BoxGroup("2D Sounds")] |
| 47 | + [ShowInInspector, Searchable] |
| 48 | + [ListDrawerSettings( |
| 49 | + Expanded = true, |
| 50 | + DraggableItems = true, |
| 51 | + ShowPaging = true, |
| 52 | + ListElementLabelName = "Name" |
| 53 | + )] |
| 54 | + private List<SoundData> _sounds2DDataEditable; |
| 55 | + |
| 56 | + [BoxGroup("3D Sounds")] |
| 57 | + [ShowInInspector, Searchable] |
| 58 | + [ListDrawerSettings( |
| 59 | + Expanded = true, |
| 60 | + DraggableItems = true, |
| 61 | + ShowPaging = true, |
| 62 | + ListElementLabelName = "Name" |
| 63 | + )] |
| 64 | + private List<Sound3DData> _sounds3DDataEditable; |
| 65 | + |
| 66 | + [BoxGroup("Music")] |
| 67 | + [ShowInInspector, Searchable] |
| 68 | + [ListDrawerSettings( |
| 69 | + Expanded = true, |
| 70 | + DraggableItems = true, |
| 71 | + ShowPaging = true, |
| 72 | + ListElementLabelName = "Name" |
| 73 | + )] |
| 74 | + private List<SoundData> _musicDataEditable; |
| 75 | + |
| 76 | + private string _namespaceCodeInfrastructureAudiovibrationfxServicesSound; |
| 77 | + |
| 78 | + [BoxGroup("Generation")] |
| 79 | + [Button("Generate Enums", ButtonSizes.Large)] |
| 80 | + [GUIColor(0f, 1f, 0f)] |
| 81 | + private void GenerateEnums() |
| 82 | + { |
| 83 | + GenerateSound2DEnumFile("Sound2DType.cs", "Sound2DType", _sounds2DDataEditable); |
| 84 | + GenerateSound3DEnumFile("Sound3DType.cs", "Sound3DType", _sounds3DDataEditable); |
| 85 | + GenerateMusicEnumFile("MusicType.cs", "MusicType", _musicDataEditable); |
| 86 | + SaveSoundsData(); |
| 87 | + AssetDatabase.SaveAssets(); |
| 88 | + AssetDatabase.Refresh(); |
| 89 | + } |
| 90 | + |
| 91 | + private void GenerateSound2DEnumFile(string fileName, string enumName, List<SoundData> soundList) |
| 92 | + { |
| 93 | + var enumPath = $"{SoundPath}{fileName}"; |
| 94 | + GenerateEnumFileBase(enumPath, enumName, NameSpaceSound, soundList, TypeSound.Sound2D); |
| 95 | + } |
| 96 | + |
| 97 | + private void GenerateSound3DEnumFile(string fileName, string enumName, List<Sound3DData> soundList) |
| 98 | + { |
| 99 | + var enumPath = $"{SoundPath}{fileName}"; |
| 100 | + List<SoundData> baseList = soundList.Cast<SoundData>().ToList(); |
| 101 | + GenerateEnumFileBase(enumPath, enumName, NameSpaceSound ,baseList, TypeSound.Sound3D); |
| 102 | + } |
| 103 | + |
| 104 | + private void GenerateMusicEnumFile(string fileName, string enumName, List<SoundData> soundList) |
| 105 | + { |
| 106 | + var enumPath = $"{MusicPath}{fileName}"; |
| 107 | + GenerateEnumFileBase(enumPath, enumName, NameSpaceMusic, soundList, TypeSound.Music); |
| 108 | + } |
| 109 | + |
| 110 | + private void GenerateEnumFileBase(string enumPath, string enumName, string nameSpace ,List<SoundData> soundList, TypeSound typeSound) |
| 111 | + { |
| 112 | + var names = soundList |
| 113 | + .Where(s => !string.IsNullOrWhiteSpace(s.Name)) |
| 114 | + .Select(s => s.Name.Replace(" ", "_").Replace("-", "_").Replace(".", "_").Trim()) |
| 115 | + .Distinct() |
| 116 | + .ToList(); |
| 117 | + |
| 118 | + using (var writer = new StreamWriter(enumPath)) |
| 119 | + { |
| 120 | + writer.WriteLine("using System;"); |
| 121 | + writer.WriteLine(); |
| 122 | + writer.WriteLine(nameSpace); |
| 123 | + writer.WriteLine("{"); |
| 124 | + writer.WriteLine(" [Serializable]"); |
| 125 | + writer.WriteLine($" public enum {enumName}"); |
| 126 | + writer.WriteLine(" {"); |
| 127 | + writer.WriteLine(" Unknown = -1,"); |
| 128 | + |
| 129 | + for (int i = 0; i < names.Count; i++) |
| 130 | + { |
| 131 | + writer.WriteLine($" {names[i]} = {i},"); |
| 132 | + } |
| 133 | + |
| 134 | + writer.WriteLine(" }"); |
| 135 | + writer.WriteLine("}"); |
| 136 | + } |
| 137 | + |
| 138 | + foreach (var sound in soundList) |
| 139 | + { |
| 140 | + var enumNameSanitized = sound.Name.Replace(" ", "_").Replace("-", "_").Replace(".", "_").Trim(); |
| 141 | + |
| 142 | + switch (typeSound) |
| 143 | + { |
| 144 | + case TypeSound.Sound2D when Enum.TryParse(enumNameSanitized, out Sound2DType sound2DType): |
| 145 | + sound.Sound2DType = sound2DType; |
| 146 | + break; |
| 147 | + case TypeSound.Sound3D when Enum.TryParse(enumNameSanitized, out Sound3DType sound3DType): |
| 148 | + sound.Sound3DType = sound3DType; |
| 149 | + break; |
| 150 | + case TypeSound.Music when Enum.TryParse(enumNameSanitized, out MusicType musicType): |
| 151 | + sound.MusicType = musicType; |
| 152 | + break; |
| 153 | + default: |
| 154 | + throw new ArgumentOutOfRangeException(nameof(typeSound), typeSound, null); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + EditorUtility.SetDirty(_soundsData); |
| 159 | + AssetDatabase.SaveAssets(); |
| 160 | + AssetDatabase.Refresh(); |
| 161 | + |
| 162 | + Debug.Log($"{enumName} enum generated and assigned successfully!"); |
| 163 | + } |
| 164 | + |
| 165 | + public void UpdateSoundTypesAfterReload() |
| 166 | + { |
| 167 | + UpdateSoundTypes(); |
| 168 | + SaveSoundsData(); |
| 169 | + AssetDatabase.SaveAssets(); |
| 170 | + AssetDatabase.Refresh(); |
| 171 | + } |
| 172 | + |
| 173 | + protected override void OnEnable() |
| 174 | + { |
| 175 | + base.OnEnable(); |
| 176 | + |
| 177 | + var loaded = Resources.Load<SoundsData>("StaticData/Sounds/Sounds"); |
| 178 | + |
| 179 | + if (loaded != null) |
| 180 | + { |
| 181 | + _soundsData = loaded; |
| 182 | + _sounds2DDataEditable = _soundsData.Sounds2DData; |
| 183 | + _sounds3DDataEditable = _soundsData.Sounds3DData; |
| 184 | + _musicDataEditable = _soundsData.MusicData; |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + Debug.LogError("SoundsData asset not found at Resources/StaticData/Sounds/Sounds.asset"); |
| 189 | + _sounds2DDataEditable = new List<SoundData>(); |
| 190 | + _sounds3DDataEditable = new List<Sound3DData>(); |
| 191 | + _musicDataEditable = new List<SoundData>(); |
| 192 | + } |
| 193 | + |
| 194 | + _sound2DTypes = GetEnumValues(typeof(Sound2DType)); |
| 195 | + _sound3DTypes = GetEnumValues(typeof(Sound3DType)); |
| 196 | + _musicTypes = GetEnumValues(typeof(MusicType)); |
| 197 | + } |
| 198 | + |
| 199 | + protected override void OnDisable() |
| 200 | + { |
| 201 | + base.OnDisable(); |
| 202 | + SaveSoundsData(); |
| 203 | + } |
| 204 | + |
| 205 | + private void SaveSoundsData() |
| 206 | + { |
| 207 | + if (_soundsData != null) |
| 208 | + EditorUtility.SetDirty(_soundsData); |
| 209 | + } |
| 210 | + |
| 211 | + private void UpdateSoundTypes() |
| 212 | + { |
| 213 | + foreach (var sound in _sounds2DDataEditable) |
| 214 | + sound.Sound2DType = Enum.TryParse(sound.Name, out Sound2DType parsedType) ? parsedType : Sound2DType.Unknown; |
| 215 | + |
| 216 | + foreach (var sound in _sounds3DDataEditable) |
| 217 | + sound.Sound3DType = Enum.TryParse(sound.Name, out Sound3DType parsedType) ? parsedType : Sound3DType.Unknown; |
| 218 | + |
| 219 | + foreach (var music in _musicDataEditable) |
| 220 | + music.MusicType = Enum.TryParse(music.Name, out MusicType parsedType) ? parsedType : MusicType.Unknown; |
| 221 | + |
| 222 | + EditorUtility.SetDirty(_soundsData); |
| 223 | + AssetDatabase.SaveAssets(); |
| 224 | + } |
| 225 | + |
| 226 | + private string GetEnumValues(Type enumType) |
| 227 | + { |
| 228 | + return string.Join(", ", Enum.GetNames(enumType)); |
| 229 | + } |
| 230 | + |
| 231 | + private enum TypeSound |
| 232 | + { |
| 233 | + Sound2D, |
| 234 | + Sound3D, |
| 235 | + Music |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments