Skip to content

Commit

Permalink
feat: ユーザー変数にIDを持たせてインデックスを安全に取得できるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
pspkurara committed Sep 9, 2020
1 parent 578fe8d commit d60e4d7
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define SKIP_LOGIC_CACHE
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
Expand Down Expand Up @@ -192,6 +193,12 @@ private bool CreateDisplayData(UserLogic userLogic)
{
if (!userLogic) return false;

#if SKIP_LOGIC_CACHE

currentUserLogic = userLogic;

#else

// ここでキャッシュしてクリエイトを抑制
if (currentUserLogic == userLogic)
{
Expand All @@ -206,6 +213,8 @@ private bool CreateDisplayData(UserLogic userLogic)
this.userLogicVariableDisplayDatas = m_CachedVariableDisplayDatas[userLogicType];
}

# endif

var userLogicVariableDisplayDatas = new List<UserLogicVariableDisplayData>();
objectReferenceArrayCount = 0;
boolArrayCount = 0;
Expand All @@ -218,69 +227,69 @@ private bool CreateDisplayData(UserLogic userLogic)
{
bool isUnCorrect = false;
var data = new UserLogicVariableDisplayData();
if (v.FieldType == typeof(Object) || v.FieldType.IsSubclassOf(typeof(Object)))
if (SkinnerSystemType.IsObject(v.FieldType))
{
data.PropertyType = SerializedPropertyType.ObjectReference;
data.FieldIndex = objectReferenceArrayCount;
objectReferenceArrayCount++;
}
else if (v.FieldType == typeof(bool))
else if (SkinnerSystemType.IsBoolean(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Boolean;
data.FieldIndex = boolArrayCount;
boolArrayCount++;
}
else if (v.FieldType == typeof(Color) || v.FieldType == typeof(Color32))
else if (SkinnerSystemType.IsColor(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Color;
data.FieldIndex = colorArrayCount;
colorArrayCount++;
}
else if (v.FieldType == typeof(float))
else if (SkinnerSystemType.IsFloat(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Float;
data.FieldIndex = floatArrayCount;
floatArrayCount++;
}
else if (v.FieldType == typeof(int))
else if (SkinnerSystemType.IsInteger(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Integer;
data.FieldIndex = intArrayCount;
intArrayCount++;
}
else if (v.FieldType.IsEnum)
else if (SkinnerSystemType.IsEnum(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Enum;
data.PopupDisplayName = v.FieldType.GetEnumNames().Select(n => new GUIContent(n)).ToArray();
data.PopupValue = v.FieldType.GetEnumValues().Cast<int>().ToArray();
data.FieldIndex = intArrayCount;
intArrayCount++;
}
else if (v.FieldType == typeof(Vector2))
else if (SkinnerSystemType.IsVector2(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Vector2;
data.FieldIndex = vector4ArrayCount;
vector4ArrayCount++;
}
else if (v.FieldType == typeof(Vector3))
else if (SkinnerSystemType.IsVector3(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Vector3;
data.FieldIndex = vector4ArrayCount;
vector4ArrayCount++;
}
else if (v.FieldType == typeof(Vector4))
else if (SkinnerSystemType.IsVector4(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Vector4;
data.FieldIndex = vector4ArrayCount;
vector4ArrayCount++;
}
else if (v.FieldType == typeof(char))
else if (SkinnerSystemType.IsChar(v.FieldType))
{
data.PropertyType = SerializedPropertyType.Character;
data.FieldIndex = stringArrayCount;
stringArrayCount++;
}
else if (v.FieldType == typeof(string))
else if (SkinnerSystemType.IsString(v.FieldType))
{
data.PropertyType = SerializedPropertyType.String;
data.FieldIndex = stringArrayCount;
Expand All @@ -297,8 +306,24 @@ private bool CreateDisplayData(UserLogic userLogic)
userLogicVariableDisplayDatas.Add(data);
}
}

#if SKIP_LOGIC_CACHE

var userLogicType = userLogic.GetType();
if (m_CachedVariableDisplayDatas.ContainsKey(userLogicType))
{
m_CachedVariableDisplayDatas[userLogicType].Clear();
m_CachedVariableDisplayDatas[userLogicType].AddRange(userLogicVariableDisplayDatas);
}

#else

m_CachedVariableDisplayDatas.Add(userLogicType, userLogicVariableDisplayDatas);

#endif

this.userLogicVariableDisplayDatas = userLogicVariableDisplayDatas;

return true;
}

Expand Down
21 changes: 18 additions & 3 deletions Packages/uGUI-Skinner/Editor/SkinnerEditorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ private static void FieldClean(SerializedProperty arrayObj, object defaultValue)
switch (arrayObj.propertyType)
{
case SerializedPropertyType.Color:
arrayObj.colorValue = hasDefaultValue ? (Color)defaultValue : SkinDefaultValue.Color;
Color convertedColor = SkinDefaultValue.Color;
if (hasDefaultValue)
{
var type = defaultValue.GetType();
if (type == typeof(Color))
{
convertedColor = (Color)defaultValue;
}
if (type == typeof(Color32))
{
convertedColor = (Color)(Color32)defaultValue;
}
}
arrayObj.colorValue = convertedColor;
break;
case SerializedPropertyType.Float:
arrayObj.floatValue = hasDefaultValue ? (float)defaultValue : SkinDefaultValue.Float;
Expand All @@ -55,6 +68,7 @@ private static void FieldClean(SerializedProperty arrayObj, object defaultValue)
break;
case SerializedPropertyType.Vector4:
Vector4 convertedVector4 = SkinDefaultValue.Vector4;
if (hasDefaultValue)
{
var type = defaultValue.GetType();
if (type == typeof(Vector2))
Expand All @@ -70,10 +84,11 @@ private static void FieldClean(SerializedProperty arrayObj, object defaultValue)
convertedVector4 = (Vector4)defaultValue;
}
}
arrayObj.vector4Value = hasDefaultValue ? convertedVector4 : SkinDefaultValue.Vector4;
arrayObj.vector4Value = convertedVector4;
break;
case SerializedPropertyType.String:
string convertedString = SkinDefaultValue.String;
if (hasDefaultValue)
{
var type = defaultValue.GetType();
if (type == typeof(char))
Expand All @@ -85,7 +100,7 @@ private static void FieldClean(SerializedProperty arrayObj, object defaultValue)
convertedString = (string)defaultValue;
}
}
arrayObj.stringValue = hasDefaultValue ? convertedString : SkinDefaultValue.String;
arrayObj.stringValue = convertedString;
break;
case SerializedPropertyType.ObjectReference:
arrayObj.objectReferenceValue = hasDefaultValue ? (Object)defaultValue : SkinDefaultValue.Object;
Expand Down
141 changes: 141 additions & 0 deletions Packages/uGUI-Skinner/Runtime/SkinnerSystemType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using UnityEngine;
using Type = System.Type;

namespace Pspkurara.UI.Skinner
{

/// <summary>
/// 型チェック等を行う
/// </summary>
public static class SkinnerSystemType
{

#region フィールド型チェック

/// <summary>
/// <see cref="SkinPartsPropertry.objectReferenceValues"/>に入る型かチェックする
/// </summary>
/// <param name="type">型</param>
public static bool IsObjectReferenceValue(Type type)
{
return IsObject(type);
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.colorValues"/>に入る型かチェックする
/// </summary>
/// <param name="type">型</param>
public static bool IsColorValue(Type type)
{
return IsColor(type);
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.boolValues"/>に入る型かチェックする
/// </summary>
/// <param name="type">型</param>
public static bool IsBoolValue(Type type)
{
return IsBoolean(type);
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.intValues"/>に入る型かチェックする
/// </summary>
/// <param name="type">型</param>
public static bool IsIntValue(Type type)
{
return IsInteger(type) || IsEnum(type);
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.floatValues"/>に入る型かチェックする
/// </summary>
/// <param name="type">型</param>
public static bool IsFloatValue(Type type)
{
return IsFloat(type);
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.vector4Values"/>に入る型かチェックする
/// </summary>
/// <param name="type">型</param>
public static bool IsVector4Value(Type type)
{
return IsVector2(type) || IsVector3(type) || IsVector4(type);
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.stringValues"/>に入る型かチェックする
/// </summary>
/// <param name="type">型</param>
public static bool IsStringValue(Type type)
{
return IsChar(type) || IsString(type);
}

#endregion

#region 型チェック

public static bool IsObject(Type type)
{
return type == typeof(Object) || type.IsSubclassOf(typeof(Object));
}

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

public static bool IsInteger(Type type)
{
return type == typeof(int);
}

public static bool IsFloat(Type type)
{
return type == typeof(float);
}

public static bool IsColor(Type type)
{
return type == typeof(Color) || type == typeof(Color32);
}

public static bool IsEnum(Type type)
{
return type.IsEnum;
}

public static bool IsVector2(Type type)
{
return type == typeof(Vector2);
}

public static bool IsVector3(Type type)
{
return type == typeof(Vector3);
}

public static bool IsVector4(Type type)
{
return type == typeof(Vector4);
}

public static bool IsChar(Type type)
{
return type == typeof(char);
}

public static bool IsString(Type type)
{
return type == typeof(string);
}

#endregion

}

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

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

Loading

0 comments on commit d60e4d7

Please sign in to comment.