From 131a48ff91fa2e1f6684a829c28591fdca7d39f2 Mon Sep 17 00:00:00 2001 From: dbrizov Date: Sat, 12 Feb 2022 16:37:15 +0200 Subject: [PATCH] Refactoring --- Samples/DemoScene/DemoScene.unity | 18 +-- .../DrawerAttributes/SortingLayerAttribute.cs | 8 +- .../SortingLayerPropertyDrawer.cs | 142 +++++++++--------- Scripts/Test/SortingLayerTest.cs | 82 +++++----- 4 files changed, 125 insertions(+), 125 deletions(-) diff --git a/Samples/DemoScene/DemoScene.unity b/Samples/DemoScene/DemoScene.unity index 7a33aec6..0612a561 100644 --- a/Samples/DemoScene/DemoScene.unity +++ b/Samples/DemoScene/DemoScene.unity @@ -929,8 +929,8 @@ Transform: - {fileID: 933563385} - {fileID: 1989156459} - {fileID: 1194502638} - - {fileID: 1380469385} - {fileID: 1609261820} + - {fileID: 1380469385} - {fileID: 732714204} - {fileID: 1784643785} - {fileID: 572382749} @@ -1212,7 +1212,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 1148579784} - m_RootOrder: 19 + m_RootOrder: 20 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &1380469386 MonoBehaviour: @@ -1659,7 +1659,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 1148579784} - m_RootOrder: 20 + m_RootOrder: 19 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &1609261821 MonoBehaviour: @@ -1673,14 +1673,14 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 8ed73e666d447964d93c4840f05423dc, type: 3} m_Name: m_EditorClassIdentifier: - layerNumber0: 0 - layerName0: Default + layerNumber0: 146379811 + layerName0: SortingLayer0 nest1: - layerNumber1: 0 - layerName1: Default + layerNumber1: -2136883699 + layerName1: SortingLayer1 nest2: - layerNumber2: 0 - layerName2: Default + layerNumber2: 1583436447 + layerName2: SortingLayer2 --- !u!1 &1622801695 GameObject: m_ObjectHideFlags: 0 diff --git a/Scripts/Core/DrawerAttributes/SortingLayerAttribute.cs b/Scripts/Core/DrawerAttributes/SortingLayerAttribute.cs index d7c3837c..43143a74 100644 --- a/Scripts/Core/DrawerAttributes/SortingLayerAttribute.cs +++ b/Scripts/Core/DrawerAttributes/SortingLayerAttribute.cs @@ -2,8 +2,8 @@ namespace NaughtyAttributes { - [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] - public class SortingLayerAttribute : DrawerAttribute - { - } + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] + public class SortingLayerAttribute : DrawerAttribute + { + } } \ No newline at end of file diff --git a/Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs b/Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs index eda40ab2..cb4ea591 100644 --- a/Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs +++ b/Scripts/Editor/PropertyDrawers/SortingLayerPropertyDrawer.cs @@ -5,87 +5,87 @@ namespace NaughtyAttributes.Editor { - [CustomPropertyDrawer(typeof(SortingLayerAttribute))] - public class SortingLayerPropertyDrawer : PropertyDrawerBase - { - private const string TypeWarningMessage = "{0} must be an int or a string"; + [CustomPropertyDrawer(typeof(SortingLayerAttribute))] + public class SortingLayerPropertyDrawer : PropertyDrawerBase + { + private const string TypeWarningMessage = "{0} must be an int or a string"; - protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) - { - bool validPropertyType = property.propertyType == SerializedPropertyType.String || property.propertyType == SerializedPropertyType.Integer; + protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) + { + bool validPropertyType = property.propertyType == SerializedPropertyType.String || property.propertyType == SerializedPropertyType.Integer; - return validPropertyType - ? GetPropertyHeight(property) - : GetPropertyHeight(property) + GetHelpBoxHeight(); - } + return validPropertyType + ? GetPropertyHeight(property) + : GetPropertyHeight(property) + GetHelpBoxHeight(); + } - protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) - { - EditorGUI.BeginProperty(rect, label, property); + protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) + { + EditorGUI.BeginProperty(rect, label, property); - switch (property.propertyType) - { - case SerializedPropertyType.String: - DrawPropertyForString(rect, property, label, GetLayers()); - break; - case SerializedPropertyType.Integer: - DrawPropertyForInt(rect, property, label, GetLayers()); - break; - default: - string message = string.Format(TypeWarningMessage, property.name); - DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning); - break; - } + switch (property.propertyType) + { + case SerializedPropertyType.String: + DrawPropertyForString(rect, property, label, GetLayers()); + break; + case SerializedPropertyType.Integer: + DrawPropertyForInt(rect, property, label, GetLayers()); + break; + default: + string message = string.Format(TypeWarningMessage, property.name); + DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning); + break; + } - EditorGUI.EndProperty(); - } + EditorGUI.EndProperty(); + } - private string[] GetLayers() - { - Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility); - PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic); - return (string[])sortingLayersProperty.GetValue(null, new object[0]); - } + private string[] GetLayers() + { + Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility); + PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic); + return (string[])sortingLayersProperty.GetValue(null, new object[0]); + } - private static void DrawPropertyForString(Rect rect, SerializedProperty property, GUIContent label, string[] layers) - { - int index = IndexOf(layers, property.stringValue); - int newIndex = EditorGUI.Popup(rect, label.text, index, layers); - string newLayer = layers[newIndex]; + private static void DrawPropertyForString(Rect rect, SerializedProperty property, GUIContent label, string[] layers) + { + int index = IndexOf(layers, property.stringValue); + int newIndex = EditorGUI.Popup(rect, label.text, index, layers); + string newLayer = layers[newIndex]; - if (!property.stringValue.Equals(newLayer, StringComparison.Ordinal)) - { - property.stringValue = layers[newIndex]; - } - } + if (!property.stringValue.Equals(newLayer, StringComparison.Ordinal)) + { + property.stringValue = layers[newIndex]; + } + } - private static void DrawPropertyForInt(Rect rect, SerializedProperty property, GUIContent label, string[] layers) - { - int index = 0; - string layerName = SortingLayer.IDToName(property.intValue); - for (int i = 0; i < layers.Length; i++) - { - if (layerName.Equals(layers[i], StringComparison.Ordinal)) - { - index = i; - break; - } - } + private static void DrawPropertyForInt(Rect rect, SerializedProperty property, GUIContent label, string[] layers) + { + int index = 0; + string layerName = SortingLayer.IDToName(property.intValue); + for (int i = 0; i < layers.Length; i++) + { + if (layerName.Equals(layers[i], StringComparison.Ordinal)) + { + index = i; + break; + } + } - int newIndex = EditorGUI.Popup(rect, label.text, index, layers); - string newLayerName = layers[newIndex]; - int newLayerNumber = SortingLayer.NameToID(newLayerName); + int newIndex = EditorGUI.Popup(rect, label.text, index, layers); + string newLayerName = layers[newIndex]; + int newLayerNumber = SortingLayer.NameToID(newLayerName); - if (property.intValue != newLayerNumber) - { - property.intValue = newLayerNumber; - } - } + if (property.intValue != newLayerNumber) + { + property.intValue = newLayerNumber; + } + } - private static int IndexOf(string[] layers, string layer) - { - var index = Array.IndexOf(layers, layer); - return Mathf.Clamp(index, 0, layers.Length - 1); - } - } + private static int IndexOf(string[] layers, string layer) + { + var index = Array.IndexOf(layers, layer); + return Mathf.Clamp(index, 0, layers.Length - 1); + } + } } diff --git a/Scripts/Test/SortingLayerTest.cs b/Scripts/Test/SortingLayerTest.cs index b582ae0c..ca527140 100644 --- a/Scripts/Test/SortingLayerTest.cs +++ b/Scripts/Test/SortingLayerTest.cs @@ -2,45 +2,45 @@ namespace NaughtyAttributes.Test { - public class SortingLayerTest : MonoBehaviour - { - [SortingLayer] - public int layerNumber0; - - [SortingLayer] - public string layerName0; - - public SortingLayerNest1 nest1; - - [Button] - public void DebugLog() - { - Debug.LogFormat("{0} = {1}", nameof(layerNumber0), layerNumber0); - Debug.LogFormat("{0} = {1}", nameof(layerName0), layerName0); - Debug.LogFormat("LayerToName({0}) = {1}", layerNumber0, SortingLayer.IDToName(layerNumber0)); - Debug.LogFormat("NameToLayer({0}) = {1}", layerName0, SortingLayer.NameToID(layerName0)); - } - } - - [System.Serializable] - public class SortingLayerNest1 - { - [SortingLayer] - public int layerNumber1; - - [SortingLayer] - public string layerName1; - - public SortingLayerNest2 nest2; - } - - [System.Serializable] - public struct SortingLayerNest2 - { - [SortingLayer] - public int layerNumber2; - - [SortingLayer] - public string layerName2; - } + public class SortingLayerTest : MonoBehaviour + { + [SortingLayer] + public int layerNumber0; + + [SortingLayer] + public string layerName0; + + public SortingLayerNest1 nest1; + + [Button] + public void DebugLog() + { + Debug.LogFormat("{0} = {1}", nameof(layerNumber0), layerNumber0); + Debug.LogFormat("{0} = {1}", nameof(layerName0), layerName0); + Debug.LogFormat("LayerToName({0}) = {1}", layerNumber0, SortingLayer.IDToName(layerNumber0)); + Debug.LogFormat("NameToLayer({0}) = {1}", layerName0, SortingLayer.NameToID(layerName0)); + } + } + + [System.Serializable] + public class SortingLayerNest1 + { + [SortingLayer] + public int layerNumber1; + + [SortingLayer] + public string layerName1; + + public SortingLayerNest2 nest2; + } + + [System.Serializable] + public struct SortingLayerNest2 + { + [SortingLayer] + public int layerNumber2; + + [SortingLayer] + public string layerName2; + } }