|
| 1 | +#if UNITY_EDITOR |
| 2 | +#if AK_WWISE_ADDRESSABLES && UNITY_ADDRESSABLES |
| 3 | +using UnityEngine; |
| 4 | +namespace AK.Wwise.Unity.WwiseAddressables |
| 5 | +{ |
| 6 | + [System.Serializable] |
| 7 | + public class AkAddressablesSettings |
| 8 | + { |
| 9 | + public const string Filename = "WwiseAddressablesSettings.xml"; |
| 10 | + |
| 11 | + public static string Path |
| 12 | + { |
| 13 | + get { return System.IO.Path.Combine(UnityEngine.Application.dataPath, Filename); } |
| 14 | + } |
| 15 | + |
| 16 | + public static bool Exists { get { return System.IO.File.Exists(Path); } } |
| 17 | + |
| 18 | + public bool UseSampleMetadataPreserver; |
| 19 | + public string MetadataPath; |
| 20 | + |
| 21 | + |
| 22 | + internal static AkAddressablesSettings LoadSettings() |
| 23 | + { |
| 24 | + var settings = new AkAddressablesSettings(); |
| 25 | + |
| 26 | + try |
| 27 | + { |
| 28 | + var path = Path; |
| 29 | + if (System.IO.File.Exists(path)) |
| 30 | + { |
| 31 | + var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(AkAddressablesSettings)); |
| 32 | + using (var xmlFileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read)) |
| 33 | + settings = xmlSerializer.Deserialize(xmlFileStream) as AkAddressablesSettings; |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + var projectDir = System.IO.Path.GetDirectoryName(UnityEngine.Application.dataPath); |
| 38 | + var foundWwiseProjects = System.IO.Directory.GetFiles(projectDir, "*.wproj", System.IO.SearchOption.AllDirectories); |
| 39 | + |
| 40 | + settings.MetadataPath = "WwiseAddressablesMetadata"; |
| 41 | + settings.UseSampleMetadataPreserver = false; |
| 42 | + } |
| 43 | + } |
| 44 | + catch (System.Exception exception) |
| 45 | + { |
| 46 | + Debug.LogWarning("Could not load Wwise Addressables settings"); |
| 47 | + Debug.LogWarning(exception); |
| 48 | + } |
| 49 | + |
| 50 | + if (string.IsNullOrEmpty(settings.MetadataPath)) |
| 51 | + { |
| 52 | + settings.MetadataPath = "WwiseAddressablesMetadata"; |
| 53 | + } |
| 54 | + return settings; |
| 55 | + } |
| 56 | + |
| 57 | + public void SaveSettings() |
| 58 | + { |
| 59 | + try |
| 60 | + { |
| 61 | + var xmlDoc = new System.Xml.XmlDocument(); |
| 62 | + var xmlSerializer = new System.Xml.Serialization.XmlSerializer(GetType()); |
| 63 | + using (var xmlStream = new System.IO.MemoryStream()) |
| 64 | + { |
| 65 | + var streamWriter = new System.IO.StreamWriter(xmlStream, System.Text.Encoding.UTF8); |
| 66 | + xmlSerializer.Serialize(streamWriter, this); |
| 67 | + xmlStream.Position = 0; |
| 68 | + xmlDoc.Load(xmlStream); |
| 69 | + xmlDoc.Save(Path); |
| 70 | + } |
| 71 | + } |
| 72 | + catch |
| 73 | + { |
| 74 | + UnityEngine.Debug.LogErrorFormat("WwiseUnity: Unable to save addressables settings to file <{0}>. Please ensure that this file path can be written to.", Path); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public class AkAddressablesEditorSettings |
| 80 | + { |
| 81 | + private static AkAddressablesSettings s_Instance; |
| 82 | + |
| 83 | + public static AkAddressablesSettings Instance |
| 84 | + { |
| 85 | + get |
| 86 | + { |
| 87 | + if (s_Instance == null) |
| 88 | + s_Instance = AkAddressablesSettings.LoadSettings(); |
| 89 | + return s_Instance; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static void Reload() |
| 94 | + { |
| 95 | + s_Instance = AkAddressablesSettings.LoadSettings(); |
| 96 | + } |
| 97 | + |
| 98 | + #region GUI |
| 99 | + class SettingsProvider : UnityEditor.SettingsProvider |
| 100 | + { |
| 101 | + class Styles |
| 102 | + { |
| 103 | + public static string AddressablesSettings = "Asset metadata preservation"; |
| 104 | + |
| 105 | + public static UnityEngine.GUIContent UseSampleMetadataPreserver = new UnityEngine.GUIContent("Use Sample Metadata Preserver", "Use the sample metadata preserver to preserve Wwise addressable asset groups and labels when they are deleted."); |
| 106 | + |
| 107 | + public static UnityEngine.GUIContent MetadataPath = new UnityEngine.GUIContent("Wwise Asset Metadata Path", "Location to create the assets that will contain the addressable asset metadata."); |
| 108 | + |
| 109 | + |
| 110 | + private static UnityEngine.GUIStyle textField; |
| 111 | + public static UnityEngine.GUIStyle TextField |
| 112 | + { |
| 113 | + get |
| 114 | + { |
| 115 | + if (textField == null) |
| 116 | + textField = new UnityEngine.GUIStyle("textfield"); |
| 117 | + return textField; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static bool Ellipsis() |
| 123 | + { |
| 124 | + return UnityEngine.GUILayout.Button("...", UnityEngine.GUILayout.Width(30)); |
| 125 | + } |
| 126 | + |
| 127 | + private SettingsProvider(string path) : base(path, UnityEditor.SettingsScope.Project) { } |
| 128 | + |
| 129 | + [UnityEditor.SettingsProvider] |
| 130 | + public static UnityEditor.SettingsProvider CreateMyCustomSettingsProvider() |
| 131 | + { |
| 132 | + return new SettingsProvider("Project/Wwise Addressables") { keywords = GetSearchKeywordsFromGUIContentProperties<Styles>() }; |
| 133 | + } |
| 134 | + |
| 135 | + public override void OnGUI(string searchContext) |
| 136 | + |
| 137 | + { |
| 138 | + bool changed = false; |
| 139 | + |
| 140 | + var labelWidth = UnityEditor.EditorGUIUtility.labelWidth; |
| 141 | + UnityEditor.EditorGUIUtility.labelWidth += 100; |
| 142 | + |
| 143 | + var settings = Instance; |
| 144 | + |
| 145 | + UnityEngine.GUILayout.Space(UnityEditor.EditorGUIUtility.standardVerticalSpacing); |
| 146 | + UnityEngine.GUILayout.Label(Styles.AddressablesSettings, UnityEditor.EditorStyles.boldLabel); |
| 147 | + UnityEngine.GUILayout.Space(UnityEditor.EditorGUIUtility.standardVerticalSpacing); |
| 148 | + |
| 149 | + UnityEditor.EditorGUI.BeginChangeCheck(); |
| 150 | + |
| 151 | + using (new UnityEngine.GUILayout.VerticalScope("box")) |
| 152 | + { |
| 153 | + bool newValue = UnityEditor.EditorGUILayout.Toggle(Styles.UseSampleMetadataPreserver, settings.UseSampleMetadataPreserver); |
| 154 | + if (settings.UseSampleMetadataPreserver != newValue) |
| 155 | + { |
| 156 | + settings.UseSampleMetadataPreserver = newValue; |
| 157 | + if (settings.UseSampleMetadataPreserver) |
| 158 | + { |
| 159 | + WwiseAddressableAssetMetadataPreserver.BindMetadataDelegate(); |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + WwiseAddressableAssetMetadataPreserver.UnbindMetaDataDelegate(); |
| 164 | + } |
| 165 | + } |
| 166 | + UnityEngine.GUILayout.Space(UnityEditor.EditorGUIUtility.standardVerticalSpacing); |
| 167 | + |
| 168 | + using (new UnityEngine.GUILayout.HorizontalScope()) |
| 169 | + { |
| 170 | + |
| 171 | + |
| 172 | + if (settings.UseSampleMetadataPreserver) |
| 173 | + { |
| 174 | + UnityEditor.EditorGUILayout.PrefixLabel(Styles.MetadataPath); |
| 175 | + UnityEditor.EditorGUILayout.SelectableLabel(settings.MetadataPath, Styles.TextField, UnityEngine.GUILayout.Height(17)); |
| 176 | + if (Ellipsis()) |
| 177 | + { |
| 178 | + var OpenInPath = System.IO.Path.GetDirectoryName(AkUtilities.GetFullPath(UnityEngine.Application.dataPath, settings.MetadataPath)); |
| 179 | + var MetadataPathNew = UnityEditor.EditorUtility.OpenFolderPanel("Select your metadata Project", OpenInPath, "WwiseAddressableMetadata"); |
| 180 | + if (MetadataPathNew.Length != 0) |
| 181 | + { |
| 182 | + settings.MetadataPath = AkUtilities.MakeRelativePath(UnityEngine.Application.dataPath, MetadataPathNew); |
| 183 | + changed = true; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + if (UnityEditor.EditorGUI.EndChangeCheck()) |
| 192 | + changed = true; |
| 193 | + |
| 194 | + UnityEngine.GUILayout.Space(UnityEditor.EditorGUIUtility.standardVerticalSpacing); |
| 195 | + |
| 196 | + UnityEditor.EditorGUIUtility.labelWidth = labelWidth; |
| 197 | + |
| 198 | + if (changed) |
| 199 | + settings.SaveSettings(); |
| 200 | + } |
| 201 | + #endregion |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | +#endif //Addressables |
| 206 | +#endif // UNITY_EDITOR |
0 commit comments