Skip to content
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

Feature/anim controller reader attribute #266

Open
wants to merge 15 commits 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
403 changes: 374 additions & 29 deletions Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ MonoBehaviour:
- {fileID: 11400000, guid: 149474eb879a6a641b560ca17d48712f, type: 2}
- {fileID: 11400000, guid: ca97c330d5c96794aa4df848d63f836b, type: 2}
- {fileID: 0}
animatorController0: {fileID: 9100000, guid: 63ee86efd213bf34285c95f33e79dc6c, type: 2}
hashController0: -392453409
nameController0: Trigger1
193 changes: 141 additions & 52 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using UnityEditor;
using UnityEngine;

Expand All @@ -10,35 +11,89 @@ namespace NaughtyAttributes.Editor
[CustomEditor(typeof(UnityEngine.Object), true)]
public class NaughtyInspector : UnityEditor.Editor
{
private List<SerializedProperty> _serializedProperties = new List<SerializedProperty>();
private IEnumerable<FieldInfo> _nonSerializedFields;
private IEnumerable<PropertyInfo> _nativeProperties;
private IEnumerable<MethodInfo> _methods;
private List<NaughtyProperty> _serializedProperties = new List<NaughtyProperty>();
private List<FieldInfo> _nonSerializedFields;
private List<PropertyInfo> _nativeProperties;
private List<MethodInfo> _methods;

private List<NaughtyProperty> _nonGroupedSerializedProperty;

private SerializedProperty m_ScriptProperty;

private List<IGrouping<string, NaughtyProperty>> _groupedSerialzedProperty;

private List<IGrouping<string, NaughtyProperty>> _foldoutGroupedSerializedProperty;

private Dictionary<string, SavedBool> _foldouts = new Dictionary<string, SavedBool>();

private bool _anyNaughtyAttribute;
private bool _useCachedMetaAttributes;
private bool _changeDetected;

protected virtual void OnEnable()
{
_nonSerializedFields = ReflectionUtility.GetAllFields(
target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0);

_nativeProperties = ReflectionUtility.GetAllProperties(
target, p => p.GetCustomAttributes(typeof(ShowNativePropertyAttribute), true).Length > 0);

_methods = ReflectionUtility.GetAllMethods(
target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0);
/*
* TODO:
* OnEnable is called for all monos and scriptable objects,
* which eats some one time perf after compilation and also takes some memory (although not noticeable)
* any other way to trigger this like via a custom editor/ window with on focus??
*
* Selection.selectionChanged += ????
* Base Mono and SO scripts that handle this??
*/

this.Prepare();
}

protected virtual void OnDisable()
{
//cleanup memory
ReorderableListPropertyDrawer.Instance.ClearCache();

_nonSerializedFields.Clear();
_nativeProperties.Clear();
_methods.Clear();
_foldoutGroupedSerializedProperty.Clear();
_groupedSerialzedProperty.Clear();
_nonGroupedSerializedProperty.Clear();
_serializedProperties.Clear();

m_ScriptProperty = default;
}

public override void OnInspectorGUI()
public virtual void Prepare()
{
_nonSerializedFields = ReflectionUtility.GetAllFields(
target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0).ToList();

_nativeProperties = ReflectionUtility.GetAllProperties(
target, p => p.GetCustomAttributes(typeof(ShowNativePropertyAttribute), true).Length > 0).ToList();

_methods = ReflectionUtility.GetAllMethods(
target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0).ToList();

GetSerializedProperties(ref _serializedProperties);

_anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute<INaughtyAttribute>(p.serializedProperty) != null);

_nonGroupedSerializedProperty = GetNonGroupedProperties(_serializedProperties).ToList();

//.First(...) doesnt work for some reason because the m_Script field isnt loaded yet I assume
NaughtyProperty[] mScripts = _serializedProperties.Where(p => p.serializedProperty.name.Equals("m_Script")).ToArray();
m_ScriptProperty = mScripts.Length > 0 ? mScripts[0].serializedProperty : null;

_groupedSerialzedProperty = GetGroupedProperties(_serializedProperties).ToList();

bool anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute<INaughtyAttribute>(p) != null);
if (!anyNaughtyAttribute)
_foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties).ToList();

_useCachedMetaAttributes = false;
}

public override void OnInspectorGUI()
{
_changeDetected = false;

if (!_anyNaughtyAttribute)
{
DrawDefaultInspector();
}
Expand All @@ -50,90 +105,124 @@ public override void OnInspectorGUI()
DrawNonSerializedFields();
DrawNativeProperties();
DrawButtons();
}

protected void GetSerializedProperties(ref List<SerializedProperty> outSerializedProperties)
_useCachedMetaAttributes = !_changeDetected;
}

protected virtual void GetSerializedProperties(ref List<NaughtyProperty> outSerializedProperties)
{
outSerializedProperties.Clear();
outSerializedProperties.TrimExcess();

using (var iterator = serializedObject.GetIterator())
{
if (iterator.NextVisible(true))
{
do
{
outSerializedProperties.Add(serializedObject.FindProperty(iterator.name));
outSerializedProperties.Add(
PropertyUtility.CreateNaughtyProperty(
serializedObject.FindProperty(iterator.name)));
}
while (iterator.NextVisible(false));
}
}
}

