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();
    }
}

最终效果:

只需把序列化字段名称传给Rotorz.ReorderableList就可以在GUI显示了。

下载地址:

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

原文地址:https://www.cnblogs.com/hont/p/5458035.html