Skip to content

Commit

Permalink
feat: ScriptableLogic FieldTypeからFlagsAttributeを取得してMaskFieldに自動的に切り替…
Browse files Browse the repository at this point in the history
…わるように修正
  • Loading branch information
pspkurara committed Nov 6, 2020
1 parent d9cab0e commit 740f1c7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using UnityEditor;
using System.Collections.Generic;
using Type = System.Type;
using Attribute = System.Attribute;
using System.Linq;

namespace Pspkurara.UI.Skinner
{
Expand All @@ -18,7 +20,9 @@ internal sealed class UserLogicVariableDisplayData
public SerializedPropertyType PropertyType;
public UserLogicVariable VariableData;
public int FieldIndex;
public GUIContent[] PopupDisplayName;
public string[] PopupDisplayName;
public List<Type> PropertyTypeDefinedAttributes;
public List<Attribute> PropertyTypeAttributes;
public int[] PopupValue;

}
Expand Down Expand Up @@ -124,7 +128,14 @@ public void DrawInspector(EditorSkinPartsPropertry property)
case SerializedPropertyType.Enum:
{
var element = property.floatValues.GetArrayElementAtIndex(v.FieldIndex);
SkinnerEditorGUILayout.IntPopup(v.DisplayName, element, v.PopupDisplayName, v.PopupValue);
if (v.PropertyTypeDefinedAttributes.Contains(typeof(System.FlagsAttribute)))
{
SkinnerEditorGUILayout.MaskField(v.DisplayName, element, v.PopupDisplayName);
}
else
{
SkinnerEditorGUILayout.IntPopup(v.DisplayName, element, v.PopupDisplayName, v.PopupValue);
}
}
break;
case SerializedPropertyType.Vector2:
Expand Down Expand Up @@ -293,6 +304,8 @@ private bool CreateDisplayData(UserLogic userLogic)
var displayName = v.FieldDisplayName == null ? SkinnerEditorUtility.GetEditorName(v.FieldType.Name) : v.FieldDisplayName;
data.DisplayName = new GUIContent(displayName);
data.VariableData = v;
data.PropertyTypeAttributes = v.FieldType.GetCustomAttributesWithBaseType().ToList();
data.PropertyTypeDefinedAttributes = data.PropertyTypeAttributes.Select(a => a.GetType()).ToList();
userLogicVariableDisplayDatas.Add(data);
}
}
Expand Down
69 changes: 69 additions & 0 deletions Packages/uGUI-Skinner/Editor/SkinnerAttributeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using Object = UnityEngine.Object;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace Pspkurara.UI.Skinner
{

/// <summary>
/// <see cref="Attribute"/>関係の拡張関数群
/// </summary>
public static class SkinnerAttributeExtensions
{

/// <summary>
/// <see cref="Attribute.IsDefined"/>を親クラスも含めて行う
/// </summary>
public static bool IsDefinedWithBaseType(this Type element, bool inherit = true)
{
return IsDefinedWithBaseType(element, typeof(Attribute), inherit);
}

/// <summary>
/// <see cref="Attribute.IsDefined"/>を親クラスも含めて行う
/// </summary>
public static bool IsDefinedWithBaseType(this Type element, Type attributeType, bool inherit = true)
{
if (element == null) return false;
if (element.IsDefined(attributeType, inherit))
{
return true;
}
return element.IsDefinedWithBaseType(attributeType.BaseType, inherit);
}

/// <summary>
/// <see cref="Attribute.GetCustomAttribute"/>を親クラスも含めて行う
/// </summary>
public static Attribute[] GetCustomAttributesWithBaseType(this Type element, bool inherit = true)
{
return GetCustomAttributesWithBaseType(element, typeof(Attribute), inherit);
}

/// <summary>
/// <see cref="Attribute.GetCustomAttribute"/>を親クラスも含めて行う
/// </summary>
public static Attribute[] GetCustomAttributesWithBaseType(this Type element, Type attributeType, bool inherit = true)
{
if (element == null) return Array.Empty<Attribute>();
HashSet<Attribute> attributeList = new HashSet<Attribute>();
if (attributeType == null) attributeType = typeof(Attribute);
AccumulationAttributesWithBaseType(element, attributeList, attributeType, inherit);
return attributeList.ToArray();
}

/// <summary>
/// <see cref="GetCustomAttributesWithBaseType(Type, Type, bool)"/>の内部処理
/// </summary>
private static void AccumulationAttributesWithBaseType(Type element, HashSet<Attribute> attributeList, Type attributeType, bool inherit)
{
if (element == null) return;
attributeList.UnionWith(element.GetCustomAttributes(attributeType, inherit).Cast<Attribute>());
AccumulationAttributesWithBaseType(element.BaseType, attributeList, attributeType, inherit);
}

}

}
11 changes: 11 additions & 0 deletions Packages/uGUI-Skinner/Editor/SkinnerAttributeExtensions.cs.meta

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

