Skip to content

Commit

Permalink
Update project settings provider to use VisualElements
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Jul 15, 2024
1 parent b4c29de commit fd640b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Better.ProjectSettings.Runtime;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

namespace Better.ProjectSettings.EditorAddons
{
Expand All @@ -12,9 +14,9 @@ protected DefaultProjectSettingsProvider(string path, SettingsScope scopes = Set
_editor = Editor.CreateEditor(_settings);
}

protected override void DrawGUI()
protected override void CreateVisualElements(VisualElement rootElement)
{
_editor.OnInspectorGUI();
InspectorElement.FillDefaultInspector(rootElement, _settingsObject, _editor);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System.IO;
using Better.Commons.Runtime.Extensions;
using Better.Internal.Core.Runtime;
using Better.ProjectSettings.Runtime;
using Better.Singletons.Runtime;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace Better.ProjectSettings.EditorAddons
{
public abstract class ProjectSettingsProvider<T> : SettingsProvider where T : ScriptableSettings<T>
{
protected readonly T _settings;
protected readonly SerializedObject _settingsObject;
private GUIStyle _style;
private const int Space = 8;
public const string ProjectPath = PrefixConstants.ProjectPrefix + "/";

protected ProjectSettingsProvider(string path, SettingsScope scope = SettingsScope.Project)
Expand All @@ -23,29 +24,22 @@ protected ProjectSettingsProvider(string path, SettingsScope scope = SettingsSco
label = Path.GetFileName(path);
}

public override void OnGUI(string searchContext)
public override void OnActivate(string searchContext, VisualElement rootElement)
{
var style = CreateOrGetStyle();
using (new EditorGUILayout.VerticalScope(style))
{
DrawGUI();
}

_settingsObject.ApplyModifiedPropertiesWithoutUndo();
CreateVisualElements(rootElement);
rootElement.Bind(_settingsObject);
rootElement.RegisterCallback<SerializedObjectChangeEvent>(OnObjectChanged);
}

private GUIStyle CreateOrGetStyle()
protected virtual void OnObjectChanged(SerializedObjectChangeEvent changeEvent)
{
if (_style != null)
{
return _style;
}
changeEvent.changedObject.ApplyModifiedPropertiesWithoutUndo();
}

_style = new GUIStyle();
_style.margin = new RectOffset(Space, Space, Space, Space);
return _style;
public override void OnDeactivate()
{
}

protected abstract void DrawGUI();
protected abstract void CreateVisualElements(VisualElement rootElement);
}
}

0 comments on commit fd640b5

Please sign in to comment.