custom attributes wip

This commit is contained in:
2016-08-24 15:53:00 +07:00
parent a3089225b9
commit ed3f768e0d
13 changed files with 165 additions and 159 deletions

View File

@@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: 92c4c08c8d0f648efa0a3f602e3e8710
folderAsset: yes
timeCreated: 1472012226
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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<int>();
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.");
}
}
}
}

View File

@@ -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<GUIContent> GetAllSortingLayers() {
var result = new List<GUIContent>();
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.");
}
}
}
}

View File

@@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: 75cfccb798f0a434492af628d750c4a4
timeCreated: 1472012246
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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<string> GetAllSortingLayers() {
var result = new List<string>();
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<int>();
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.");
}
}
}
}

View File

@@ -22,7 +22,7 @@ namespace FlashTools.Internal {
[System.Serializable] [System.Serializable]
public struct Settings { public struct Settings {
[SwfPowerOfTwoIfAttribute("AtlasPowerOfTwo", 32, 8192)] [SwfPowerOfTwoIfAttribute(5, 13, "AtlasPowerOfTwo")]
public int MaxAtlasSize; public int MaxAtlasSize;
public int AtlasPadding; public int AtlasPadding;
public int PixelsPerUnit; public int PixelsPerUnit;

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -1,6 +0,0 @@
using UnityEngine;
namespace FlashTools.Internal {
public class SwfSortingLayerAttribute : PropertyAttribute {
}
}

View File

@@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: a33d7ac8d525c4e8fbe3084eacab4f07
timeCreated: 1472011819
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -38,9 +38,12 @@ TagManager:
- -
- -
m_SortingLayers: m_SortingLayers:
- name: SortingDown
uniqueID: 811819031
locked: 0
- name: Default - name: Default
uniqueID: 0 uniqueID: 0
locked: 0 locked: 0
- name: jvjhv - name: SortingUp
uniqueID: 811819031 uniqueID: 756447657
locked: 0 locked: 0