28 changes: 25 additions & 3 deletions Packages/uGUI-Skinner/Editor/SkinnerEditorGUILayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static void Toggle(GUIContent label, SerializedProperty property, params
/// <param name="options">レイアウト設定</param>
public static void EnumPopup(GUIContent label, SerializedProperty property, System.Type enumType, params GUILayoutOption[] options)
{
GUIContent[] popupDisplayName;
string[] popupDisplayName;
int[] popupValue;
SkinnerEditorUtility.GetPopupOptionsFromEnum(enumType, out popupDisplayName, out popupValue);
IntPopup(label, property, popupDisplayName, popupValue, options);
Expand All @@ -189,14 +189,36 @@ public static void EnumPopup(GUIContent label, SerializedProperty property, Syst
/// <param name="label">ラベル</param>
/// <param name="property">プロパティ</param>
/// <param name="options">レイアウト設定</param>
public static void IntPopup(GUIContent label, SerializedProperty property, GUIContent[] displayOptions, int[] optionValues, params GUILayoutOption[] options)
public static void IntPopup(GUIContent label, SerializedProperty property, string[] displayOptions, int[] optionValues, params GUILayoutOption[] options)
{
bool showMixedValue = EditorGUI.showMixedValue;
if (property.hasMultipleDifferentValues)
{
EditorGUI.showMixedValue = true;
}
var result = EditorGUILayout.IntPopup(label, property.floatValue.ToInt(), displayOptions, optionValues, options);
var result = EditorGUILayout.IntPopup(label.text, property.floatValue.ToInt(), displayOptions, optionValues, options);
if (!Mathf.Approximately(result, property.floatValue))
{
property.floatValue = result;
}
EditorGUI.showMixedValue = showMixedValue;
}

/// <summary>
/// <see cref="EditorGUILayout.MaskField"/>を表示
/// </summary>
/// <param name="label">ラベル</param>
/// <param name="property">プロパティ</param>
/// <param name="displayOptions">表示するテキスト類</param>
/// <param name="options">レイアウト設定</param>
public static void MaskField(GUIContent label, SerializedProperty property, string[] displayOptions, params GUILayoutOption[] options)
{
bool showMixedValue = EditorGUI.showMixedValue;
if (property.hasMultipleDifferentValues)
{
EditorGUI.showMixedValue = true;
}
var result = EditorGUILayout.MaskField(label, property.floatValue.ToInt(), displayOptions, options);
if (!Mathf.Approximately(result, property.floatValue))
{
property.floatValue = result;
Expand Down
5 changes: 3 additions & 2 deletions Packages/uGUI-Skinner/Editor/SkinnerEditorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ public static void DrawLine()

public static string GetEditorName(string name)
{
return name;
var m = Regex.Matches(name, "[\\x41-\\x5a]+[\\x61-\\x7a]*");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < m.Count; i++)
Expand All @@ -347,9 +348,9 @@ public static string GetEditorName(string name)
/// <param name="enumType">Enumの型</param>
/// <param name="displayOptions">表示名配列</param>
/// <param name="optionValues">値配列</param>
public static void GetPopupOptionsFromEnum(Type enumType, out GUIContent[] displayOptions, out int[] optionValues)
public static void GetPopupOptionsFromEnum(Type enumType, out string[] displayOptions, out int[] optionValues)
{
displayOptions = enumType.GetEnumNames().Select(n => new GUIContent(GetEditorName(n))).ToArray();
displayOptions = enumType.GetEnumNames();
optionValues = enumType.GetEnumValues().Cast<int>().ToArray();
}

Expand Down

0 comments on commit 740f1c7

Please sign in to comment.