天天看點

Unity中實作List類型的自定義GUI(ReorderableList)

感謝韓同學提供的資源

Unity本身提供了float,int,vector3..等類型字段的gui接口,而對于集合類型一般要自己硬寫。

官方提供了一個List的自定義GUI,但使用起來非常複雜

UnityEditorInternal.ReorderableList使用:

http://www.cnblogs.com/hont/p/5458021.html

另外有一個開源的ReorderableList實作,Rotorz.ReorderableList

這個用起來非常簡單

MonoBehaviour:

public class ReorderableTest : MonoBehaviour
{
    public List<string> names = new List<string>();
}      

Editor:

[CustomEditor(typeof(ReorderableTest))]
public class ReorderableTestInspector : Editor
{
    SerializedProperty mNamesProp;


    private void OnEnable()
    {
        mNamesProp = serializedObject.FindProperty("names");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        ReorderableListGUI.Title("names");
        ReorderableListGUI.ListField(mNamesProp);

        serializedObject.ApplyModifiedProperties();
    }
}      

最終效果:

Unity中實作List類型的自定義GUI(ReorderableList)

隻需把序列化字段名稱傳給Rotorz.ReorderableList就可以在GUI顯示了。

下載下傳位址:

https://bitbucket.org/rotorz/reorderable-list-editor-field-for-unity