Skip to content

Commit 35abc15

Browse files
author
Markus
committed
Added UIElementsHelper
1 parent 44452c6 commit 35abc15

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [0.1.0] - 2022-11-02
1010
### Added
11-
- Initial release
11+
- Initial release
12+
13+
## [0.1.1] - 2023-03-29
14+
### Added
15+
- UIElementsHelper

Editor/UIElementsHelper.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using UnityEditor;
3+
using UnityEditor.UIElements;
4+
using UnityEngine.UIElements;
5+
6+
public static class UIElementsHelper {
7+
/// <summary>
8+
/// Create a PropertyField with a callback.
9+
/// </summary>
10+
/// <param name="property">SerializedProperty that should be used to create the PropertyField.</param>
11+
/// <param name="changeCallback">Callback that gets triggered if a value changed.</param>
12+
/// <returns></returns>
13+
public static PropertyField CreatePropertyFieldWithCallback(SerializedProperty property, EventCallback<SerializedPropertyChangeEvent> changeCallback = null) {
14+
PropertyField propertyField = new PropertyField(property);
15+
if (changeCallback != null) {
16+
propertyField.RegisterCallback(changeCallback);
17+
}
18+
return propertyField;
19+
}
20+
21+
/// <summary>
22+
/// This is an alternative to InspectorElement.FillDefaultInspector that works without having to provide an editor object.
23+
/// </summary>
24+
/// <param name="serializedObject"></param>
25+
/// <param name="root">The UI root object.</param>
26+
/// <param name="changeCallback">Optional: Callback fired when a property changed.</param>
27+
/// <param name="CreateAdditionalUI">Optional: A method executed to create additional UI for every row.</param>
28+
/// <param name="rootPropertyPath">Optional: A relative property path that can be provided to start with a different property inside the serializedObject.</param>
29+
public static void CreateGenericUI(SerializedObject serializedObject, VisualElement root, EventCallback<SerializedPropertyChangeEvent> changeCallback = null, System.Action<SerializedProperty, VisualElement> CreateAdditionalUI = null, string rootPropertyPath = null) {
30+
31+
SerializedProperty rootProperty = null;
32+
if (rootPropertyPath != null) {
33+
rootProperty = serializedObject.FindProperty(rootPropertyPath);
34+
}
35+
36+
Action<SerializedProperty, ScrollView> creationLogic;
37+
if (CreateAdditionalUI != null) {
38+
creationLogic = (prop, scrollView) => {
39+
VisualElement container = new VisualElement();
40+
container.AddToClassList(nameof(container) + nameof(PropertyField));
41+
container.Add(CreatePropertyFieldWithCallback(prop, changeCallback));
42+
CreateAdditionalUI(prop, container);
43+
scrollView.Add(container);
44+
};
45+
} else {
46+
creationLogic = (prop, scrollView) => {
47+
scrollView.Add(CreatePropertyFieldWithCallback(prop, changeCallback));
48+
};
49+
}
50+
51+
void ForEachProperty(ref ScrollView scrollView) {
52+
SerializedProperty prop = rootProperty == null ? serializedObject.GetIterator() : rootProperty;
53+
if (prop.NextVisible(true)) {
54+
do {
55+
if (prop.name != "m_Script") {
56+
creationLogic(prop.Copy(), scrollView);
57+
}
58+
}
59+
while (prop.NextVisible(false));
60+
}
61+
}
62+
63+
ScrollView scrollView = new ScrollView();
64+
scrollView.AddToClassList("propertyList" + nameof(ScrollView));
65+
root.Add(scrollView);
66+
ForEachProperty(ref scrollView);
67+
68+
}
69+
}
70+

Editor/UIElementsHelper.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.gentlymadstudios.editorui",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"displayName": "EditorUI",
55
"description": "Editor UI by Gentlymad Studios.",
66
"unity": "2022.1",

0 commit comments

Comments
 (0)