Skip to content

Commit

Permalink
#232 - fixed a bug where expandable is not working for a list of scri…
Browse files Browse the repository at this point in the history
…ptable objects
  • Loading branch information
dbrizov committed May 2, 2021
1 parent 383c802 commit e4edd04
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 753bdb918c6038142acddbd7aae6958f, type: 3}
m_Name: NaughtyScriptableObject
m_EditorClassIdentifier:
integer: 1
minMaxSlider: {x: 0.25, y: 0.75}
vectorValue: {x: 0, y: 1, z: 0}
list:
- {fileID: 11400000, guid: 149474eb879a6a641b560ca17d48712f, type: 2}
- {fileID: 11400000, guid: ca97c330d5c96794aa4df848d63f836b, type: 2}
- {fileID: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 19472ac11eae27a4b804f354ca7d9c00, type: 3}
m_Name: TestScriptableObject 0
m_EditorClassIdentifier:
integer: 0
minMaxSlider: {x: 0, y: 0.5}
vectorValue: {x: 1, y: 0, z: 0}

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,16 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 19472ac11eae27a4b804f354ca7d9c00, type: 3}
m_Name: TestScriptableObject 1
m_EditorClassIdentifier:
minMaxSlider: {x: 0.5, y: 1}
vectorValue: {x: 0, y: 1, z: 0}

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
Expand Up @@ -8,6 +8,11 @@ public class ExpandablePropertyDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
{
if (property.objectReferenceValue == null)
{
return GetPropertyHeight(property);
}

System.Type propertyType = PropertyUtility.GetPropertyType(property);
if (typeof(ScriptableObject).IsAssignableFrom(propertyType))
{
Expand Down Expand Up @@ -67,51 +72,58 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
{
EditorGUI.BeginProperty(rect, label, property);

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

property.serializedObject.ApplyModifiedProperties();
property.serializedObject.ApplyModifiedProperties();

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

EditorGUI.EndProperty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ internal static bool GetConditionsFlag(List<bool> conditionValues, EConditionOpe

public static Type GetPropertyType(SerializedProperty property)
{
Type parentType = GetTargetObjectWithProperty(property).GetType();
FieldInfo fieldInfo = parentType.GetField(property.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
object obj = GetTargetObjectOfProperty(property);
Type objType = obj.GetType();

return fieldInfo.FieldType;
return objType;
}

/// <summary>
Expand Down
25 changes: 2 additions & 23 deletions Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,7 @@ namespace NaughtyAttributes.Test
//[CreateAssetMenu(fileName = "NaughtyScriptableObject", menuName = "NaughtyAttributes/_NaughtyScriptableObject")]
public class _NaughtyScriptableObject : ScriptableObject
{
public int integer;

[MinMaxSlider(0.0f, 1.0f)]
public Vector2 minMaxSlider;

[Dropdown("GetVectorValues")]
public Vector3 vectorValue;

private DropdownList<Vector3> GetVectorValues()
{
return new DropdownList<Vector3>()
{
{ "Right", Vector3.right },
{ "Up", Vector3.up },
{ "Forward", Vector3.forward }
};
}

[Button]
private void Log()
{
Debug.Log(vectorValue);
}
[Expandable]
public List<_TestScriptableObject> list;
}
}
24 changes: 24 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Test/_TestScriptableObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UnityEngine;

namespace NaughtyAttributes.Test
{
//[CreateAssetMenu(fileName = "TestScriptableObject", menuName = "NaughtyAttributes/TestScriptableObject")]
public class _TestScriptableObject : ScriptableObject
{
[MinMaxSlider(0.0f, 1.0f)]
public Vector2 minMaxSlider;

[Dropdown("GetVectorValues")]
public Vector3 vectorValue;

private DropdownList<Vector3> GetVectorValues()
{
return new DropdownList<Vector3>()
{
{ "Right", Vector3.right },
{ "Up", Vector3.up },
{ "Forward", Vector3.forward }
};
}
}
}

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

0 comments on commit e4edd04

Please sign in to comment.