Skip to content

Commit

Permalink
#213 - fixed a bug where Dropdown doesn't save first value by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrizov committed Apr 24, 2021
1 parent ce8eaf2 commit 325696f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9c928ea15ae74a44089beb2e534c1a35, type: 3}
m_Name:
m_EditorClassIdentifier:
intValue: 10
--- !u!1 &732714203
GameObject:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 753bdb918c6038142acddbd7aae6958f, type: 3}
m_Name: NaughtyScriptableObject
m_EditorClassIdentifier:
enableMyInt: 0
showMyFloat: 1
myInt: 0
myFloat: 1
mySlider: {x: 0.25, y: 0.75}
list: 010000000200000003000000
vectorValue: {x: 1, y: 0, z: 0}
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,15 @@ public static void EndBoxGroup_Layout()
EditorGUI.BeginChangeCheck();

int newIndex = EditorGUI.Popup(rect, label, selectedValueIndex, displayOptions);
object newValue = values[newIndex];

if (EditorGUI.EndChangeCheck())
if (!dropdownField.GetValue(target).Equals(newValue))
{
Undo.RecordObject(serializedObject.targetObject, "Dropdown");

// TODO: Problem with structs, because they are value type.
// The solution is to make boxing/unboxing but unfortunately I don't know the compile time type of the target object
dropdownField.SetValue(target, values[newIndex]);
dropdownField.SetValue(target, newValue);
}
}

Expand Down
12 changes: 12 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Test/_NaughtyComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ namespace NaughtyAttributes.Test
{
public class _NaughtyComponent : MonoBehaviour
{
[Dropdown("intValues")]
public int intValue;

#pragma warning disable 414
private int[] intValues = new int[] { 10, 20, 30 };
#pragma warning restore 414

[Button]
private void Log()
{
Debug.Log(intValue);
}
}

[System.Serializable]
Expand Down
32 changes: 12 additions & 20 deletions Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,23 @@ namespace NaughtyAttributes.Test
//[CreateAssetMenu(fileName = "NaughtyScriptableObject", menuName = "NaughtyAttributes/_NaughtyScriptableObject")]
public class _NaughtyScriptableObject : ScriptableObject
{
public bool enableMyInt = true;
public bool showMyFloat = true;
[Dropdown("GetVectorValues")]
public Vector3 vectorValue;

[EnableIf("enableMyInt")]
public int myInt;

[ShowIf("showMyFloat")]
public float myFloat;

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

//[ReorderableList]
public List<int> list;

[Button]
private void IncrementMyInt()
private DropdownList<Vector3> GetVectorValues()
{
myInt++;
return new DropdownList<Vector3>()
{
{ "Right", Vector3.right },
{ "Up", Vector3.up },
{ "Forward", Vector3.forward }
};
}

[Button("Decrement My Int")]
private void DecrementMyInt()
[Button]
private void Log()
{
myInt--;
Debug.Log(vectorValue);
}
}
}

0 comments on commit 325696f

Please sign in to comment.