Skip to content

Commit

Permalink
dbrizov#133 waaay faster now
Browse files Browse the repository at this point in the history
  • Loading branch information
niggo1243 committed Sep 10, 2021
1 parent 8eb3aa1 commit 1cc0aa6
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
animator0: {fileID: 1178133862}
hash0: -392453409
hash0: -1617128887
name0: Float1
animatorParamNest1:
animator1: {fileID: 1178133862}
Expand All @@ -577,7 +577,7 @@ MonoBehaviour:
str1:
trans0: {fileID: 0}
trans1: {fileID: 0}
myInt: 13
myInt: 24
curve:
serializedVersion: 2
m_Curve:
Expand Down
33 changes: 18 additions & 15 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace NaughtyAttributes.Editor
public class NaughtyInspector : UnityEditor.Editor
{
private List<NaughtyProperty> _serializedProperties = new List<NaughtyProperty>();
private IEnumerable<FieldInfo> _nonSerializedFields;
private IEnumerable<PropertyInfo> _nativeProperties;
private IEnumerable<MethodInfo> _methods;
private List<FieldInfo> _nonSerializedFields;
private List<PropertyInfo> _nativeProperties;
private List<MethodInfo> _methods;

private IEnumerable<NaughtyProperty> _nonGroupedSerializedProperty;
private List<NaughtyProperty> _nonGroupedSerializedProperty;

private SerializedProperty m_ScriptProperty;

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

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

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

Expand Down Expand Up @@ -50,9 +50,12 @@ protected virtual void OnDisable()
//cleanup memory
ReorderableListPropertyDrawer.Instance.ClearCache();

_foldoutGroupedSerializedProperty = Enumerable.Empty<IGrouping<string, NaughtyProperty>>();
_groupedSerialzedProperty = Enumerable.Empty<IGrouping<string, NaughtyProperty>>();
_nonGroupedSerializedProperty = Enumerable.Empty<NaughtyProperty>();
_nonSerializedFields.Clear();
_nativeProperties.Clear();
_methods.Clear();
_foldoutGroupedSerializedProperty.Clear();
_groupedSerialzedProperty.Clear();
_nonGroupedSerializedProperty.Clear();
_serializedProperties.Clear();

m_ScriptProperty = default;
Expand All @@ -61,27 +64,27 @@ protected virtual void OnDisable()
public virtual void Prepare()
{
_nonSerializedFields = ReflectionUtility.GetAllFields(
target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0);
target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0).ToList();

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

_methods = ReflectionUtility.GetAllMethods(
target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0);
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);
_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);
_groupedSerialzedProperty = GetGroupedProperties(_serializedProperties).ToList();

_foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties);
_foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties).ToList();

_useCachedMetaAttributes = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ namespace NaughtyAttributes.Editor
[CustomPropertyDrawer(typeof(AnimatorParamAttribute))]
public class AnimatorParamPropertyDrawer : PropertyDrawerBase
{
private AnimatorParamAttribute _cachedAnimatorParamAttribute;

private const string InvalidAnimatorControllerWarningMessage = "Target animator controller is null";
private const string InvalidTypeWarningMessage = "{0} must be an int or a string";

protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute<AnimatorParamAttribute>(property);
if (_cachedAnimatorParamAttribute == null)
_cachedAnimatorParamAttribute = PropertyUtility.GetAttribute<AnimatorParamAttribute>(property);

AnimatorParamAttribute animatorParamAttribute = _cachedAnimatorParamAttribute;
bool validAnimatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName) != null;
bool validPropertyType = property.propertyType == SerializedPropertyType.Integer || property.propertyType == SerializedPropertyType.String;

