-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ui toolkit network behaviour editor for unity 2022.2 and newer (#…
…1121) Squashed commit of the following: commit 53f5515 Author: Hertzole <hampusringstrom@gmail.com> Date: Wed Dec 28 15:38:55 2022 +0100 moved UI toolkit code to own files commit 9b2e013 Author: Hertzole <hampusringstrom@gmail.com> Date: Sat Dec 24 02:32:26 2022 +0100 add foldout event ui toolkit editor commit 765f919 Author: Hertzole <hampusringstrom@gmail.com> Date: Sat Dec 24 02:21:11 2022 +0100 add readonly decortator ui toolkit editor commit 94e2c53 Author: Hertzole <hampusringstrom@gmail.com> Date: Sat Dec 24 02:15:56 2022 +0100 add syncvar ui toolkit editor commit a514494 Author: Hertzole <hampusringstrom@gmail.com> Date: Sat Dec 24 02:06:32 2022 +0100 made imgui editor match ui toolkit editor You can now click the label to expand sync lists and it has a new empty state, along with an outlined box. commit ec345d8 Author: Hertzole <hampusringstrom@gmail.com> Date: Sat Dec 24 01:59:52 2022 +0100 add ui toolkit inspector
- Loading branch information
1 parent
8fb7264
commit f626c77
Showing
12 changed files
with
324 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#if UNITY_2022_2_OR_NEWER | ||
using UnityEditor; | ||
using UnityEditor.UIElements; | ||
using UnityEngine.UIElements; | ||
|
||
namespace Mirage | ||
{ | ||
public partial class FoldoutEventDrawer | ||
{ | ||
public override VisualElement CreatePropertyGUI(SerializedProperty property) | ||
{ | ||
var foldout = new Foldout { text = property.displayName }; | ||
|
||
foldout.Add(UnityEventDrawer.CreatePropertyGUI(property)); | ||
|
||
foldout.BindProperty(property); | ||
|
||
return foldout; | ||
} | ||
} | ||
} | ||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
Assets/Mirage/Editor/NetworkBehaviourInspectorUIToolkit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
#if UNITY_2022_2_OR_NEWER // Unity uses UI toolkit by default for inspectors in 2022.2 and up. | ||
using UnityEditor.UIElements; | ||
using UnityEngine.UIElements; | ||
using System.Globalization; | ||
using UnityEngine; | ||
|
||
namespace Mirage | ||
{ | ||
#if !EXCLUDE_NETWORK_BEHAVIOUR_INSPECTOR | ||
public partial class NetworkBehaviourInspector | ||
{ | ||
public override VisualElement CreateInspectorGUI() | ||
{ | ||
var root = new VisualElement(); | ||
|
||
// Create the default inspector. | ||
var iterator = serializedObject.GetIterator(); | ||
for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false) | ||
{ | ||
var field = new PropertyField(iterator); | ||
field.Bind(serializedObject); | ||
|
||
// Disable the script field. | ||
if (iterator.propertyPath == "m_Script") | ||
{ | ||
field.SetEnabled(false); | ||
} | ||
|
||
root.Add(field); | ||
} | ||
|
||
// Create the sync lists editor. | ||
var syncLists = _drawer.CreateDefaultSyncLists(); | ||
if (syncLists != null) | ||
{ | ||
root.Add(syncLists); | ||
} | ||
|
||
// Crete the sync settings editor. | ||
var syncSettings = _drawer.CreateDefaultSyncSettings(); | ||
if (syncSettings != null) | ||
{ | ||
root.Add(syncSettings); | ||
} | ||
|
||
return root; | ||
} | ||
} | ||
#endif // !EXCLUDE_NETWORK_BEHAVIOUR_INSPECTOR | ||
|
||
public partial class NetworkBehaviourInspectorDrawer | ||
{ | ||
public VisualElement CreateDefaultSyncLists() | ||
{ | ||
return _syncListDrawer?.Create(); | ||
} | ||
|
||
public VisualElement CreateDefaultSyncSettings() | ||
{ | ||
if (!_syncsAnything) | ||
{ | ||
return null; | ||
} | ||
|
||
var root = new VisualElement(); | ||
|
||
root.Add(CreateHeader("Sync Settings")); | ||
|
||
var syncMode = new PropertyField(_serializedObject.FindProperty("syncMode")); | ||
syncMode.Bind(_serializedObject); | ||
root.Add(syncMode); | ||
|
||
var syncInterval = new PropertyField(_serializedObject.FindProperty("syncInterval")); | ||
syncInterval.Bind(_serializedObject); | ||
root.Add(syncInterval); | ||
|
||
return root; | ||
} | ||
|
||
public static VisualElement CreateHeader(string text) | ||
{ | ||
var root = new VisualElement(); | ||
root.AddToClassList("unity-decorator-drawers-container"); | ||
|
||
var label = new Label(text); | ||
label.AddToClassList("unity-header-drawer__label"); | ||
|
||
root.Add(label); | ||
|
||
return root; | ||
} | ||
} | ||
|
||
public partial class SyncListDrawer | ||
{ | ||
public VisualElement Create() | ||
{ | ||
if (_syncListFields.Count == 0) { return null; } | ||
|
||
var root = new VisualElement(); | ||
|
||
root.Add(NetworkBehaviourInspectorDrawer.CreateHeader("Sync Lists")); | ||
|
||
foreach (var syncListField in _syncListFields) | ||
{ | ||
root.Add(CreateSyncList(syncListField)); | ||
} | ||
|
||
return root; | ||
} | ||
|
||
private VisualElement CreateSyncList(SyncListField syncListField) | ||
{ | ||
syncListField.UpdateItems(_targetObject); | ||
|
||
var list = new ListView(syncListField.items) | ||
{ | ||
showBorder = true, | ||
showFoldoutHeader = true, | ||
headerTitle = syncListField.label, | ||
showAddRemoveFooter = false, | ||
showBoundCollectionSize = false, | ||
showAlternatingRowBackgrounds = AlternatingRowBackground.All, | ||
makeItem = MakeSyncListItem, | ||
bindItem = (element, i) => BindSyncListItem(element, i, syncListField), | ||
viewDataKey = "mirage-sync-list-" + syncListField.field.Name // Used for the expanded state | ||
}; | ||
|
||
// Because we can't listen for any lists updates, just refresh the list completely every 1 second. | ||
list.schedule.Execute(() => | ||
{ | ||
// No need to update the game is not playing. | ||
if (Application.isPlaying) | ||
{ | ||
syncListField.UpdateItems(_targetObject); | ||
list.Rebuild(); | ||
} | ||
}).Every(1000); | ||
|
||
return list; | ||
} | ||
|
||
private static VisualElement MakeSyncListItem() | ||
{ | ||
var root = new VisualElement(); | ||
root.AddToClassList("unity-base-field"); | ||
root.AddToClassList("unity-base-field__aligned"); | ||
root.AddToClassList("unity-base-field__inspector-field"); | ||
|
||
var elementLabel = new Label | ||
{ | ||
name = "element-label" | ||
}; | ||
|
||
elementLabel.AddToClassList("unity-base-field__label"); | ||
elementLabel.AddToClassList("unity-property-field__label"); | ||
|
||
var elementValue = new Label("Test") | ||
{ | ||
name = "element-value" | ||
}; | ||
|
||
root.Add(elementLabel); | ||
root.Add(elementValue); | ||
|
||
return root; | ||
} | ||
|
||
private static void BindSyncListItem(VisualElement element, int index, SyncListField field) | ||
{ | ||
var elementLabel = element.Q<Label>("element-label"); | ||
var elementValue = element.Q<Label>("element-value"); | ||
|
||
elementLabel.text = $"Element {index.ToString(CultureInfo.InvariantCulture)}"; | ||
|
||
elementValue.text = field.items[index] != null ? field.items[index].ToString() : "NULL"; | ||
} | ||
} | ||
} | ||
#endif // UNITY_2022_2_OR_NEWER |
3 changes: 3 additions & 0 deletions
3
Assets/Mirage/Editor/NetworkBehaviourInspectorUIToolkit.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#if UNITY_2022_2_OR_NEWER | ||
using UnityEditor; | ||
using UnityEditor.UIElements; | ||
using UnityEngine.UIElements; | ||
|
||
namespace Mirage | ||
{ | ||
public partial class ReadOnlyDecoratorDrawer | ||
{ | ||
public override VisualElement CreatePropertyGUI(SerializedProperty property) | ||
{ | ||
var field = new PropertyField(property); | ||
field.SetEnabled(false); | ||
return field; | ||
} | ||
} | ||
} | ||
#endif // UNITY_2022_2_OR_NEWER |
3 changes: 3 additions & 0 deletions
3
Assets/Mirage/Editor/ReadOnlyDecoratorDrawerUIToolkit.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.