HotReloadOptionsSection.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace SingularityGroup.HotReload.Editor {
  4. internal class HotReloadOptionsSection {
  5. /// <remarks>
  6. /// Opening options tab does not automatically create the settings asset file.
  7. /// - The Options UI shows defaults if the object asset doesn't exist.
  8. /// - When a build starts, we also ensure the asset file exists.
  9. /// </remarks>
  10. public void DrawGUI(SerializedObject so) {
  11. so.Update(); // must update in-case asset was modified externally
  12. foreach (var option in HotReloadSettingsTab.allOptions) {
  13. GUILayout.Space(4f);
  14. DrawOption(option, so);
  15. }
  16. // commit any changes to the underlying ScriptableObject
  17. if (so.hasModifiedProperties) {
  18. so.ApplyModifiedProperties();
  19. // Ensure asset file exists on disk, because we initially create it in memory (to provide the default values)
  20. // This does not save the asset, user has to do that by saving assets in Unity (e.g. press hotkey Ctrl + S)
  21. var target = so.targetObject as HotReloadSettingsObject;
  22. if (target == null) {
  23. Log.Warning("Unexpected problem unable to save HotReloadSettingsObject");
  24. } else {
  25. // when one of the project options changed then we ensure the asset file exists.
  26. HotReloadSettingsEditor.EnsureSettingsCreated(target);
  27. }
  28. }
  29. }
  30. static void DrawOption(IOption option, SerializedObject so) {
  31. EditorGUILayout.BeginVertical(HotReloadWindowStyles.BoxStyle);
  32. var before = option.GetValue(so);
  33. var after = EditorGUILayout.BeginToggleGroup(new GUIContent(" " + option.Summary), before);
  34. if (after != before) {
  35. option.SetValue(so, after);
  36. }
  37. option.InnerOnGUI(so);
  38. EditorGUILayout.EndToggleGroup();
  39. EditorGUILayout.EndVertical();
  40. }
  41. }
  42. }