From ed3f768e0d409c8b90ddc33edc933f966918ec9b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 24 Aug 2016 15:53:00 +0700 Subject: [PATCH] custom attributes wip --- .../Internal/Editor/SwfEditorTools.meta | 9 -- .../SwfEditorTools/SwfPowerOfTwoIfDrawer.cs | 60 -------- .../SwfEditorTools/SwfSortingLayerDrawer.cs | 43 ------ .../SwfSortingLayerDrawer.cs.meta | 12 -- .../Internal/Editor/SwfPropertyDrawers.cs | 142 ++++++++++++++++++ ...wer.cs.meta => SwfPropertyDrawers.cs.meta} | 0 .../Scripts/Internal/SwfConverterSettings.cs | 2 +- .../Internal/SwfPowerOfTwoIfAttribute.cs | 14 -- .../Scripts/Internal/SwfPropertyAttributes.cs | 17 +++ ....cs.meta => SwfPropertyAttributes.cs.meta} | 0 .../Internal/SwfSortingLayerAttribute.cs | 6 - .../Internal/SwfSortingLayerAttribute.cs.meta | 12 -- ProjectSettings/TagManager.asset | 7 +- 13 files changed, 165 insertions(+), 159 deletions(-) delete mode 100644 Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools.meta delete mode 100644 Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfPowerOfTwoIfDrawer.cs delete mode 100644 Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfSortingLayerDrawer.cs delete mode 100644 Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfSortingLayerDrawer.cs.meta create mode 100644 Assets/FlashTools/Scripts/Internal/Editor/SwfPropertyDrawers.cs rename Assets/FlashTools/Scripts/Internal/Editor/{SwfEditorTools/SwfPowerOfTwoIfDrawer.cs.meta => SwfPropertyDrawers.cs.meta} (100%) delete mode 100644 Assets/FlashTools/Scripts/Internal/SwfPowerOfTwoIfAttribute.cs create mode 100644 Assets/FlashTools/Scripts/Internal/SwfPropertyAttributes.cs rename Assets/FlashTools/Scripts/Internal/{SwfPowerOfTwoIfAttribute.cs.meta => SwfPropertyAttributes.cs.meta} (100%) delete mode 100644 Assets/FlashTools/Scripts/Internal/SwfSortingLayerAttribute.cs delete mode 100644 Assets/FlashTools/Scripts/Internal/SwfSortingLayerAttribute.cs.meta diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools.meta b/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools.meta deleted file mode 100644 index 405a777..0000000 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools.meta +++ /dev/null @@ -1,9 +0,0 @@ -fileFormatVersion: 2 -guid: 92c4c08c8d0f648efa0a3f602e3e8710 -folderAsset: yes -timeCreated: 1472012226 -licenseType: Free -DefaultImporter: - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfPowerOfTwoIfDrawer.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfPowerOfTwoIfDrawer.cs deleted file mode 100644 index 866dc3e..0000000 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfPowerOfTwoIfDrawer.cs +++ /dev/null @@ -1,60 +0,0 @@ -using UnityEngine; -using UnityEditor; -using System.Linq; -using System.Collections.Generic; - -namespace FlashTools.Internal.SwfEditorTools { - [CustomPropertyDrawer(typeof(SwfPowerOfTwoIfAttribute))] - public class SwfPowerOfTwoIfDrawer : PropertyDrawer { - - SerializedProperty FindBoolProperty(SerializedProperty property, string bool_prop) { - var prop = property.Copy(); - while ( prop.NextVisible(false) ) { - if ( prop.propertyType == SerializedPropertyType.Boolean && prop.name == bool_prop ) { - return prop; - } - } - return null; - } - - void PropertyToPowerOfTwo(SerializedProperty property) { - if ( property.propertyType == SerializedPropertyType.Integer ) { - if ( !Mathf.IsPowerOfTwo(property.intValue) ) { - property.intValue = Mathf.ClosestPowerOfTwo(property.intValue); - property.serializedObject.ApplyModifiedProperties(); - } - } - } - - int[] GenPowerOfTwoValues(int min, int max) { - var values = new List(); - if ( !Mathf.IsPowerOfTwo(min) ) { - min = Mathf.NextPowerOfTwo(min); - } - while ( min <= max ) { - values.Add(min); - min = Mathf.NextPowerOfTwo(min + 1); - } - return values.ToArray(); - } - - public override void OnGUI( - Rect position, SerializedProperty property, GUIContent label) - { - if ( property.propertyType == SerializedPropertyType.Integer ) { - var attr = attribute as SwfPowerOfTwoIfAttribute; - var bool_prop = FindBoolProperty(property, attr.BoolProp); - if ( bool_prop != null && bool_prop.boolValue ) { - PropertyToPowerOfTwo(property); - var values = GenPowerOfTwoValues(attr.Min, attr.Max); - var vnames = values.Select(p => new GUIContent(p.ToString())).ToArray(); - EditorGUI.IntPopup(position, property, vnames, values, label); - } else { - EditorGUI.PropertyField(position, property, label); - } - } else { - EditorGUI.LabelField(position, label.text, "Use SwfPowerOfTwoIf with integer attribute."); - } - } - } -} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfSortingLayerDrawer.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfSortingLayerDrawer.cs deleted file mode 100644 index 7860b71..0000000 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfSortingLayerDrawer.cs +++ /dev/null @@ -1,43 +0,0 @@ -using UnityEngine; -using UnityEditor; -using System.Linq; -using System.Collections.Generic; - -namespace FlashTools.Internal.SwfEditorTools { - [CustomPropertyDrawer(typeof(SwfSortingLayerAttribute))] - public class SwfSortingLayerDrawer : PropertyDrawer { - - List GetAllSortingLayers() { - var result = new List(); - var tag_manager_so = new SerializedObject( - AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]); - var layers = tag_manager_so.FindProperty("m_SortingLayers"); - if ( layers != null && layers.isArray ) { - for ( var i = 0; i < layers.arraySize; ++i ) { - var layer_prop = layers.GetArrayElementAtIndex(i); - var layer_name_prop = layer_prop.FindPropertyRelative("name"); - if ( !string.IsNullOrEmpty(layer_name_prop.stringValue) ) { - result.Add(new GUIContent(layer_name_prop.stringValue)); - } - } - } - return result; - } - - public override void OnGUI( - Rect position, SerializedProperty property, GUIContent label) - { - var all_sorting_layers = GetAllSortingLayers(); - if ( property.propertyType == SerializedPropertyType.String ) { - var new_sorting_layer = EditorGUI.Popup( - position, - label, - all_sorting_layers.FindIndex(p => p.text == property.stringValue), - all_sorting_layers.ToArray()); - property.stringValue = all_sorting_layers[new_sorting_layer].text; - } else { - EditorGUI.LabelField(position, label.text, "Use SwfSortingLayer with string attribute."); - } - } - } -} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfSortingLayerDrawer.cs.meta b/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfSortingLayerDrawer.cs.meta deleted file mode 100644 index 79edfd9..0000000 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfSortingLayerDrawer.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: 75cfccb798f0a434492af628d750c4a4 -timeCreated: 1472012246 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfPropertyDrawers.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfPropertyDrawers.cs new file mode 100644 index 0000000..bf9b02c --- /dev/null +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfPropertyDrawers.cs @@ -0,0 +1,142 @@ +using UnityEngine; +using UnityEditor; +using System.Linq; +using System.Collections.Generic; + +namespace FlashTools.Internal.SwfEditorTools { + + // + // SwfSortingLayerDrawer + // + + [CustomPropertyDrawer(typeof(SwfSortingLayerAttribute))] + public class SwfSortingLayerDrawer : PropertyDrawer { + + const string DefaultLayerName = "Default"; + + static List GetAllSortingLayers() { + var result = new List(); + var tag_assets = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset"); + if ( tag_assets.Length > 0 ) { + var tag_manager = new SerializedObject(tag_assets[0]); + var layers = tag_manager.FindProperty("m_SortingLayers"); + if ( layers != null && layers.isArray ) { + for ( var i = 0; i < layers.arraySize; ++i ) { + var layer_prop = layers.GetArrayElementAtIndex(i); + var layer_prop_name = layer_prop != null + ? layer_prop.FindPropertyRelative("name") + : null; + var layer_name = layer_prop_name != null && layer_prop_name.propertyType == SerializedPropertyType.String + ? layer_prop_name.stringValue + : string.Empty; + if ( !string.IsNullOrEmpty(layer_name) ) { + result.Add(layer_name); + } + } + } + } + if ( !result.Contains(DefaultLayerName) ) { + result.Add(DefaultLayerName); + } + return result; + } + + static void ValidateProperty(SerializedProperty property) { + if ( property.propertyType == SerializedPropertyType.String ) { + var all_sorting_layers = GetAllSortingLayers(); + if ( !all_sorting_layers.Contains(property.stringValue) ) { + property.stringValue = DefaultLayerName; + property.serializedObject.ApplyModifiedProperties(); + } + } + } + + public override void OnGUI( + Rect position, SerializedProperty property, GUIContent label) + { + var all_sorting_layers = GetAllSortingLayers(); + if ( property.propertyType == SerializedPropertyType.String ) { + ValidateProperty(property); + var old_sorting_layer = property.stringValue; + var sorting_layer_index = EditorGUI.Popup( + position, + label, + all_sorting_layers.FindIndex(p => p == property.stringValue), + all_sorting_layers.Select(p => new GUIContent(p)).ToArray()); + property.stringValue = all_sorting_layers[sorting_layer_index]; + if ( old_sorting_layer != property.stringValue ) { + property.serializedObject.ApplyModifiedProperties(); + } + } else { + EditorGUI.LabelField(position, label.text, "Use SwfSortingLayer with string attribute."); + } + } + } + + // + // SwfPowerOfTwoIfDrawer + // + + [CustomPropertyDrawer(typeof(SwfPowerOfTwoIfAttribute))] + public class SwfPowerOfTwoIfDrawer : PropertyDrawer { + + static SerializedProperty FindNextBoolProperty(SerializedProperty property, string next_prop) { + var prop = property.Copy(); + while ( prop.NextVisible(false) ) { + if ( prop.name == next_prop && prop.propertyType == SerializedPropertyType.Boolean ) { + return prop; + } + } + return null; + } + + static int GetPowerOfTwo(int value) { + return Mathf.RoundToInt(Mathf.Pow(2, value)); + } + + int[] GenPowerOfTwoValues(int min_pow2, int max_pow2) { + var values = new List(); + while ( min_pow2 <= max_pow2 ) { + values.Add(GetPowerOfTwo(min_pow2)); + ++min_pow2; + } + return values.ToArray(); + } + + static void ValidateProperty(SerializedProperty property, bool need_pow2, int min_pow2, int max_pow2) { + if ( property.propertyType == SerializedPropertyType.Integer ) { + var old_value = property.intValue; + if ( need_pow2 && !Mathf.IsPowerOfTwo(property.intValue) ) { + property.intValue = Mathf.ClosestPowerOfTwo(property.intValue); + } + property.intValue = Mathf.Clamp( + property.intValue, + GetPowerOfTwo(min_pow2), + GetPowerOfTwo(max_pow2)); + if ( old_value != property.intValue ) { + property.serializedObject.ApplyModifiedProperties(); + } + } + } + + public override void OnGUI( + Rect position, SerializedProperty property, GUIContent label) + { + if ( property.propertyType == SerializedPropertyType.Integer ) { + var attr = attribute as SwfPowerOfTwoIfAttribute; + var bool_prop = FindNextBoolProperty(property, attr.BoolProp); + var need_pow2 = (bool_prop != null && bool_prop.boolValue); + ValidateProperty(property, need_pow2, attr.MinPow2, attr.MaxPow2); + if ( need_pow2 ) { + var values = GenPowerOfTwoValues(attr.MinPow2, attr.MaxPow2); + var vnames = values.Select(p => new GUIContent(p.ToString())).ToArray(); + EditorGUI.IntPopup(position, property, vnames, values, label); + } else { + EditorGUI.PropertyField(position, property, label); + } + } else { + EditorGUI.LabelField(position, label.text, "Use SwfPowerOfTwoIf with integer attribute."); + } + } + } +} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfPowerOfTwoIfDrawer.cs.meta b/Assets/FlashTools/Scripts/Internal/Editor/SwfPropertyDrawers.cs.meta similarity index 100% rename from Assets/FlashTools/Scripts/Internal/Editor/SwfEditorTools/SwfPowerOfTwoIfDrawer.cs.meta rename to Assets/FlashTools/Scripts/Internal/Editor/SwfPropertyDrawers.cs.meta diff --git a/Assets/FlashTools/Scripts/Internal/SwfConverterSettings.cs b/Assets/FlashTools/Scripts/Internal/SwfConverterSettings.cs index 54fc229..79ec58d 100644 --- a/Assets/FlashTools/Scripts/Internal/SwfConverterSettings.cs +++ b/Assets/FlashTools/Scripts/Internal/SwfConverterSettings.cs @@ -22,7 +22,7 @@ namespace FlashTools.Internal { [System.Serializable] public struct Settings { - [SwfPowerOfTwoIfAttribute("AtlasPowerOfTwo", 32, 8192)] + [SwfPowerOfTwoIfAttribute(5, 13, "AtlasPowerOfTwo")] public int MaxAtlasSize; public int AtlasPadding; public int PixelsPerUnit; diff --git a/Assets/FlashTools/Scripts/Internal/SwfPowerOfTwoIfAttribute.cs b/Assets/FlashTools/Scripts/Internal/SwfPowerOfTwoIfAttribute.cs deleted file mode 100644 index b92543e..0000000 --- a/Assets/FlashTools/Scripts/Internal/SwfPowerOfTwoIfAttribute.cs +++ /dev/null @@ -1,14 +0,0 @@ -using UnityEngine; - -namespace FlashTools.Internal { - public class SwfPowerOfTwoIfAttribute : PropertyAttribute { - public string BoolProp; - public int Min; - public int Max; - public SwfPowerOfTwoIfAttribute(string bool_prop, int min, int max) { - BoolProp = bool_prop; - Min = min; - Max = max; - } - } -} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/SwfPropertyAttributes.cs b/Assets/FlashTools/Scripts/Internal/SwfPropertyAttributes.cs new file mode 100644 index 0000000..0586a46 --- /dev/null +++ b/Assets/FlashTools/Scripts/Internal/SwfPropertyAttributes.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace FlashTools.Internal { + public class SwfSortingLayerAttribute : PropertyAttribute { + } + + public class SwfPowerOfTwoIfAttribute : PropertyAttribute { + public int MinPow2; + public int MaxPow2; + public string BoolProp; + public SwfPowerOfTwoIfAttribute(int min_pow2, int max_pow2, string prop) { + MinPow2 = min_pow2; + MaxPow2 = max_pow2; + BoolProp = prop; + } + } +} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/SwfPowerOfTwoIfAttribute.cs.meta b/Assets/FlashTools/Scripts/Internal/SwfPropertyAttributes.cs.meta similarity index 100% rename from Assets/FlashTools/Scripts/Internal/SwfPowerOfTwoIfAttribute.cs.meta rename to Assets/FlashTools/Scripts/Internal/SwfPropertyAttributes.cs.meta diff --git a/Assets/FlashTools/Scripts/Internal/SwfSortingLayerAttribute.cs b/Assets/FlashTools/Scripts/Internal/SwfSortingLayerAttribute.cs deleted file mode 100644 index 9c54173..0000000 --- a/Assets/FlashTools/Scripts/Internal/SwfSortingLayerAttribute.cs +++ /dev/null @@ -1,6 +0,0 @@ -using UnityEngine; - -namespace FlashTools.Internal { - public class SwfSortingLayerAttribute : PropertyAttribute { - } -} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/SwfSortingLayerAttribute.cs.meta b/Assets/FlashTools/Scripts/Internal/SwfSortingLayerAttribute.cs.meta deleted file mode 100644 index b6d3292..0000000 --- a/Assets/FlashTools/Scripts/Internal/SwfSortingLayerAttribute.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: a33d7ac8d525c4e8fbe3084eacab4f07 -timeCreated: 1472011819 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index c5dade8..ba7a82b 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -38,9 +38,12 @@ TagManager: - - m_SortingLayers: + - name: SortingDown + uniqueID: 811819031 + locked: 0 - name: Default uniqueID: 0 locked: 0 - - name: jvjhv - uniqueID: 811819031 + - name: SortingUp + uniqueID: 756447657 locked: 0