Skip to content

feat: Distributed Authority Sample - refactoring inspectors to allow customizable child inspectors #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Unity.Multiplayer.Samples.SocialHub.Physics;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace Unity.Multiplayer.Samples.SocialHub.Editor
{
[CanEditMultipleObjects]
[CustomEditor(typeof(BaseObjectMotionHandler), editorForChildClasses: true)]
class BaseObjectMotionHandlerEditor : UnityEditor.Editor
{
HashSet<string> m_DrawnFields = new HashSet<string>();

public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();

Type currentType = target.GetType();
var types = new List<Type>();

// Gather all types up to MonoBehaviour
while (currentType != typeof(MonoBehaviour) && currentType != null)
{
types.Add(currentType);
currentType = currentType.BaseType;
}

// Iterate types in reverse (base class first)
types.Reverse();

foreach (var type in types)
{
Foldout foldout = CreateFoldoutForType(serializedObject, type);
root.Add(foldout);
}

root.Bind(serializedObject);
return root;
}

Foldout CreateFoldoutForType(SerializedObject obj, Type type)
{
var foldout = new Foldout { text = type.Name };

// Check for custom drawing method
MethodInfo customDrawMethod = type.GetMethod("CreateInspectorGUI", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
if (customDrawMethod != null && type != typeof(BaseObjectMotionHandler)) // ie. only include overriden methods
{
VisualElement customElement = (VisualElement)customDrawMethod.Invoke(obj.targetObject, new object[] { obj });
foldout.Add(customElement);
return foldout;
}

// Fallback to default drawing if no custom drawing method is found
FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
foreach (var fieldInfo in fieldInfos)
{
if (m_DrawnFields.Contains(fieldInfo.Name))
{
continue;
}

SerializedProperty property = obj.FindProperty(fieldInfo.Name);
if (property != null)
{
var propertyField = new PropertyField(property);
foldout.Add(propertyField);
m_DrawnFields.Add(fieldInfo.Name);
}
}

return foldout;
}
}
}

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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
using Unity.Netcode.Components;
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
#endif

namespace Unity.Multiplayer.Samples.SocialHub.Physics
{
Expand Down Expand Up @@ -367,5 +372,29 @@ protected void LogMessage(string msg, bool forceMessage = false, float messageTi
}

#endregion

#if UNITY_EDITOR
// override this in child classes to implement custom drawing of a child class' inspector
protected virtual VisualElement CreateInspectorGUI(SerializedObject serializedObj)
{
// Create a new VisualElement to be the root of our inspector UI.
var root = new VisualElement();

// Iterate through the properties of the serialized object.
SerializedProperty property = serializedObj.GetIterator();
property.NextVisible(true); // Skip the script property.

while (property.NextVisible(false))
{
// Create a PropertyField for each visible property.
var propertyField = new PropertyField(property);
propertyField.BindProperty(property);
root.Add(propertyField);
}

// Return the finished UI.
return root;
}
#endif
}
}