Skip to content

ReadonlyExpandablePropertyDrawer: initial commit. #403

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace NaughtyAttributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class ReadonlyExpandableAttribute : DrawerAttribute
{
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
using System;
using UnityEngine;
using UnityEditor;

namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(ReadonlyExpandableAttribute))]
public class ReadonlyExpandablePropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
if (property.objectReferenceValue == null)
{
return GetPropertyHeight(property);
}

Type propertyType = PropertyUtility.GetPropertyType(property);
if (typeof(ScriptableObject).IsAssignableFrom(propertyType))
{
ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
if (scriptableObject == null)
{
return GetPropertyHeight(property);
}

if (!property.isExpanded)
{
return GetPropertyHeight(property);
}

using SerializedObject serializedObject = new SerializedObject(scriptableObject);
float totalHeight = EditorGUIUtility.singleLineHeight;

using var iterator = serializedObject.GetIterator();
if (iterator.NextVisible(true))
{
do
{
SerializedProperty childProperty = serializedObject.FindProperty(iterator.name);
if (childProperty.name.Equals("m_Script", StringComparison.Ordinal))
{
continue;
}

bool visible = PropertyUtility.IsVisible(childProperty);
if (!visible)
{
continue;
}

float height = GetPropertyHeight(childProperty);
totalHeight += height;
} while (iterator.NextVisible(false));
}

totalHeight += EditorGUIUtility.standardVerticalSpacing;
return totalHeight;
}

return GetPropertyHeight(property) + GetHelpBoxHeight();
}

protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);

if (property.objectReferenceValue == null)
{
EditorGUI.PropertyField(rect, property, label, false);
}
else
{
Type propertyType = PropertyUtility.GetPropertyType(property);
if (typeof(ScriptableObject).IsAssignableFrom(propertyType))
{
ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
if (scriptableObject == null)
{
EditorGUI.PropertyField(rect, property, label, false);
}
else
{
// Draw a foldout
Rect foldoutRect = new Rect
{
x = rect.x,
y = rect.y,
width = EditorGUIUtility.labelWidth,
height = EditorGUIUtility.singleLineHeight
};

property.isExpanded = EditorGUI.Foldout(
foldoutRect, property.isExpanded, label, toggleOnLabelClick: true);

// Draw the scriptable object field
Rect propertyRect = new Rect
{
x = rect.x,
y = rect.y,
width = rect.width,
height = EditorGUIUtility.singleLineHeight
};

EditorGUI.PropertyField(propertyRect, property, label, false);

// Draw the child properties
if (property.isExpanded)
{
DrawChildProperties(rect, property);
}
}
}
else
{
string message = $"{nameof(ReadonlyExpandableAttribute)} can only be used on scriptable objects";
DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
}
}

property.serializedObject.ApplyModifiedProperties();
EditorGUI.EndProperty();
}

private void DrawChildProperties(Rect rect, SerializedProperty property)
{
ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
if (scriptableObject == null)
{
return;
}

using (new EditorGUI.DisabledScope(true))
{
Rect boxRect = new Rect
{
x = 0.0f,
y = rect.y + EditorGUIUtility.singleLineHeight,
width = rect.width * 2.0f,
height = rect.height - EditorGUIUtility.singleLineHeight
};

GUI.Box(boxRect, GUIContent.none);

using (new EditorGUI.IndentLevelScope())
{
SerializedObject serializedObject = new SerializedObject(scriptableObject);
serializedObject.Update();

using (var iterator = serializedObject.GetIterator())
{
float yOffset = EditorGUIUtility.singleLineHeight;

if (iterator.NextVisible(true))
{
do
{
SerializedProperty childProperty = serializedObject.FindProperty(iterator.name);
if (childProperty.name.Equals("m_Script", StringComparison.Ordinal))
{
continue;
}

bool visible = PropertyUtility.IsVisible(childProperty);
if (!visible)
{
continue;
}

float childHeight = GetPropertyHeight(childProperty);
Rect childRect = new Rect()
{
x = rect.x,
y = rect.y + yOffset,
width = rect.width,
height = childHeight
};


NaughtyEditorGUI.PropertyField(childRect, childProperty, true);

yOffset += childHeight;
} while (iterator.NextVisible(false));
}
}

serializedObject.ApplyModifiedProperties();
}
}
}
}
}

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