3
3

More than 5 years have passed since last update.

【Unity】ReorderableListを気軽に使う

Last updated at Posted at 2019-04-06

ReorderableListって?

Animatorとかで見かける並べ替えできるやつです。
最近まで名前を知らなかった...
スクリーンショット 2019-04-06 19.57.00.png
基本的にはEditor拡張でEditorWindowやComponentの描画を書き換えて
ReorderableListが使われることが多いイメージ。
ただそれだと毎回自作クラスとかを拡張して書いてあげる必要があって、
Editor拡張わかる人しかゴリゴリ使えない!!

 List<T>使う感覚でサクッと使えればみんな幸せ。

という事で色々参考にしながら作りました。
CustomReorderalbeList

環境

使い方

UnityPackageをインポート。
[ReorderableList]をつけてpublicでL***の型を指定して終わり。
privateでも使えますがInspectorに表示しないならList<T>を使ったほうが気持ちいいです。

Example.cs
// Ex.
[ReorderableList]
public LString m_string;  // string型
[ReorderableList]
public LBool   m_bool;    // bool型

list1.gif
スクリプト内でもListとだいたい同じように使えます。
m_string.Add("new Text");
foreach (var item in m_string) {}

もっと使い方

Systemで使えるfloatbyteなどはL***で使えるようにしてあります。
UnityEngineで使えるのはGameObjectVector3くらいしか設定していません。
ジェネリックのクラスはUnityのInspectorで表示されないようで、
あらかじめ継承したクラスを用意してあげなきゃみたいです。
UnityのComponentを全部用意するのは大変なので必要に応じて追加してください。

各クラスはPackageの中にあるReorderList.csの下の方にあります。

ReorderList.cs
/* 省略 */
// UnityEngine
[System.Serializable] public class LGameObject : ReorderList<GameObject> { }
[System.Serializable] public class LTransform : ReorderList<Transform> { }
[System.Serializable] public class LVector2 : ReorderList<Vector2> { }
[System.Serializable] public class LVector3 : ReorderList<Vector3> { }
// 追加 TextComponentをReorderableListで使えるようにする
[System.Serializable] public class LText : ReorderList<UnityEngine.UI.Text> {}

参考にした記事

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