|
| 1 | +using UnityEditor; |
| 2 | +using UnityEngine; |
| 3 | +using Unity.MLAgents.Editor; |
| 4 | +using Unity.MLAgents.Extensions.Sensors; |
| 5 | + |
| 6 | +namespace Unity.MLAgents.Extensions.Editor |
| 7 | +{ |
| 8 | + [CustomEditor(typeof(GridSensorComponent))] |
| 9 | + [CanEditMultipleObjects] |
| 10 | + internal class GridSensorComponentEditor : UnityEditor.Editor |
| 11 | + { |
| 12 | + public override void OnInspectorGUI() |
| 13 | + { |
| 14 | + var so = serializedObject; |
| 15 | + so.Update(); |
| 16 | + |
| 17 | + // Drawing the GridSensorComponent |
| 18 | + EditorGUI.BeginChangeCheck(); |
| 19 | + |
| 20 | + EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties()); |
| 21 | + { |
| 22 | + // These fields affect the sensor order or observation size, |
| 23 | + // So can't be changed at runtime. |
| 24 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_SensorName)), true); |
| 25 | + |
| 26 | + EditorGUILayout.LabelField("Grid Settings", EditorStyles.boldLabel); |
| 27 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_CellScale)), true); |
| 28 | + // We only supports 2D GridSensor now so display gridNumSide as Vector2 |
| 29 | + var gridNumSide = so.FindProperty(nameof(GridSensorComponent.m_GridNumSide)); |
| 30 | + var gridNumSide2d = new Vector2Int(gridNumSide.vector3IntValue.x, gridNumSide.vector3IntValue.z); |
| 31 | + var newGridNumSide = EditorGUILayout.Vector2IntField("Grid Num Side", gridNumSide2d); |
| 32 | + gridNumSide.vector3IntValue = new Vector3Int(newGridNumSide.x, 1, newGridNumSide.y); |
| 33 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_RootReference)), true); |
| 34 | + } |
| 35 | + EditorGUI.EndDisabledGroup(); |
| 36 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_RotateWithAgent)), true); |
| 37 | + |
| 38 | + EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties()); |
| 39 | + { |
| 40 | + EditorGUILayout.LabelField("Channel Settings", EditorStyles.boldLabel); |
| 41 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_DepthType)), true); |
| 42 | + |
| 43 | + // channel depth |
| 44 | + var channelDepth = so.FindProperty(nameof(GridSensorComponent.m_ChannelDepth)); |
| 45 | + var newDepth = EditorGUILayout.IntField("Channel Depth", channelDepth.arraySize); |
| 46 | + if (newDepth != channelDepth.arraySize) |
| 47 | + { |
| 48 | + channelDepth.arraySize = newDepth; |
| 49 | + } |
| 50 | + EditorGUI.indentLevel++; |
| 51 | + for (var i = 0; i < channelDepth.arraySize; i++) |
| 52 | + { |
| 53 | + var objectTag = channelDepth.GetArrayElementAtIndex(i); |
| 54 | + EditorGUILayout.PropertyField(objectTag, new GUIContent("Channel " + i + " Depth"), true); |
| 55 | + } |
| 56 | + EditorGUI.indentLevel--; |
| 57 | + |
| 58 | + // detectable objects |
| 59 | + var detectableObjects = so.FindProperty(nameof(GridSensorComponent.m_DetectableObjects)); |
| 60 | + var newSize = EditorGUILayout.IntField("Detectable Objects", detectableObjects.arraySize); |
| 61 | + if (newSize != detectableObjects.arraySize) |
| 62 | + { |
| 63 | + detectableObjects.arraySize = newSize; |
| 64 | + } |
| 65 | + EditorGUI.indentLevel++; |
| 66 | + for (var i = 0; i < detectableObjects.arraySize; i++) |
| 67 | + { |
| 68 | + var objectTag = detectableObjects.GetArrayElementAtIndex(i); |
| 69 | + EditorGUILayout.PropertyField(objectTag, new GUIContent("Tag " + i), true); |
| 70 | + } |
| 71 | + EditorGUI.indentLevel--; |
| 72 | + |
| 73 | + EditorGUILayout.LabelField("Collider and Buffer", EditorStyles.boldLabel); |
| 74 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_MaxColliderBufferSize)), true); |
| 75 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_InitialColliderBufferSize)), true); |
| 76 | + } |
| 77 | + EditorGUI.EndDisabledGroup(); |
| 78 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_ObserveMask)), true); |
| 79 | + EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties()); |
| 80 | + { |
| 81 | + EditorGUILayout.LabelField("Sensor Settings", EditorStyles.boldLabel); |
| 82 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_ObservationStacks)), true); |
| 83 | + } |
| 84 | + EditorGUI.EndDisabledGroup(); |
| 85 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_CompressionType)), true); |
| 86 | + |
| 87 | + EditorGUILayout.LabelField("Debug Gizmo", EditorStyles.boldLabel); |
| 88 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_ShowGizmos)), true); |
| 89 | + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_GizmoYOffset)), true); |
| 90 | + |
| 91 | + // detectable objects |
| 92 | + var debugColors = so.FindProperty(nameof(GridSensorComponent.m_DebugColors)); |
| 93 | + var detectableObjectSize = so.FindProperty(nameof(GridSensorComponent.m_DetectableObjects)).arraySize; |
| 94 | + if (detectableObjectSize != debugColors.arraySize) |
| 95 | + { |
| 96 | + debugColors.arraySize = detectableObjectSize; |
| 97 | + } |
| 98 | + EditorGUI.indentLevel++; |
| 99 | + for (var i = 0; i < debugColors.arraySize; i++) |
| 100 | + { |
| 101 | + var debugColor = debugColors.GetArrayElementAtIndex(i); |
| 102 | + EditorGUILayout.PropertyField(debugColor, new GUIContent("Tag " + i + " Color"), true); |
| 103 | + } |
| 104 | + EditorGUI.indentLevel--; |
| 105 | + |
| 106 | + var requireSensorUpdate = EditorGUI.EndChangeCheck(); |
| 107 | + so.ApplyModifiedProperties(); |
| 108 | + |
| 109 | + if (requireSensorUpdate) |
| 110 | + { |
| 111 | + UpdateSensor(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + void UpdateSensor() |
| 116 | + { |
| 117 | + var sensorComponent = serializedObject.targetObject as GridSensorComponent; |
| 118 | + sensorComponent?.UpdateSensor(); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments