LoginSignup
3
0

More than 3 years have passed since last update.

Unity 2020.1 で ReorderableList が作りやすくなったらしい……?

Posted at

みんな大好き、ReorderableList

カッコいいエディタ拡張の代名詞たるReorderableList、好きですよね。しかし実際に使おうとすると、ReorderableListはUnityEditorInternalに入っている関係上、マニュアルはおろか、リファレンスすらありません。幸いすでにたくさんの方がその使い方を記事にされており、私もそれらを参考に実装していました。それでもなおその使い方はかなり煩雑で、だいぶめんどくさかったのです。

しかし先日Unity 2020.1のリファレンスを眺めていたところ、以下のような項目が。
Unity - Scripting API: UIElements.ListView.reorderable
なんとUI ElementsのほうのListViewにreorderableというプロパティが実装されていました。プロパティいっこ設定するだけでReorderableにできるというのです。これは試さなくては!

意気揚々と実装してみた結果が以下になります。

g.gif

えっなんか違う……
reorderableってそういう……?

そんなにカッコよくはないけどまあ便利そうなので

以下、今回のコードです。

SampleList.cs
using System;
using UnityEngine;

public class SampleList : MonoBehaviour
{
    public SampleListItem[] items;
}

[Serializable]
public class SampleListItem
{
    public string key;
    public string value;
}

SampleListEditor.cs
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

[CustomEditor(typeof(SampleList))]
public class SampleListEditor : Editor
{
    public override VisualElement CreateInspectorGUI()
    {
        var root = new VisualElement();

        var list = new ListView();
        list.bindingPath = "items";
        list.reorderable = true;
        list.itemHeight = 70;

        list.style.flexGrow = 1;

        root.Add(list);
        root.Bind(serializedObject);

        root.style.height = 200;

        return root;
    }
}

まあ実装は楽だし入れ替えは便利だし……

3
0
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
0