Expand All @@ -27,7 +32,10 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
{
EditorGUI.BeginProperty(rect, label, property);

AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute<AnimatorParamAttribute>(property);
if (_cachedAnimatorParamAttribute == null)
_cachedAnimatorParamAttribute = PropertyUtility.GetAttribute<AnimatorParamAttribute>(property);

AnimatorParamAttribute animatorParamAttribute = _cachedAnimatorParamAttribute;

AnimatorController animatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName);
if (animatorController == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace NaughtyAttributes.Editor
[CustomPropertyDrawer(typeof(CurveRangeAttribute))]
public class CurveRangePropertyDrawer : PropertyDrawerBase
{
private CurveRangeAttribute _cachedCurveRangeAttribute;

protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
float propertyHeight = property.propertyType == SerializedPropertyType.AnimationCurve
Expand All @@ -27,7 +29,10 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
return;
}

var attribute = PropertyUtility.GetAttribute<CurveRangeAttribute>(property);
if (_cachedCurveRangeAttribute == null)
_cachedCurveRangeAttribute = PropertyUtility.GetAttribute<CurveRangeAttribute>(property);

var attribute = _cachedCurveRangeAttribute;

EditorGUI.CurveField(
rect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ namespace NaughtyAttributes.Editor
[CustomPropertyDrawer(typeof(ProgressBarAttribute))]
public class ProgressBarPropertyDrawer : PropertyDrawerBase
{
private ProgressBarAttribute _cachedProgressBarAttribute;

protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute<ProgressBarAttribute>(property);
if (_cachedProgressBarAttribute == null)
_cachedProgressBarAttribute = PropertyUtility.GetAttribute<ProgressBarAttribute>(property);

ProgressBarAttribute progressBarAttribute = _cachedProgressBarAttribute;
var maxValue = GetMaxValue(property, progressBarAttribute);

return IsNumber(property) && IsNumber(maxValue)
Expand All @@ -28,7 +33,10 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
return;
}

ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute<ProgressBarAttribute>(property);
if (_cachedProgressBarAttribute == null)
_cachedProgressBarAttribute = PropertyUtility.GetAttribute<ProgressBarAttribute>(property);

ProgressBarAttribute progressBarAttribute = _cachedProgressBarAttribute;
var value = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value);
var maxValue = GetMaxValue(property, progressBarAttribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace NaughtyAttributes.Editor
{
public abstract class PropertyDrawerBase : PropertyDrawer
{
private SpecialCaseDrawerAttribute _cachedSpecialCaseDrawerAttribute;

public sealed override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
// Check if visible
Expand Down Expand Up @@ -57,7 +59,10 @@ protected virtual float GetPropertyHeight_Internal(SerializedProperty property,

protected float GetPropertyHeight(SerializedProperty property)
{
SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute<SpecialCaseDrawerAttribute>(property);
if (_cachedSpecialCaseDrawerAttribute == null)
_cachedSpecialCaseDrawerAttribute = PropertyUtility.GetAttribute<SpecialCaseDrawerAttribute>(property);

SpecialCaseDrawerAttribute specialCaseAttribute = _cachedSpecialCaseDrawerAttribute;
if (specialCaseAttribute != null)
{
return specialCaseAttribute.GetDrawer().GetPropertyHeight(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace NaughtyAttributes.Editor
[CustomPropertyDrawer(typeof(ShowAssetPreviewAttribute))]
public class ShowAssetPreviewPropertyDrawer : PropertyDrawerBase
{
private ShowAssetPreviewAttribute _cachedShowAssetPreviewAttribute;

protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.ObjectReference)
Expand Down Expand Up @@ -93,7 +95,11 @@ private Vector2 GetAssetPreviewSize(SerializedProperty property)
int targetWidth = ShowAssetPreviewAttribute.DefaultWidth;
int targetHeight = ShowAssetPreviewAttribute.DefaultHeight;

ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute<ShowAssetPreviewAttribute>(property);
if (_cachedShowAssetPreviewAttribute == null)
_cachedShowAssetPreviewAttribute =
PropertyUtility.GetAttribute<ShowAssetPreviewAttribute>(property);

ShowAssetPreviewAttribute showAssetPreviewAttribute = _cachedShowAssetPreviewAttribute;
if (showAssetPreviewAttribute != null)
{
targetWidth = showAssetPreviewAttribute.Width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ namespace NaughtyAttributes.Editor
{
public class MaxValuePropertyValidator : PropertyValidatorBase
{
private MaxValueAttribute _cachedMaxValueAttribute;

public override void ValidateProperty(SerializedProperty property)
{
MaxValueAttribute maxValueAttribute = PropertyUtility.GetAttribute<MaxValueAttribute>(property);
if (_cachedMaxValueAttribute == null)
_cachedMaxValueAttribute = PropertyUtility.GetAttribute<MaxValueAttribute>(property);

MaxValueAttribute maxValueAttribute = _cachedMaxValueAttribute;

if (property.propertyType == SerializedPropertyType.Integer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ namespace NaughtyAttributes.Editor
{
public class MinValuePropertyValidator : PropertyValidatorBase
{
private MinValueAttribute _cachedMinValueAttribute;

public override void ValidateProperty(SerializedProperty property)
{
MinValueAttribute minValueAttribute = PropertyUtility.GetAttribute<MinValueAttribute>(property);
if (_cachedMinValueAttribute == null)
_cachedMinValueAttribute = PropertyUtility.GetAttribute<MinValueAttribute>(property);

MinValueAttribute minValueAttribute = _cachedMinValueAttribute;

if (property.propertyType == SerializedPropertyType.Integer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ namespace NaughtyAttributes.Editor
{
public class RequiredPropertyValidator : PropertyValidatorBase
{
private RequiredAttribute _cachedRequiredAttribute;

public override void ValidateProperty(SerializedProperty property)
{
RequiredAttribute requiredAttribute = PropertyUtility.GetAttribute<RequiredAttribute>(property);
if (_cachedRequiredAttribute == null)
_cachedRequiredAttribute = PropertyUtility.GetAttribute<RequiredAttribute>(property);

RequiredAttribute requiredAttribute = _cachedRequiredAttribute;

if (property.propertyType == SerializedPropertyType.ObjectReference)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ namespace NaughtyAttributes.Editor
{
public class ValidateInputPropertyValidator : PropertyValidatorBase
{
private ValidateInputAttribute _cachedValidateInputAttribute;

public override void ValidateProperty(SerializedProperty property)
{
ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute<ValidateInputAttribute>(property);
if (_cachedValidateInputAttribute == null)
_cachedValidateInputAttribute = PropertyUtility.GetAttribute<ValidateInputAttribute>(property);

ValidateInputAttribute validateInputAttribute = _cachedValidateInputAttribute;
object target = PropertyUtility.GetTargetObjectWithProperty(property);

MethodInfo validationCallback = ReflectionUtility.GetMethod(target, validateInputAttribute.CallbackName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static class PropertyUtility
{
public static T GetAttribute<T>(SerializedProperty property) where T : class
{
//Debug.Log("waste of time");

T[] attributes = GetAttributes<T>(property);
return (attributes.Length > 0) ? attributes[0] : null;
}
Expand Down

0 comments on commit 1cc0aa6

Please sign in to comment.