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

Horizontal Enum Added To Naughty Attributes #331

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
92 changes: 78 additions & 14 deletions Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.44657874, g: 0.49641258, b: 0.5748172, a: 1}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -124,6 +124,69 @@ NavMeshSettings:
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &52032590
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 52032591}
- component: {fileID: 52032592}
m_Layer: 0
m_Name: HorizontalEnum
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &52032591
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 52032590}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 21
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &52032592
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 52032590}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f4ebc88c7e34d148979dbb5cf38c2fa, type: 3}
m_Name:
m_EditorClassIdentifier:
choose: 1
test1Bool: 0
test1Int: 0
test1String:
test2Int: 0
test2Bool: 0
test2String:
test3String:
test3Bool: 0
test3Int: 0
chooseFlagsEnum: 5
test1BoolFlags: 0
test1IntFlags: 0
test1StringFlags:
test2IntFlags: 0
test2BoolFlags: 0
test2StringFlags:
test3StringFlags:
test3BoolFlags: 0
test3IntFlags: 0
--- !u!1 &106181862
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -184,7 +247,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 24
m_RootOrder: 25
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &114650325
MonoBehaviour:
Expand Down Expand Up @@ -345,7 +408,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 28
m_RootOrder: 29
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &237121642
MonoBehaviour:
Expand Down Expand Up @@ -393,7 +456,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 31
m_RootOrder: 32
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &369789278
MonoBehaviour:
Expand Down Expand Up @@ -441,7 +504,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 23
m_RootOrder: 24
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &572382751
MonoBehaviour:
Expand Down Expand Up @@ -584,7 +647,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 21
m_RootOrder: 22
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &779670086
GameObject:
Expand Down Expand Up @@ -931,6 +994,7 @@ Transform:
- {fileID: 1194502638}
- {fileID: 1609261820}
- {fileID: 1380469385}
- {fileID: 52032591}
- {fileID: 732714204}
- {fileID: 1784643785}
- {fileID: 572382749}
Expand Down Expand Up @@ -1164,7 +1228,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 26
m_RootOrder: 27
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1293624597
MonoBehaviour:
Expand Down Expand Up @@ -1353,7 +1417,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 32
m_RootOrder: 33
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1463483879
MonoBehaviour:
Expand Down Expand Up @@ -1406,7 +1470,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 25
m_RootOrder: 26
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1524906392
MonoBehaviour:
Expand Down Expand Up @@ -1758,7 +1822,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 29
m_RootOrder: 30
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1706612701
GameObject:
Expand Down Expand Up @@ -1789,7 +1853,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 30
m_RootOrder: 31
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1706612703
MonoBehaviour:
Expand Down Expand Up @@ -1925,7 +1989,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 22
m_RootOrder: 23
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1784643786
MonoBehaviour:
Expand Down Expand Up @@ -1978,7 +2042,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1148579784}
m_RootOrder: 27
m_RootOrder: 28
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1871127831
MonoBehaviour:
Expand Down Expand Up @@ -2083,7 +2147,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7b7f6b84ce0d7674d8a386fde729279c, type: 3}
m_Name:
m_EditorClassIdentifier:
flags0: 0
flags0: 30
nest1:
flags1: 6
nest2:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using UnityEngine;

namespace NaughtyAttributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public class HorizontalEnumAttribute : PropertyAttribute
{
}
}

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,115 @@
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace NaughtyAttributes.Editor
{
[CustomPropertyDrawer(typeof(HorizontalEnumAttribute))]
public class HorizontalEnumPropertyDrawer : PropertyDrawerBase
{
protected override void OnGUI_Internal(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);

if (property.propertyType == SerializedPropertyType.Enum)
{
var hasFlags = fieldInfo.FieldType.GetCustomAttribute<FlagsAttribute>() != null;
var enumValues = Enum.GetValues(fieldInfo.FieldType).Cast<int>().ToArray();
var enumNames = property.enumDisplayNames;
var numberOfButtons = property.enumDisplayNames.Length;

if (hasFlags) numberOfButtons++;

var buttonsWidth = position.width / numberOfButtons;
var nowValue = property.intValue;

GUILayout.BeginHorizontal();

for (int i = 0; i < numberOfButtons; i++)
{
var pos = new Rect(
position.x + i * buttonsWidth,
position.y,
buttonsWidth,
position.height
);


bool isSelected, wasSelected;

if (hasFlags && i == 0)
{
var allValue = enumValues.Aggregate(OrOpNumbers);
wasSelected = allValue == nowValue;
isSelected = Button(pos, wasSelected, "All");
if (isSelected != wasSelected)
{
property.intValue = isSelected ? allValue : 0;
}

continue;
}

var fixedIndex = hasFlags ? i - 1 : i;
var displayName = enumNames[fixedIndex];
var buttonValue = enumValues[fixedIndex];

if (hasFlags && buttonValue != 0)
wasSelected = (nowValue & buttonValue) == buttonValue;
else
wasSelected = nowValue == buttonValue;

isSelected = Button(pos, wasSelected, displayName);
if (wasSelected != isSelected)
{
if (hasFlags)
{
if (isSelected && buttonValue != 0) property.intValue |= buttonValue;
else if (isSelected) property.intValue = 0;
else
{
var tmpValues = enumValues.ToList();
tmpValues.RemoveAt(fixedIndex);

property.intValue = tmpValues.Aggregate(OrOpNumbers);
}
}
else if (isSelected)
{
property.intValue = buttonValue;
}
}
}

GUILayout.EndHorizontal();

}
else
{
DrawDefaultPropertyAndHelpBox(position, property, $"{nameof(HorizontalEnumAttribute)} Can only be used on enum fields", MessageType.Warning);
}

EditorGUI.EndProperty();
}

private int OrOpNumbers(int first, int second)
{
return first | second;
}

private bool Button(Rect pos, bool value, string name)
{
var result = EditorGUI.Toggle(pos, value, EditorStyles.miniButton);

var labelStyle = GUI.skin.label;
labelStyle.alignment = TextAnchor.MiddleCenter;
labelStyle.fontStyle = FontStyle.Bold;

EditorGUI.LabelField(pos, name, labelStyle);

return result;
}
}
}

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

Loading