You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DemonstrationDrawer.cs 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Text;
  2. using MLAgents;
  3. using UnityEditor;
  4. /// <summary>
  5. /// Renders a custom UI for Demonstration Scriptable Object.
  6. /// </summary>
  7. [CustomEditor(typeof(Demonstration))]
  8. [CanEditMultipleObjects]
  9. public class DemonstrationEditor : Editor
  10. {
  11. SerializedProperty brainParameters;
  12. SerializedProperty demoMetaData;
  13. void OnEnable()
  14. {
  15. brainParameters = serializedObject.FindProperty("brainParameters");
  16. demoMetaData = serializedObject.FindProperty("metaData");
  17. }
  18. /// <summary>
  19. /// Renders Inspector UI for Demonstration metadata.
  20. /// </summary>
  21. void MakeMetaDataProperty(SerializedProperty property)
  22. {
  23. var nameProp = property.FindPropertyRelative("demonstrationName");
  24. var expProp = property.FindPropertyRelative("numberExperiences");
  25. var epiProp = property.FindPropertyRelative("numberEpisodes");
  26. var rewProp = property.FindPropertyRelative("meanReward");
  27. var nameLabel = nameProp.displayName + ": " + nameProp.stringValue;
  28. var expLabel = expProp.displayName + ": " + expProp.intValue;
  29. var epiLabel = epiProp.displayName + ": " + epiProp.intValue;
  30. var rewLabel = rewProp.displayName + ": " + rewProp.floatValue;
  31. EditorGUILayout.LabelField(nameLabel);
  32. EditorGUILayout.LabelField(expLabel);
  33. EditorGUILayout.LabelField(epiLabel);
  34. EditorGUILayout.LabelField(rewLabel);
  35. }
  36. /// <summary>
  37. /// Constructs label for action size array.
  38. /// </summary>
  39. static string BuildActionArrayLabel(SerializedProperty actionSizeProperty)
  40. {
  41. var actionSize = actionSizeProperty.arraySize;
  42. StringBuilder actionLabel = new StringBuilder("[ ");
  43. for (int i = 0; i < actionSize; i++)
  44. {
  45. actionLabel.Append(actionSizeProperty.GetArrayElementAtIndex(i).intValue);
  46. if (i < actionSize - 1)
  47. {
  48. actionLabel.Append(", ");
  49. }
  50. }
  51. actionLabel.Append(" ]");
  52. return actionLabel.ToString();
  53. }
  54. /// <summary>
  55. /// Renders Inspector UI for Brain Parameters of Demonstration.
  56. /// </summary>
  57. void MakeBrainParametersProperty(SerializedProperty property)
  58. {
  59. var vecObsSizeProp = property.FindPropertyRelative("vectorObservationSize");
  60. var numStackedProp = property.FindPropertyRelative("numStackedVectorObservations");
  61. var actSizeProperty = property.FindPropertyRelative("vectorActionSize");
  62. var camResProp = property.FindPropertyRelative("cameraResolutions");
  63. var actSpaceTypeProp = property.FindPropertyRelative("vectorActionSpaceType");
  64. var vecObsSizeLabel = vecObsSizeProp.displayName + ": " + vecObsSizeProp.intValue;
  65. var numStackedLabel = numStackedProp.displayName + ": " + numStackedProp.intValue;
  66. var vecActSizeLabel = actSizeProperty.displayName + ": " + BuildActionArrayLabel(actSizeProperty);
  67. var camResLabel = camResProp.displayName + ": " + camResProp.arraySize;
  68. var actSpaceTypeLabel = actSpaceTypeProp.displayName + ": " + (SpaceType) actSpaceTypeProp.enumValueIndex;
  69. EditorGUILayout.LabelField(vecObsSizeLabel);
  70. EditorGUILayout.LabelField(numStackedLabel);
  71. EditorGUILayout.LabelField(vecActSizeLabel);
  72. EditorGUILayout.LabelField(camResLabel);
  73. EditorGUILayout.LabelField(actSpaceTypeLabel);
  74. }
  75. public override void OnInspectorGUI()
  76. {
  77. serializedObject.Update();
  78. EditorGUILayout.LabelField("Meta Data", EditorStyles.boldLabel);
  79. MakeMetaDataProperty(demoMetaData);
  80. EditorGUILayout.LabelField("Brain Parameters", EditorStyles.boldLabel);
  81. MakeBrainParametersProperty(brainParameters);
  82. serializedObject.ApplyModifiedProperties();
  83. }
  84. }