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

Added an optional format string for [Dropdown] #330

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ public class DropdownAttribute : DrawerAttribute
{
public string ValuesName { get; private set; }

public DropdownAttribute(string valuesName)
public string DisplayFormat { get; set; }

public DropdownAttribute(string valuesName, string displayFormat = "")
{
ValuesName = valuesName;
DisplayFormat = displayFormat;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
object valuesObject = GetValues(property, dropdownAttribute.ValuesName);
FieldInfo dropdownField = ReflectionUtility.GetField(target, property.name);

Func<object, string> generateDisplayValue = v => v.ToString();

if (!string.IsNullOrWhiteSpace(dropdownAttribute.DisplayFormat))
{
generateDisplayValue = v => string.Format(dropdownAttribute.DisplayFormat, v);
}

if (AreValuesValid(valuesObject, dropdownField))
{
if (valuesObject is IList && dropdownField.FieldType == GetElementType(valuesObject))
Expand All @@ -49,7 +56,9 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
{
object value = valuesList[i];
values[i] = value;
displayOptions[i] = value == null ? "<null>" : value.ToString();
displayOptions[i] = value == null
? "<null>"
: generateDisplayValue(value);
}

// Selected value index
Expand Down Expand Up @@ -98,7 +107,7 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
}
else
{
displayOptions.Add(current.Key);
displayOptions.Add(generateDisplayValue(current.Key));
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Test/DropdownTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,25 @@ public class DropdownTest : MonoBehaviour
{
[Dropdown("intValues")]
public int intValue;

[Dropdown("intValues", "Number: {0}")]
public int prefixedIntValue;

[Dropdown("intValues", "{0} Megabyte(s)")]
public int suffixedIntValue;

[Dropdown("intValues", "Show {0} Widgets")]
public int surroundedIntValue;

[Dropdown("floatValues", "Value: {0}")]
public float floatValue;

[Dropdown("floatValues", "{0:0.#%}")]
public float formattedFloatValue;

#pragma warning disable 414
private int[] intValues = new int[] { 1, 2, 3 };
private float[] floatValues = new float[] { 0.1234f, 0.5648f, 1.0f };
#pragma warning restore 414

public DropdownNest1 nest1;
Expand All @@ -20,6 +36,15 @@ public class DropdownNest1
{
[Dropdown("StringValues")]
public string stringValue;

[Dropdown("StringValues", "Letter: {0}")]
public string prefixedStringValue;

[Dropdown("StringValues", "{0} Grade")]
public string suffixedStringValue;

[Dropdown("StringValues", "Hello {0} World")]
public string surroundedStringValue;

private List<string> StringValues { get { return new List<string>() { "A", "B", "C" }; } }

Expand All @@ -31,6 +56,15 @@ public class DropdownNest2
{
[Dropdown("GetVectorValues")]
public Vector3 vectorValue;

[Dropdown("GetVectorValues", "Go {0}")]
public Vector3 prefixedVectorValue;

[Dropdown("GetVectorValues", "{0} is the way!")]
public Vector3 suffixedVectorValue;

[Dropdown("GetVectorValues", "Go: {0} now!")]
public Vector3 surroundedVectorValue;

private DropdownList<Vector3> GetVectorValues()
{
Expand Down