protected void DrawSerializedProperties()
protected virtual void DrawSerializedProperties()
{
serializedObject.Update();

// Draw non-grouped serialized properties
foreach (var property in GetNonGroupedProperties(_serializedProperties))
if (m_ScriptProperty != null)
{
if (property.name.Equals("m_Script", System.StringComparison.Ordinal))
using (new EditorGUI.DisabledScope(disabled: true))
{
using (new EditorGUI.DisabledScope(disabled: true))
{
EditorGUILayout.PropertyField(property);
}
EditorGUILayout.PropertyField(m_ScriptProperty);
}
else
}

// Draw non-grouped serialized properties
foreach (var naughtyProperty in _nonGroupedSerializedProperty)
{
if (!_useCachedMetaAttributes)
{
NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true);
naughtyProperty.cachedIsVisible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute,
naughtyProperty.serializedProperty);

naughtyProperty.cachedIsEnabled = PropertyUtility.IsEnabled(naughtyProperty.readOnlyAttribute, naughtyProperty.enableIfAttribute,
naughtyProperty.serializedProperty);
}

_changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true);
}

// Draw grouped serialized properties
foreach (var group in GetGroupedProperties(_serializedProperties))
foreach (var group in _groupedSerialzedProperty)
{
IEnumerable<SerializedProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p));
IEnumerable<NaughtyProperty> visibleProperties =
_useCachedMetaAttributes
? group.Where(p => p.cachedIsVisible)
: group.Where(p =>
{
p.cachedIsEnabled = PropertyUtility.IsEnabled(p.readOnlyAttribute, p.enableIfAttribute,
p.serializedProperty);

return p.cachedIsVisible =
PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty);
});

if (!visibleProperties.Any())
{
continue;
}

NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key);
foreach (var property in visibleProperties)
foreach (var naughtyProperty in visibleProperties)
{
NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true);
_changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true);
}

NaughtyEditorGUI.EndBoxGroup_Layout();
}

// Draw foldout serialized properties
foreach (var group in GetFoldoutProperties(_serializedProperties))
foreach (var group in _foldoutGroupedSerializedProperty)
{
IEnumerable<SerializedProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p));
IEnumerable<NaughtyProperty> visibleProperties =
_useCachedMetaAttributes
? group.Where(p => p.cachedIsVisible)
: group.Where(p =>
{
p.cachedIsEnabled = PropertyUtility.IsEnabled(p.readOnlyAttribute, p.enableIfAttribute,
p.serializedProperty);

return p.cachedIsVisible =
PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty);
});

if (!visibleProperties.Any())
{
continue;
}

if (!_foldouts.ContainsKey(group.Key))
{
_foldouts[group.Key] = new SavedBool($"{target.GetInstanceID()}.{group.Key}", false);
}

_foldouts[group.Key].Value = EditorGUILayout.Foldout(_foldouts[group.Key].Value, group.Key, true);
if (_foldouts[group.Key].Value)
{
foreach (var property in visibleProperties)
foreach (var naughtyProperty in visibleProperties)
{
NaughtyEditorGUI.PropertyField_Layout(property, true);
_changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, true);
}
}
}

serializedObject.ApplyModifiedProperties();
}

protected void DrawNonSerializedFields(bool drawHeader = false)
protected virtual void DrawNonSerializedFields(bool drawHeader = false)
{
if (_nonSerializedFields.Any())
{
Expand All @@ -152,7 +241,7 @@ protected void DrawNonSerializedFields(bool drawHeader = false)
}
}

protected void DrawNativeProperties(bool drawHeader = false)
protected virtual void DrawNativeProperties(bool drawHeader = false)
{
if (_nativeProperties.Any())
{
Expand All @@ -171,7 +260,7 @@ protected void DrawNativeProperties(bool drawHeader = false)
}
}

protected void DrawButtons(bool drawHeader = false)
protected virtual void DrawButtons(bool drawHeader = false)
{
if (_methods.Any())
{
Expand All @@ -190,23 +279,23 @@ protected void DrawButtons(bool drawHeader = false)
}
}

private static IEnumerable<SerializedProperty> GetNonGroupedProperties(IEnumerable<SerializedProperty> properties)
private static IEnumerable<NaughtyProperty> GetNonGroupedProperties(IEnumerable<NaughtyProperty> properties)
{
return properties.Where(p => PropertyUtility.GetAttribute<IGroupAttribute>(p) == null);
return properties.Where(p => PropertyUtility.GetAttribute<IGroupAttribute>(p.serializedProperty) == null && !p.serializedProperty.name.Equals("m_Script"));
}

private static IEnumerable<IGrouping<string, SerializedProperty>> GetGroupedProperties(IEnumerable<SerializedProperty> properties)
private static IEnumerable<IGrouping<string, NaughtyProperty>> GetGroupedProperties(IEnumerable<NaughtyProperty> properties)
{
return properties
.Where(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p) != null)
.GroupBy(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p).Name);
.Where(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p.serializedProperty) != null)
.GroupBy(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p.serializedProperty).Name);
}

private static IEnumerable<IGrouping<string, SerializedProperty>> GetFoldoutProperties(IEnumerable<SerializedProperty> properties)
private static IEnumerable<IGrouping<string, NaughtyProperty>> GetFoldoutProperties(IEnumerable<NaughtyProperty> properties)
{
return properties
.Where(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p) != null)
.GroupBy(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p).Name);
.Where(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p.serializedProperty) != null)
.GroupBy(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p.serializedProperty).Name);
}

private static GUIStyle GetHeaderGUIStyle()
Expand Down
21 changes: 21 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using UnityEditor;

namespace NaughtyAttributes.Editor
{
public class NaughtyProperty
{
public SerializedProperty serializedProperty;

public LabelAttribute labelAttribute;

public SpecialCaseDrawerAttribute specialCaseDrawerAttribute;

public ShowIfAttributeBase showIfAttribute;
public EnableIfAttributeBase enableIfAttribute;
public ReadOnlyAttribute readOnlyAttribute;
public ValidatorAttribute[] validatorAttributes;

public bool cachedIsVisible = true;
public bool cachedIsEnabled = true;
}
}
11 changes: 11 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs.meta

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

Loading