Skip to content

Commit

Permalink
feat: スキンパーツ・各スキンパーツプロパティ ToStringでクラスの中身を出力可能にした
Browse files Browse the repository at this point in the history
  • Loading branch information
pspkurara committed Nov 8, 2020
1 parent 41f1d0b commit 71c3054
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Packages/uGUI-Skinner/Editor/EditorSkinPartsPropertry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEditor;
using System.Text;

namespace Pspkurara.UI.Skinner
{
Expand All @@ -25,6 +26,19 @@ internal override void MapProperties(SerializedProperty property)
base.MapProperties(property);
}

/// <summary>
/// 文字列に変換する
/// クラスの変数の内容を出力する
/// </summary>
/// <returns>自身の中身の文字列</returns>
public override string ToString()
{
StringBuilder builder = new StringBuilder();
SkinnerEditorUtility.AppendSkinPartsPropertyElementString(builder, objectReferenceValues, p => p.objectReferenceValue);
SkinnerUtility.AppendIfStringNotEmpty(builder, base.ToString());
return builder.ToString();
}

}

/// <summary>
Expand Down Expand Up @@ -61,6 +75,20 @@ internal virtual void MapProperties(SerializedProperty property)
stringValues = property.FindPropertyRelative("m_StringValues");
}

/// <summary>
/// 文字列に変換する
/// クラスの変数の内容を出力する
/// </summary>
/// <returns>自身の中身の文字列</returns>
public override string ToString()
{
StringBuilder builder = new StringBuilder();
SkinnerEditorUtility.AppendSkinPartsPropertyElementString(builder, floatValues, p => p.floatValue);
SkinnerEditorUtility.AppendSkinPartsPropertyElementString(builder, vector4Values, p => p.vector4Value);
SkinnerEditorUtility.AppendSkinPartsPropertyElementString(builder, stringValues, p => p.stringValue);
return builder.ToString();
}

}

}
25 changes: 25 additions & 0 deletions Packages/uGUI-Skinner/Editor/SkinnerEditorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,31 @@ private static void MapEditorFromRuntimeSingleProperty<T>(SerializedProperty map
}
}

/// <summary>
/// <see cref="EditorSkinPartsPropertry">を<see cref="object.ToString"/>した際の各要素の文字列を<see cref="StringBuilder"/>に追加する
/// </summary>
/// <typeparam name="T">要素の型</typeparam>
/// <param name="builder">追加対象の<see cref="StringBuilder"></param>
/// <param name="property">プロパティの要素</param>
/// <param name="getValueFunction">シリアライズプロパティから変数を抜き出すメソッド</param>
/// <seealso cref="SkinnerUtility.WriteSkinPartsPropertyElementString"/>
internal static void AppendSkinPartsPropertyElementString<T>(StringBuilder builder, SerializedProperty property, Func<SerializedProperty, T> getValueFunction)
{
if (property != null && property.arraySize > 0)
{
if (builder.Length > 0) builder.AppendLine();
builder.Append(typeof(T).Name);
builder.Append(SkinnerUtility.ToStringSeparater);
for (int i = 0; i < property.arraySize; i++)
{
builder.AppendLine();
T v = getValueFunction(property.GetArrayElementAtIndex(i));
builder.Append(SkinnerUtility.ToStringIndent);
builder.Append(v);
}
}
}

}

}
31 changes: 31 additions & 0 deletions Packages/uGUI-Skinner/Runtime/SkinParts.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using UnityEngine;

namespace Pspkurara.UI.Skinner
Expand Down Expand Up @@ -123,6 +124,36 @@ public bool Equals(SkinParts other)
return m_Property.Equals(other);
}

/// <summary>
/// 文字列に変換する
/// クラスの変数の内容を出力する
/// </summary>
/// <returns>自身の中身の文字列</returns>
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append("Type: ");
var rootType = SkinPartsAccess.GetSkinPartsRootType(m_Type);
if (rootType != null)
{
builder.Append(rootType.Name);
}
else
{
builder.Append(m_Type);
}
builder.AppendLine();

var propertyString = m_Property.ToString();
if (propertyString.Length > 0)
{
builder.AppendLine("Property:");
builder.Append(SkinnerUtility.ToIndentedString(propertyString));
}

