93 lines
3.3 KiB
C#
93 lines
3.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEditor;
|
||
using UnityEditorInternal;
|
||
|
||
[CustomEditor(typeof(Robot))]
|
||
public class RobotEditor : Editor
|
||
{
|
||
private Robot robot;
|
||
private ReorderableList _motionList;
|
||
SerializedProperty CenterOfMass;
|
||
SerializedProperty isPlaying;
|
||
SerializedProperty HVBUSSERVO_Speed;
|
||
SerializedProperty HVBUSSERVOs;
|
||
SerializedProperty hvbdata;
|
||
|
||
private void OnEnable()
|
||
{
|
||
CenterOfMass = serializedObject.FindProperty("CenterOfMass");
|
||
isPlaying = serializedObject.FindProperty("isPlaying");
|
||
HVBUSSERVO_Speed = serializedObject.FindProperty("HVBUSSERVO_Speed");
|
||
HVBUSSERVOs = serializedObject.FindProperty("HVBUSSERVOs");
|
||
hvbdata = serializedObject.FindProperty("hvbdata");
|
||
|
||
robot = (Robot)target;
|
||
|
||
//创建ReorderableList 参数:
|
||
//arg1:序列化物体,arg2:序列化数据,arg3:可否拖动,arg4:是否显示标题,arg5:是否显示添加按钮,arg6:是否显示添加按钮
|
||
_motionList = new ReorderableList(serializedObject, serializedObject.FindProperty("motions")
|
||
, true, true, true, true);
|
||
|
||
//自定义列表名称
|
||
_motionList.drawHeaderCallback = (Rect rect) =>
|
||
{
|
||
GUI.Label(rect, "动作元列表");
|
||
};
|
||
|
||
//定义元素的高度
|
||
_motionList.elementHeight = 190;
|
||
|
||
//_motionList.elementHeightCallback = (int index) =>
|
||
//{
|
||
// SerializedProperty element = _motionList.serializedProperty.GetArrayElementAtIndex(index);
|
||
|
||
// SerializedProperty namesProp = element.FindPropertyRelative("");
|
||
|
||
// return EditorGUI.GetPropertyHeight(namesProp) + EditorGUIUtility.standardVerticalSpacing;
|
||
//};
|
||
|
||
//自定义绘制列表元素
|
||
_motionList.drawElementCallback = (Rect rect, int index, bool selected, bool focused) =>
|
||
{
|
||
//根据index获取对应元素
|
||
SerializedProperty item = _motionList.serializedProperty.GetArrayElementAtIndex(index);
|
||
rect.height = 18;
|
||
rect.y += 2;
|
||
EditorGUI.PropertyField(rect, item, new GUIContent("动作_" + (index + 1) + " :"));
|
||
};
|
||
|
||
//当删除元素时候的回调函数,实现删除元素时,有提示框跳出
|
||
_motionList.onRemoveCallback = (ReorderableList list) =>
|
||
{
|
||
if (EditorUtility.DisplayDialog("Warnning", "Do you want to remove this element?", "Remove", "Cancel"))
|
||
{
|
||
ReorderableList.defaultBehaviours.DoRemoveButton(list);
|
||
}
|
||
};
|
||
}
|
||
|
||
|
||
public override void OnInspectorGUI()
|
||
{
|
||
//更新
|
||
serializedObject.Update();
|
||
|
||
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
|
||
EditorGUILayout.PropertyField(CenterOfMass, new GUIContent("观测重心:"));
|
||
EditorGUILayout.PropertyField(isPlaying, new GUIContent("isPlaying:"));
|
||
//EditorGUILayout.PropertyField(HVBUSSERVO_Speed, new GUIContent("电机速度:"));
|
||
EditorGUILayout.PropertyField(hvbdata, new GUIContent("测试数据:"));
|
||
EditorGUI.BeginDisabledGroup(true);
|
||
EditorGUILayout.PropertyField(HVBUSSERVOs, new GUIContent("电机列表:"));
|
||
EditorGUI.EndDisabledGroup();
|
||
EditorGUILayout.EndVertical();
|
||
//自动布局绘制列表
|
||
_motionList.DoLayoutList();
|
||
//应用
|
||
serializedObject.ApplyModifiedProperties();
|
||
}
|
||
|
||
}
|