Skip to content

Commit

Permalink
feat: ScriptableLogic RectとLayerMaskに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
pspkurara committed Nov 6, 2020
1 parent d010ca9 commit 6c3232f
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public void DrawInspector(EditorSkinPartsPropertry property)
}
}
break;
case SerializedPropertyType.LayerMask:
{
var element = property.floatValues.GetArrayElementAtIndex(v.FieldIndex);
SkinnerEditorGUILayout.LayerMaskField(v.DisplayName, element);
}
break;
case SerializedPropertyType.Enum:
{
var element = property.floatValues.GetArrayElementAtIndex(v.FieldIndex);
Expand Down Expand Up @@ -182,6 +188,12 @@ public void DrawInspector(EditorSkinPartsPropertry property)
SkinnerEditorGUILayout.Vector4Field(v.DisplayName, element);
}
break;
case SerializedPropertyType.Rect:
{
var element = property.vector4Values.GetArrayElementAtIndex(v.FieldIndex);
SkinnerEditorGUILayout.RectField(v.DisplayName, element);
}
break;
case SerializedPropertyType.Character:
{
var element = property.stringValues.GetArrayElementAtIndex(v.FieldIndex);
Expand Down Expand Up @@ -296,6 +308,12 @@ private bool CreateDisplayData(UserLogic userLogic)
data.FieldIndex = floatArrayCount;
floatArrayCount++;
}
else if (SkinnerSystemType.IsLayerMask(v.FieldType))
{
data.PropertyType = SerializedPropertyType.LayerMask;
data.FieldIndex = floatArrayCount;
floatArrayCount++;
}
else if (SkinnerSystemType.IsEnum(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Enum;
Expand All @@ -321,6 +339,12 @@ private bool CreateDisplayData(UserLogic userLogic)
data.FieldIndex = vector4ArrayCount;
vector4ArrayCount++;
}
else if (SkinnerSystemType.IsRect(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Rect;
data.FieldIndex = vector4ArrayCount;
vector4ArrayCount++;
}
else if (SkinnerSystemType.IsChar(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Character;
Expand Down
99 changes: 98 additions & 1 deletion Packages/uGUI-Skinner/Editor/SkinnerEditorGUILayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ public static void EnumPopup(GUIContent label, SerializedProperty property, Syst
/// </summary>
/// <param name="label">ラベル</param>
/// <param name="property">プロパティ</param>
/// <param name="displayOptions">表示するテキスト類</param>
/// <param name="optionValues">表示要素に対する値</param>
/// <param name="options">レイアウト設定</param>
public static void IntPopup(GUIContent label, SerializedProperty property, string[] displayOptions, int[] optionValues, params GUILayoutOption[] options)
{
Expand All @@ -250,6 +252,27 @@ public static void IntPopup(GUIContent label, SerializedProperty property, strin
EditorGUI.showMixedValue = showMixedValue;
}

/// <summary>
/// <see cref="EditorGUILayout.LayerField"/>を表示
/// </summary>
/// <param name="label">ラベル</param>
/// <param name="property">プロパティ</param>
/// <param name="options">レイアウト設定</param>
public static void LayerField(GUIContent label, SerializedProperty property, params GUILayoutOption[] options)
{
bool showMixedValue = EditorGUI.showMixedValue;
if (property.hasMultipleDifferentValues)
{
EditorGUI.showMixedValue = true;
}
var result = EditorGUILayout.LayerField(label, property.floatValue.ToInt(), options);
if (!Mathf.Approximately(result, property.floatValue))
{
property.floatValue = result;
}
EditorGUI.showMixedValue = showMixedValue;
}

/// <summary>
/// <see cref="EditorGUILayout.MaskField"/>を表示
/// </summary>
Expand All @@ -258,20 +281,73 @@ public static void IntPopup(GUIContent label, SerializedProperty property, strin
/// <param name="displayOptions">表示するテキスト類</param>
/// <param name="options">レイアウト設定</param>
public static void MaskField(GUIContent label, SerializedProperty property, string[] displayOptions, params GUILayoutOption[] options)
{
var optionValues = displayOptions.Select((_, index) => 1 << index).ToArray();
MaskField(label, property, displayOptions, optionValues, options);
}

/// <summary>
/// <see cref="EditorGUILayout.MaskField"/>を表示
/// </summary>
/// <param name="label">ラベル</param>
/// <param name="property">プロパティ</param>
/// <param name="displayOptions">表示するテキスト類</param>
/// <param name="optionValues">表示要素に対する値</param>
/// <param name="options">レイアウト設定</param>
public static void MaskField(GUIContent label, SerializedProperty property, string[] displayOptions, int[] optionValues, params GUILayoutOption[] options)
{
bool showMixedValue = EditorGUI.showMixedValue;
if (property.hasMultipleDifferentValues)
{
EditorGUI.showMixedValue = true;
}
var result = EditorGUILayout.MaskField(label, property.floatValue.ToInt(), displayOptions, options);

int displayValue = -1;
var castedProperty = property.floatValue.ToInt();
if (castedProperty != -1)
{
displayValue = 0;
for (var i = 0; i < optionValues.Length; ++i)
{
if (0 < (castedProperty & (1 << optionValues[i])))
displayValue |= 1 << i;
}
}

var displayResult = EditorGUILayout.MaskField(label, displayValue, displayOptions, options);

int result = -1;
if (displayResult != -1)
{
result = 0;
for (var i = 0; i < optionValues.Length; ++i)
{
if (0 < (displayResult & (1 << i)))
result |= 1 << optionValues[i];
}
}

if (!Mathf.Approximately(result, property.floatValue))
{
property.floatValue = result;
}
EditorGUI.showMixedValue = showMixedValue;
}

/// <summary>
/// <see cref="EditorGUILayout.MaskField"/>の<see cref="LayerMask"/>対応バージョンを表示
/// </summary>
/// <param name="label">ラベル</param>
/// <param name="property">プロパティ</param>
/// <param name="options">レイアウト設定</param>
public static void LayerMaskField(GUIContent label, SerializedProperty property, params GUILayoutOption[] options)
{
string[] displayOptions;
int[] optionValues;
SkinnerEditorUtility.GetMaskOptionsWithLayer(out displayOptions, out optionValues);
MaskField(label, property, displayOptions, optionValues, options);
}

#endregion

#region vector4
Expand Down Expand Up @@ -374,6 +450,27 @@ public static void Vector4Field(GUIContent label, SerializedProperty property, p
EditorGUI.showMixedValue = showMixedValue;
}

/// <summary>
/// <see cref="EditorGUILayout.RectField"/>を表示
/// </summary>
/// <param name="label">ラベル</param>
/// <param name="property">プロパティ</param>
/// <param name="options">レイアウト設定</param>
public static void RectField(GUIContent label, SerializedProperty property, params GUILayoutOption[] options)
{
bool showMixedValue = EditorGUI.showMixedValue;
if (property.hasMultipleDifferentValues)
{
EditorGUI.showMixedValue = true;
}
Vector4 result = EditorGUILayout.RectField(label, property.vector4Value.ToRect(), options).ToVector();
if (result != property.vector4Value)
{
property.vector4Value = result;
}
EditorGUI.showMixedValue = showMixedValue;
}

#endregion

#region string
Expand Down
31 changes: 31 additions & 0 deletions Packages/uGUI-Skinner/Editor/SkinnerEditorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ private static void FieldClean(SerializedProperty arrayObj, object defaultValue)
{
convertedFloat = (int)defaultValue;
}
if (type == typeof(LayerMask))
{
convertedFloat = ((LayerMask)defaultValue);
}
if (type == typeof(bool))
{
convertedFloat = ((bool)defaultValue).ToFloat();
Expand Down Expand Up @@ -129,6 +133,10 @@ private static void FieldClean(SerializedProperty arrayObj, object defaultValue)
{
convertedVector4 = (Vector4)defaultValue;
}
if (type == typeof(Rect))
{
convertedVector4 = ((Rect)defaultValue).ToVector();
}
}
arrayObj.vector4Value = convertedVector4;
break;
Expand Down Expand Up @@ -353,6 +361,29 @@ public static void GetPopupOptionsFromEnum(Type enumType, out string[] displayOp
optionValues = enumType.GetEnumValues().Cast<int>().ToArray();
}


/// <summary>
/// <see cref="EditorGUI.MaskField"/>等で使う表示名や値をLayerから取得する
/// </summary>
/// <param name="displayOptions">表示名配列</param>
/// <param name="optionValues">値配列</param>
public static void GetMaskOptionsWithLayer(out string[] displayOptions, out int[] optionValues)
{
var layerOptionValues = new List<int>();
var layerDisplayOptions = new List<string>();
for (int i = 0; i < 32; i++)
{
var layerName = LayerMask.LayerToName(i);
if (layerName.Length > 0)
{
layerDisplayOptions.Add(layerName);
layerOptionValues.Add(1 << i);
}
}
displayOptions = layerDisplayOptions.ToArray();
optionValues = layerOptionValues.ToArray();
}

/// <summary>
/// <see cref="EditorSkinPartsPropertry">を<see cref="SkinPartsPropertry"/>にマップする
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Packages/uGUI-Skinner/Runtime/SkinnerSystemType.FieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static bool IsObjectReferenceValue(Type type)
/// <param name="type">型</param>
public static bool IsFloatValue(Type type)
{
return IsFloat(type) || IsInteger(type) || IsEnum(type) || IsBoolean(type);
return IsFloat(type) || IsInteger(type) || IsLayerMask(type) || IsEnum(type) || IsBoolean(type);
}

/// <summary>
Expand All @@ -35,7 +35,7 @@ public static bool IsFloatValue(Type type)
/// <param name="type">型</param>
public static bool IsVector4Value(Type type)
{
return IsVector2(type) || IsVector3(type) || IsVector4(type) || IsColor(type);
return IsVector2(type) || IsVector3(type) || IsVector4(type) || IsColor(type) || IsRect(type);
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions Packages/uGUI-Skinner/Runtime/SkinnerSystemType.ObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public static bool IsInteger(Type type)
return type == typeof(int);
}

public static bool IsLayerMask(Type type)
{
return type == typeof(LayerMask);
}

public static bool IsEnum(Type type)
{
return type.IsEnum;
Expand Down Expand Up @@ -65,6 +70,11 @@ public static bool IsVector4(Type type)
return type == typeof(Vector4);
}

public static bool IsRect(Type type)
{
return type == typeof(Rect);
}

#endregion

#region String
Expand Down
19 changes: 19 additions & 0 deletions Packages/uGUI-Skinner/Runtime/ValueUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ public static Vector4 ToVector(this Color32 color)
return color.ToVector();
}

public static Vector4 ToVector(this Rect rect)
{
return new Vector4(rect.x, rect.y, rect.width, rect.height);
}

public static float ToFloat(this bool self)
{
return self ? 1 : 0;
}

public static float ToFloat(this LayerMask self)
{
return self.value;
}

#endregion

#region 逆変換
Expand All @@ -34,6 +44,10 @@ public static Color ToColor(this Vector4 self)
return new Color(self.x, self.y, self.z, self.w);
}

public static Rect ToRect(this Vector4 self)
{
return new Rect(self.x, self.y, self.z, self.w);
}
public static int ToInt(this float self)
{
return Mathf.RoundToInt(self);
Expand All @@ -44,6 +58,11 @@ public static bool ToBool(this float self)
return self.ToInt() > 0;
}

public static LayerMask ToLayerMask(this float self)
{
return new LayerMask() { value = self.ToInt() };
}

#endregion

}
Expand Down

0 comments on commit 6c3232f

Please sign in to comment.