Unity中实现List类型的自定义GUI(ReorderableList)

本文介绍如何在Unity编辑器中使用ReorderableList来简化集合类型的GUI操作。通过Rotorz.ReorderableList实现,可以轻松地在Inspector面板上编辑List<string>类型的数据,无需复杂的自定义GUI代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

感谢韩同学提供的资源

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值