return builder.ToString();
}

}

}
28 changes: 28 additions & 0 deletions Packages/uGUI-Skinner/Runtime/SkinPartsProperty.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Pspkurara.UI.Skinner
Expand Down Expand Up @@ -69,6 +70,19 @@ public bool Equals(SkinPartsPropertry other)
return base.Equals(other);
}

/// <summary>
/// 文字列に変換する
/// クラスの変数の内容を出力する
/// </summary>
/// <returns>自身の中身の文字列</returns>
public override string ToString()
{
StringBuilder builder = new StringBuilder();
SkinnerUtility.AppendSkinPartsPropertyElementString(builder, m_ObjectReferenceValues);
SkinnerUtility.AppendIfStringNotEmpty(builder, base.ToString());
return builder.ToString();
}

#endregion

}
Expand Down Expand Up @@ -164,6 +178,20 @@ public bool Equals(SkinPartsPropertryWithoutObjectReference other)
return true;
}

/// <summary>
/// 文字列に変換する
/// クラスの変数の内容を出力する
/// </summary>
/// <returns>自身の中身の文字列</returns>
public override string ToString()
{
StringBuilder builder = new StringBuilder();
SkinnerUtility.AppendSkinPartsPropertyElementString(builder, m_FloatValues);
SkinnerUtility.AppendSkinPartsPropertyElementString(builder, m_Vector4Values);
SkinnerUtility.AppendSkinPartsPropertyElementString(builder, m_StringValues);
return builder.ToString();
}

#endregion

}
Expand Down
74 changes: 74 additions & 0 deletions Packages/uGUI-Skinner/Runtime/SkinnerUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;

namespace Pspkurara.UI.Skinner
{
Expand All @@ -11,6 +12,16 @@ namespace Pspkurara.UI.Skinner
internal static class SkinnerUtility
{

/// <summary>
/// <see cref="object.ToString"/>した際に接頭子として使うインデント
/// </summary>
public const string ToStringIndent = " ";

/// <summary>
/// <see cref="object.ToString"/>した際にフィールド型名の接尾子として使うセパレーター
/// </summary>
public const string ToStringSeparater = ":";

/// <summary>
/// オブジェクトを強制的にリロードする
/// </summary>
Expand Down Expand Up @@ -79,6 +90,69 @@ internal static Dictionary<int, int> CreateVariableIdToIndexDictionary(IEnumerab
return dic;
}

/// <summary>
/// <see cref="SkinPartsPropertry">を<see cref="object.ToString"/>した際の各要素の文字列を<see cref="StringBuilder"/>に追加する
/// </summary>
/// <typeparam name="T">要素の型</typeparam>
/// <param name="builder">追加対象の<see cref="StringBuilder"></param>
/// <param name="property">プロパティの要素</param>
internal static void AppendSkinPartsPropertyElementString<T>(StringBuilder builder, List<T> property)
{
if (property != null && property.Count > 0)
{
if (builder.Length > 0) builder.AppendLine();
builder.Append(typeof(T).Name);
builder.Append(ToStringSeparater);
for (int i = 0; i < property.Count; i++)
{
builder.AppendLine();
var v = property[i];
builder.Append(ToStringIndent);
builder.Append(v);
}
}
}

/// <summary>
/// 引数が空じゃなかったら文字列を<see cref="StringBuilder"/>に追加する
/// </summary>
internal static void AppendIfStringNotEmpty(StringBuilder builder, string value)
{
if (value.Length > 0)
{
if (builder.Length > 0) builder.AppendLine();
builder.Append(value);
}
}

/// <summary>
/// 引数の文字列にインデントを追加して返す
/// </summary>
/// <param name="value">インデントを追加したい文字列</param>
/// <returns>インデント付き文字列</returns>
internal static string ToIndentedString(string value, int indentCount = 1)
{
StringBuilder builder = new StringBuilder();
foreach (var v in value.Split('\n'))
{
if (v.Length > 0)
{
builder.AppendLine();
}
else
{
for (int i = 0; i < indentCount; i++)
{
builder.Append(ToStringIndent);
}
builder.AppendLine(v);
}
}
// 最後の行を削除
builder.Remove(builder.Length - 1, 1);
return builder.ToString();
}

}

}

0 comments on commit 71c3054

Please sign in to comment.