LoginSignup
3
2

More than 3 years have passed since last update.

【Unity(C#)】便利なReorderableListをスニペット機能でより便利に

Last updated at Posted at 2019-04-16

ReorderableList

ReorderableListを教えてもらったとき感動したのを覚えています。

その名の通り順番を自由に並びかえられるリストをInspector上に作ることができます。
ReorderableList.png
Element をドラッグして他のElementの位置に移動させると、
リストの番号も自動で入れ替わってくれます。

サンプル

TestReorder
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

public class TestReorder : MonoBehaviour
{
    [SerializeField,HideInInspector]
    public GameObject[] test_Obj;

#if UNITY_EDITOR
    [CustomEditor(typeof(TestReorder))]
    public class ExampleInspector : Editor
    {
        ReorderableList reorderableList;

        void OnEnable()
        {
            SerializedProperty prop = serializedObject.FindProperty("test_Obj");

            reorderableList = new ReorderableList(serializedObject, prop);

            reorderableList.drawElementCallback = (rect, index, isActive, isFocused) =>
            {
                SerializedProperty element = prop.GetArrayElementAtIndex(index);
                rect.height -= 4;
                rect.y += 2;
                EditorGUI.PropertyField(rect, element);
            };
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            serializedObject.Update();
            reorderableList.DoLayoutList();
            serializedObject.ApplyModifiedProperties();
        }
    }
#endif
}

  
  
[HideInInspector]で二重にプロパティが表示されるのを防いでます。

 [SerializeField]
 [HideInInspector]
 public GameObject[] test_Obj;

無いとこうなります。
ReorderableList_miss.png


2019/04/23 追記

Header,Elementの文字列変更

ReorderableListのSerialized Property(Header) ,Element
任意の文字列に変更する方法の記事があまり無かったのでメモしておきます。

SerializedProperty(Header)を変更

reorderableList.drawHeaderCallback = (rect) =>
                     EditorGUI.LabelField(rect, "リストのたいとる");
Elementを変更
EditorGUI.PropertyField(rect, element,new GUIContent("お好きな要素名" + index));

スニペット

とても便利なReorderableListですが、愚直に思い出して書くには少々長いです。
なのでVisual Stadioのスニペット機能を利用してその欠点を補いましょう。

rol.png

たった4文字で呼び出せました。最高。


2019/10/23 追記
Snippet Designerを使用した場合、
$ $で囲んだ箇所がスニペット呼び出し時に自動でフォーカスされ、呼び出し後の編集が楽ちんになります。

既存のスニペットを編集する際は、VSの左上のファイル開くファイルで編集したいスニペットを開けばOKです。


2019/05/12 追記

ReorderableListとPropertyDrawerを組み合わせると便利だよ~という記事を書きました。
ReorderableListでパラメータをひとまとめに

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2