From 7c83771b8b0253b736e52075173b8dfc7a46babc Mon Sep 17 00:00:00 2001 From: mohammad7800 Date: Wed, 27 Jul 2022 19:53:29 +0430 Subject: [PATCH 1/2] HorizontalEnum Added To Naughty Attributes Tests Added To Demo Scene --- .../Samples/DemoScene/DemoScene.unity | 92 +++++++++++--- .../MetaAttributes/HorizontalEnumAttribute.cs | 10 ++ .../HorizontalEnumAttribute.cs.meta | 11 ++ .../HorizontalEnumPropertyDrawer.cs | 115 ++++++++++++++++++ .../HorizontalEnumPropertyDrawer.cs.meta | 11 ++ .../Scripts/Test/HorizontalEnumTest.cs | 88 ++++++++++++++ .../Scripts/Test/HorizontalEnumTest.cs.meta | 11 ++ 7 files changed, 324 insertions(+), 14 deletions(-) create mode 100644 Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/HorizontalEnumAttribute.cs create mode 100644 Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/HorizontalEnumAttribute.cs.meta create mode 100644 Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs create mode 100644 Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs.meta create mode 100644 Assets/NaughtyAttributes/Scripts/Test/HorizontalEnumTest.cs create mode 100644 Assets/NaughtyAttributes/Scripts/Test/HorizontalEnumTest.cs.meta diff --git a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity index 3c134139..52c38778 100644 --- a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity +++ b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity @@ -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: @@ -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 @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -931,6 +994,7 @@ Transform: - {fileID: 1194502638} - {fileID: 1609261820} - {fileID: 1380469385} + - {fileID: 52032591} - {fileID: 732714204} - {fileID: 1784643785} - {fileID: 572382749} @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/HorizontalEnumAttribute.cs b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/HorizontalEnumAttribute.cs new file mode 100644 index 00000000..04bbb713 --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/HorizontalEnumAttribute.cs @@ -0,0 +1,10 @@ +using System; +using UnityEngine; + +namespace NaughtyAttributes +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)] + public class HorizontalEnumAttribute : PropertyAttribute + { + } +} \ No newline at end of file diff --git a/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/HorizontalEnumAttribute.cs.meta b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/HorizontalEnumAttribute.cs.meta new file mode 100644 index 00000000..78731fe4 --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/HorizontalEnumAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0e856291d6039a84c9bd5788a347a059 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs new file mode 100644 index 00000000..6270bd46 --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs @@ -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 : PropertyDrawer + { + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + EditorGUI.BeginProperty(position, label, property); + + if (property.propertyType == SerializedPropertyType.Enum) + { + var hasFlags = fieldInfo.FieldType.GetCustomAttribute() != null; + var enumValues = Enum.GetValues(fieldInfo.FieldType).Cast().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 + { + EditorGUI.HelpBox(position, $"{nameof(HorizontalEnumAttribute)} Can only be used on enum fields", MessageType.Error); + } + + 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 = EditorStyles.label; + labelStyle.alignment = TextAnchor.MiddleCenter; + labelStyle.fontStyle = FontStyle.Bold; + + EditorGUI.LabelField(pos, name, labelStyle); + + return result; + } + } +} \ No newline at end of file diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs.meta b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs.meta new file mode 100644 index 00000000..ef0e8b6f --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0b32e20e2d1a44144bad2912c135c710 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/NaughtyAttributes/Scripts/Test/HorizontalEnumTest.cs b/Assets/NaughtyAttributes/Scripts/Test/HorizontalEnumTest.cs new file mode 100644 index 00000000..ff0451f7 --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Test/HorizontalEnumTest.cs @@ -0,0 +1,88 @@ +using System; +using UnityEngine; + +namespace NaughtyAttributes.Test +{ + public class HorizontalEnumTest : MonoBehaviour + { + public enum NormalEnum { Test1, Test2, Test3 } + + #region NormalEnum + + [Space(20)] + [HorizontalEnum] + public NormalEnum choose; + + [ShowIf(nameof(choose), NormalEnum.Test1)] + public bool test1Bool; + + [ShowIf(nameof(choose), NormalEnum.Test1)] + public int test1Int; + + [ShowIf(nameof(choose), NormalEnum.Test1)] + public string test1String; + + + [ShowIf(nameof(choose), NormalEnum.Test2)] + public int test2Int; + + [ShowIf(nameof(choose), NormalEnum.Test2)] + public bool test2Bool; + + [ShowIf(nameof(choose), NormalEnum.Test2)] + public string test2String; + + + [ShowIf(nameof(choose), NormalEnum.Test3)] + public string test3String; + + [ShowIf(nameof(choose), NormalEnum.Test3)] + public bool test3Bool; + + [ShowIf(nameof(choose), NormalEnum.Test3)] + public int test3Int; + + + #endregion + + + #region FlagsEnum + + [Flags] + public enum FlagsEnum { None = 0, Test1 = 1 << 0, Test2 = 1 << 1, Test3 = 1 << 2 } + + [Space(20)] + [HorizontalEnum] + public FlagsEnum chooseFlagsEnum; + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test1)] + public bool test1BoolFlags; + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test1)] + public int test1IntFlags; + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test1)] + public string test1StringFlags; + + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test2)] + public int test2IntFlags; + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test2)] + public bool test2BoolFlags; + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test2)] + public string test2StringFlags; + + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test3)] + public string test3StringFlags; + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test3)] + public bool test3BoolFlags; + + [ShowIf(nameof(chooseFlagsEnum), FlagsEnum.Test3)] + public int test3IntFlags; + #endregion + } +} \ No newline at end of file diff --git a/Assets/NaughtyAttributes/Scripts/Test/HorizontalEnumTest.cs.meta b/Assets/NaughtyAttributes/Scripts/Test/HorizontalEnumTest.cs.meta new file mode 100644 index 00000000..b653ac1e --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Test/HorizontalEnumTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5f4ebc88c7e34d148979dbb5cf38c2fa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 235b73e3656e5fcd547242c16c25b3424f243d61 Mon Sep 17 00:00:00 2001 From: mohammad7800 Date: Wed, 27 Jul 2022 20:33:08 +0430 Subject: [PATCH 2/2] Some Bug Fixes --- .../PropertyDrawers/HorizontalEnumPropertyDrawer.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs index 6270bd46..61d2a366 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/HorizontalEnumPropertyDrawer.cs @@ -7,9 +7,9 @@ namespace NaughtyAttributes.Editor { [CustomPropertyDrawer(typeof(HorizontalEnumAttribute))] - public class HorizontalEnumPropertyDrawer : PropertyDrawer + public class HorizontalEnumPropertyDrawer : PropertyDrawerBase { - public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + protected override void OnGUI_Internal(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); @@ -88,7 +88,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten } else { - EditorGUI.HelpBox(position, $"{nameof(HorizontalEnumAttribute)} Can only be used on enum fields", MessageType.Error); + DrawDefaultPropertyAndHelpBox(position, property, $"{nameof(HorizontalEnumAttribute)} Can only be used on enum fields", MessageType.Warning); } EditorGUI.EndProperty(); @@ -103,7 +103,7 @@ private bool Button(Rect pos, bool value, string name) { var result = EditorGUI.Toggle(pos, value, EditorStyles.miniButton); - var labelStyle = EditorStyles.label; + var labelStyle = GUI.skin.label; labelStyle.alignment = TextAnchor.MiddleCenter; labelStyle.fontStyle = FontStyle.Bold;