[UNITY]2DRPG開発日誌 #15 リストの要素(Element)に名前を付ける

リストの要素(Element)に名前を付ける

調べれば何でも解決する。先輩達に感謝…。

Inspectorの配列のElementに固定の名前つける

「この記事は最終更新日から3年以上が経過しています。」というアラートが出てますが全然問題ありませんでした。

手順は次のとおり。

1.Editorフォルダを作成(これを作らないとUNITYの中身を書き換えられない)

2.そのフォルダ内にC#スクリプトを作成、「NamedArrayDrawer」と名付けて、中身は次のコードをコピペ。

using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(NamedArrayAttribute))]
public class NamedArrayDrawer : PropertyDrawer
{
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
try
{
int pos = int.Parse(property.propertyPath.Split('[', ']')[1]);
EditorGUI.PropertyField(rect, property, new GUIContent(((NamedArrayAttribute)attribute).names[pos]));
}
catch
{
EditorGUI.PropertyField(rect, property, label);
}
}
}/* Your code... */

3.どこでもいいのでC#スクリプトを作成、「NamedArrayAttribute」と名付けて、中身は次のコードをコピペ。

using UnityEngine;

public class NamedArrayAttribute : PropertyAttribute
{
public readonly string[] names;
public NamedArrayAttribute(string[] names) { this.names = names; }
}

4.リストを作るときに、次のように書く

[NamedArrayAttribute(new string[] { "Element0の名前", "Element1の名前","なんでもいれてOK","どんだけ入れてもOK"})]
public リストの型[] リスト名;

//または

[NamedArrayAttribute(new string[] { "Element0の名前", "Element1の名前","なんでもいれてOK","どんだけ入れてもOK"})]
public リストの型[] リスト名 = new リストの型[];

完成です。

解決!

UNITY
スポンサーリンク
Share
Follow Us
KITTYPOOL