I.はじめに
配列またはリストを定義します, デフォルトではこのように表示されます. この
表示にはいくつかの欠点があります
- テーブル要素の順序を変更できない
- 要素を追加する場合, サイズの値を大きくする必要がある
- 必要に応じて要素の 1 つを削除すると、非常に面倒です
。
2: 使う
最初に、いくつかのコールバック関数を持つ ReorderableList オブジェクトを作成する必要があります
- drawElementCallback: 各要素を描画するためのコールバック
- drawHeaderCallback: タイトルを描画するためのコールバック -
elementHeightCallback: 各要素の高さを描画するためのコールバック -
onAddCallback: 追加するとき要素 コールバック
- onRemoveCallback: 要素が削除され、
最後に DoLayoutList メソッドが描画のために呼び出されるときのコールバック
3: コードの実装 - 単純なケース
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
using System;
using System.Linq;
public class Test : MonoBehaviour
{
public List<string> list = new List<string>();
}
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
SerializedProperty m_list;
ReorderableList reorderableList;
private void OnEnable()
{
m_list = serializedObject.FindProperty("list");
reorderableList = new ReorderableList(serializedObject, m_list, true, true, true, true);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
reorderableList.drawElementCallback = DrawElement;
reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
void DrawElement(Rect rect, int index, bool isActive, bool isFocused)
{
SerializedProperty element = m_list.GetArrayElementAtIndex(index);
EditorGUI.PropertyField(rect, element);
}
}
4: コードの実装 - 複雑なケース
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
using System;
using System.Linq;
public class Test : MonoBehaviour
{
public List<TestChild> list = new List<TestChild>();
}
[Serializable]
public class TestChild
{
public Sprite sprite;
public string des;
public int index;
}
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
SerializedProperty m_list;
ReorderableList reorderableList;
List<float> m_HeightCache = new List<float>();
private void OnEnable()
{
m_list = serializedObject.FindProperty("list");
reorderableList = new ReorderableList(serializedObject, m_list, true, true, true, true);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
reorderableList.onAddCallback = OnAdd;
reorderableList.onRemoveCallback = OnRemove;
reorderableList.onReorderCallback = OnReorder;
reorderableList.onCanRemoveCallback = OnCanRemove;
reorderableList.drawHeaderCallback = DrawHeader;
reorderableList.drawElementCallback = DrawElement;
reorderableList.onSelectCallback = OnSelect;
reorderableList.onAddDropdownCallback = OnAddDropdown;
reorderableList.elementHeightCallback = DrawElementHeight;
reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
void DrawHeader(Rect rect)
{
EditorGUI.LabelField(rect, "Test List");
}
void DrawElement(Rect rect, int index, bool isActive, bool isFocused)
{
m_HeightCache[index] = isActive ? 100 : 50;
SerializedProperty element = reorderableList.serializedProperty.GetArrayElementAtIndex(index);
SerializedProperty serializedProperty_sprite = element.FindPropertyRelative("sprite");
SerializedProperty serializedProperty_des = element.FindPropertyRelative("des");
SerializedProperty serializedProperty_index = element.FindPropertyRelative("index");
Rect rect_sprite;
Rect rect_des;
Rect rect_index;
if (isActive)
{
rect_sprite = new Rect(rect.x, rect.y, 100, 100);
rect_des = new Rect(rect.x + 120, rect.y, rect.width, 40);
rect_index = new Rect(rect.x + 120, rect.y + 60, rect.width, 40);
}
else
{
rect_sprite = new Rect(rect.x, rect.y, 100, 50);
rect_des = new Rect(rect.x + 120, rect.y, rect.width, 15);
rect_index = new Rect(rect.x + 120, rect.y + 20, rect.width, 15);
}
EditorGUI.PropertyField(rect_sprite, serializedProperty_sprite, GUIContent.none);
EditorGUI.PropertyField(rect_des, serializedProperty_des, GUIContent.none);
EditorGUI.PropertyField(rect_index, serializedProperty_index, GUIContent.none);
}
float DrawElementHeight(int index)
{
if (m_HeightCache.Count > index)
{
return m_HeightCache[index];
}
else
{
m_HeightCache.Add(0);
return m_HeightCache[index];
}
}
void OnAdd(ReorderableList list)
{
if (EditorUtility.DisplayDialog("是否添加", "是否添加", "是", "否"))
{
ReorderableList.defaultBehaviours.DoAddButton(list);
m_HeightCache.Add(0);
}
}
void OnRemove(ReorderableList list)
{
if (EditorUtility.DisplayDialog("是否移除", "是否移除", "是", "否"))
{
ReorderableList.defaultBehaviours.DoRemoveButton(list);
m_HeightCache.RemoveAt(list.count - 1);
}
}
bool OnCanRemove(ReorderableList list)
{
return list.count >= 1;
}
void OnReorder(ReorderableList list)
{
Debug.Log("顺序改变后当前选择元素的index:" + list.index);
}
void OnSelect(ReorderableList list)
{
Debug.Log("当前选择元素的index:" + list.index);
}
void OnAddDropdown(Rect buttonRect, ReorderableList list)
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Add Default"), false, clickHandler,
new TestContextParams() { des = "testDes", index = 1, });
menu.ShowAsContext();
}
private void clickHandler(object target)
{
TestContextParams param = (TestContextParams)target;
var index = m_list.arraySize;
m_list.arraySize++;
var element = m_list.GetArrayElementAtIndex(index);
element.FindPropertyRelative("des").stringValue = param.des;
element.FindPropertyRelative("index").intValue = param.index;
serializedObject.ApplyModifiedProperties();
}
struct TestContextParams
{
public string des;
public int index;
}
}