Skip to content

Commit

Permalink
- Fixed settings file not serializing correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doppelkeks committed Mar 9, 2023
1 parent 23d846b commit ee69b15
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 111 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.1.7] - 2023-03-09
### Added
- Added ability for EditableLabelFields to be exited via escape or return

## [0.1.8] - 2023-03-09
### Fixed
- Fixed settings file not serializing correctly.
24 changes: 21 additions & 3 deletions Editor/Helpers/UIElementsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
namespace NewGraph {
public static class UIElementsHelper {

/// <summary>
/// Create a PropertyField with a callback.
/// </summary>
/// <param name="property">SerializedProperty that should be used to create the PropertyField.</param>
/// <param name="changeCallback">Callback that gets triggered if a value changed.</param>
/// <returns></returns>
public static PropertyField CreatePropertyFieldWithCallback(SerializedProperty property, EventCallback<SerializedPropertyChangeEvent> changeCallback = null) {
PropertyField propertyField = new PropertyField(property);
if (changeCallback != null) {
Expand All @@ -14,7 +20,20 @@ public static PropertyField CreatePropertyFieldWithCallback(SerializedProperty p
return propertyField;
}

public static void CreateGenericUI(SerializedObject serializedObject, VisualElement root, EventCallback<SerializedPropertyChangeEvent> changeCallback = null, System.Action<SerializedProperty, VisualElement> CreateAdditionalUI = null) {
/// <summary>
/// This is an alternative to InspectorElement.FillDefaultInspector that works without having to provide an editor object.
/// </summary>
/// <param name="serializedObject"></param>
/// <param name="root">The UI root object.</param>
/// <param name="changeCallback">Optional: Callback fired when a property changed.</param>
/// <param name="CreateAdditionalUI">Optional: A method executed to create additional UI for every row.</param>
/// <param name="rootPropertyPath">Optional: A relative property path that can be provided to start with a different property inside the serializedObject.</param>
public static void CreateGenericUI(SerializedObject serializedObject, VisualElement root, EventCallback<SerializedPropertyChangeEvent> changeCallback = null, System.Action<SerializedProperty, VisualElement> CreateAdditionalUI = null, string rootPropertyPath = null) {

SerializedProperty rootProperty = null;
if (rootPropertyPath != null) {
rootProperty = serializedObject.FindProperty(rootPropertyPath);
}

Action<SerializedProperty, ScrollView> creationLogic;
if (CreateAdditionalUI != null) {
Expand All @@ -32,7 +51,7 @@ public static void CreateGenericUI(SerializedObject serializedObject, VisualElem
}

void ForEachProperty(ref ScrollView scrollView) {
SerializedProperty prop = serializedObject.GetIterator();
SerializedProperty prop = rootProperty == null ? serializedObject.GetIterator(): rootProperty;
if (prop.NextVisible(true)) {
do {
if (prop.name != "m_Script") {
Expand All @@ -47,7 +66,6 @@ void ForEachProperty(ref ScrollView scrollView) {
scrollView.AddToClassList("propertyList"+nameof(ScrollView));
root.Add(scrollView);
ForEachProperty(ref scrollView);
root.Bind(serializedObject);

}
}
Expand Down
73 changes: 37 additions & 36 deletions Editor/Settings/GraphSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,40 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 237dcb76433844e4480d927e69af7b6b, type: 3}
m_Name: GraphSettings
m_EditorClassIdentifier:
customStylesheet: {fileID: 0}
searchWindowCommandHeader: Commands
searchWindowRootHeader: Create Nodes
openGraphButtonText: Open Graph
wasBlueprintTransferred: 1
hideInspectorIcon: SceneLoadIn
showInspectorIcon: SceneLoadOut
homeButtonIcon: d_SceneViewCamera@2x
createButtonIcon: d_Toolbar Plus
loadButtonIcon: d_FolderOpened Icon
resetButtonIcon: Refresh@2x
createUtilityNodeLabel: Create Utility
createNodeLabel: Create Node
noGraphLoadedLabel: No graph active!
multipleSelectedMessagePartial: selected. Select a single Node to change any exposed
values.
node: node
edge: edge
edgeWidthSelected: 4
edgeWidthUnselected: 3
disabledPortColor: {r: 0.27450982, g: 0.27450982, b: 0.27450982, a: 0.27}
portColor: {r: 0.9411765, g: 0.9411765, b: 1, a: 0.95}
colorSelected: {r: 0.9411765, g: 0.9411765, b: 0.9411765, a: 1}
colorUnselected: {r: 0.57254905, g: 0.57254905, b: 0.57254905, a: 1}
defaultNodeColor: {r: 0.21960786, g: 0.21960786, b: 0.21960786, a: 1}
defaultLabelForPortListItems: Empty
defaultInputName: Input
nodeWidth: 210
inspectorWidth: 290
baseGraphPathPartial: Project/Graphs
loggerColor: {r: 0, g: 0.49803922, b: 0, a: 1}
handleBarsPartialIdentifier: 4e7bd4c2a267fc147b96af42ce53487c
graphDocumentIdentifier: a1a3b33cb142d06479dfaa381cc82245
graphStylesheetVariablesIdentifier: a22fe40a267e5824dbde2cea493f40e4
graphStylesheetIdentifier: e2825f280a2499d4587129e0a3716e17
settingsStylesheetIdentifier: 7d7e4dcd278879849b62a784de9bdc3c
graphSettings:
customStylesheet: {fileID: 0}
searchWindowCommandHeader: Commands
searchWindowRootHeader: Create Nodes
openGraphButtonText: Open Graph
wasBlueprintTransferred: 0
hideInspectorIcon: SceneLoadIn
showInspectorIcon: SceneLoadOut
homeButtonIcon: d_SceneViewCamera@2x
createButtonIcon: d_Toolbar Plus
loadButtonIcon: d_FolderOpened Icon
resetButtonIcon: Refresh@2x
createUtilityNodeLabel: Create Utility
createNodeLabel: Create Node
noGraphLoadedLabel: No graph active!
multipleSelectedMessagePartial: selected. Select a single node to change any
exposed values.
node: node
edge: edge
edgeWidthSelected: 4
edgeWidthUnselected: 3
disabledPortColor: {r: 0.27450982, g: 0.27450982, b: 0.27450982, a: 0.27}
portColor: {r: 0.9411765, g: 0.9411765, b: 1, a: 0.95}
colorSelected: {r: 0.9411765, g: 0.9411765, b: 0.9411765, a: 1}
colorUnselected: {r: 0.57254905, g: 0.57254905, b: 0.57254905, a: 1}
defaultNodeColor: {r: 0.21960786, g: 0.21960786, b: 0.21960786, a: 1}
defaultLabelForPortListItems: Empty
defaultInputName: input
nodeWidth: 210
inspectorWidth: 290
baseGraphPathPartial: Project/Graphs
loggerColor: {r: 0, g: 0.49803922, b: 0, a: 1}
handleBarsPartialIdentifier: 4e7bd4c2a267fc147b96af42ce53487c
graphDocumentIdentifier: a1a3b33cb142d06479dfaa381cc82245
graphStylesheetVariablesIdentifier: a22fe40a267e5824dbde2cea493f40e4
graphStylesheetIdentifier: e2825f280a2499d4587129e0a3716e17
settingsStylesheetIdentifier: 7d7e4dcd278879849b62a784de9bdc3c
25 changes: 11 additions & 14 deletions Editor/Settings/GraphSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
using static NewGraph.GraphSettingsSingleton;

namespace NewGraph {

[CreateAssetMenu(fileName = nameof(GraphSettings), menuName = nameof(GraphSettings), order = 1)]
public class GraphSettings : ScriptableObject {
[System.Serializable]
public class GraphSettings {
public const string assetGUID = "015778251503b3c44a2b9dfc3a36ad10";
public const string menuItemBase = "Tools/";
public const string debugDefine = "TOOLS_DEBUG";
public const string lastOpenedGraphEditorPrefsKey = nameof(NewGraph) + "." + nameof(GraphSettings) + "." + nameof(lastOpenedGraphEditorPrefsKey);
public const string lastOpenedDirectoryPrefsKey = nameof(NewGraph) + "." + nameof(GraphSettings) + "." + nameof(lastOpenedDirectoryPrefsKey);
public const string isInspectorVisiblePrefsKey = nameof(NewGraph) + "." + nameof(GraphSettings) + "." + nameof(isInspectorVisiblePrefsKey);
public const string lastOpenedGraphEditorPrefsKey = nameof(NewGraph) + "." + nameof(GraphSettingsAsset) + "." + nameof(lastOpenedGraphEditorPrefsKey);
public const string lastOpenedDirectoryPrefsKey = nameof(NewGraph) + "." + nameof(GraphSettingsAsset) + "." + nameof(lastOpenedDirectoryPrefsKey);
public const string isInspectorVisiblePrefsKey = nameof(NewGraph) + "." + nameof(GraphSettingsAsset) + "." + nameof(isInspectorVisiblePrefsKey);

public StyleSheet customStylesheet;

Expand All @@ -23,7 +22,7 @@ public class GraphSettings : ScriptableObject {
public static string PathPartialToCategory {
get {
if (pathPartialToCategory == null) {
pathPartialToCategory = menuItemBase+nameof(GraphSettings);
pathPartialToCategory = menuItemBase + nameof(GraphSettings);
}
return pathPartialToCategory;
}
Expand Down Expand Up @@ -76,16 +75,14 @@ public static GraphModel LastOpenedGraphModel {
[SerializeField]
private string loadButtonIcon = "d_FolderOpened Icon";
public static Image LoadButtonIcon => new Image() { image = EditorGUIUtility.IconContent(Settings.loadButtonIcon).image };

[SerializeField]
private string resetButtonIcon = "Refresh@2x";
public static Image ResetButtonIcon => new Image() { image = EditorGUIUtility.IconContent(Settings.resetButtonIcon).image };

/*
[SerializeField]
private string saveButtonIcon = "d_SaveAs@2x";
public static Image SaveButtonIcon => new Image() { image = EditorGUIUtility.IconContent(Instance.saveButtonIcon).image };
*/
public string resetButtonTooltip = "Reset this value back to the default value.";
public string resetAllLabel = "Reset All To Default";
public string resetAllTooltip = "Reset All values to default values.";

public string createUtilityNodeLabel = "Create Utility";
public string createNodeLabel = "Create Node";
Expand All @@ -112,7 +109,7 @@ public static GraphModel LastOpenedGraphModel {
private string baseGraphPathPartial = string.Empty;
[SerializeField]
private Color loggerColor = Color.green;

[NonSerialized]
private string loggerColorHex = null;
public static string LoggerColorHex {
Expand Down
2 changes: 1 addition & 1 deletion Editor/Settings/GraphSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Editor/Settings/GraphSettingsAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UnityEngine;

namespace NewGraph {

//[CreateAssetMenu(fileName = nameof(GraphSettingsAsset), menuName = nameof(GraphSettingsAsset), order = 1)]
public class GraphSettingsAsset : ScriptableObject {
public GraphSettings graphSettings = new GraphSettings();
}
}
11 changes: 11 additions & 0 deletions Editor/Settings/GraphSettingsAsset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 39 additions & 16 deletions Editor/Settings/GraphSettingsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using UnityEngine;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
Expand All @@ -23,47 +22,71 @@ protected override Action<SerializedProperty, VisualElement> GetCreateAdditional
return CreateAdditionalUI;
}

protected override string GetRootPropertyPath() {
return nameof(GraphSettingsAsset.graphSettings);
}

public override void OnActivate(string searchContext, VisualElement rootElement) {
base.OnActivate(searchContext, rootElement);

// create a button to reset all properties back to the bleuprint value
Button resetAll = new Button(() => {
CopyAllFromBlueprint(serializedObject);
});
resetAll.text = Settings.resetAllLabel;
resetAll.tooltip = Settings.resetAllTooltip;
resetAll.AddToClassList(nameof(resetAll));
rootElement[0].Insert(1, resetAll);
// add a custom stylesheet
rootElement.styleSheets.Add(GraphSettings.settingsStylesheet);
rootElement.Add(new Button(() => {
EditorUtility.CopySerialized(BlueprintSettings, Settings);
new SerializedObject(Settings).ApplyModifiedProperties();
}) { text = "Reset All To Default"});
}

/// <summary>
/// Executed after every PropertyField.
/// We'll attach a reset button here.
/// </summary>
/// <param name="property"></param>
/// <param name="container"></param>
private void CreateAdditionalUI(SerializedProperty property, VisualElement container) {
// create a resetButton per Row
Button resetButton= new Button(() => {
SerializedProperty blueprintProperty = new SerializedObject(BlueprintSettings).FindProperty(property.propertyPath);
property.serializedObject.CopyFromSerializedProperty(blueprintProperty);
property.serializedObject.ApplyModifiedProperties();
// if reset is executed, copy the property from the blueprint to the property
CopyFromBlueprint(property);
});
resetButton.AddToClassList(nameof(resetButton));
resetButton.Add(GraphSettings.ResetButtonIcon);
resetButton.tooltip = "Reset this value back to the default value.";
resetButton.tooltip = Settings.resetButtonTooltip;
container.Add(resetButton);
}

/// <summary>
/// Called when any value changed.
/// </summary>
/// <param name="evt"></param>
private void ValueChanged(SerializedPropertyChangeEvent evt) {
// notify all listeneres (ReactiveSettings)
Settings.NotifyValueChanged(evt);
serializedObject.ApplyModifiedProperties();
// call save on our singleton as it is a strange hybrid and not a full ScriptableObject
Save();
}

// Register the SettingsProvider
[SettingsProvider]
public static SettingsProvider CreateMyCustomSettingsProvider() {
return Settings ? new GraphSettingsProvider() : null;
return Settings != null ? new GraphSettingsProvider() : null;
}

protected override string GetHeader() {
return nameof(GraphSettings);
}

public override Type GetDataType() {
return typeof(GraphSettings);
return typeof(GraphSettingsAsset);
}

public override dynamic GetInstance() {
return Settings;
return GraphSettingsSingleton.instance.settingsAsset;
}

protected override void OnChange() {
Save();
}
}
}
Loading

0 comments on commit ee69b15

Please sign in to comment.