您的位置:首页 > 移动开发 > Unity3D

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

2016-05-04 12:57 866 查看
感谢韩同学提供的资源

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: