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.

PlayerBrainEditor.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Linq;
  6. namespace MLAgents
  7. {
  8. /// <summary>
  9. /// CustomEditor for the PlayerBrain class. Defines the default Inspector view for a
  10. /// PlayerBrain.
  11. /// Shows the BrainParameters of the Brain and expose a tool to deep copy BrainParameters
  12. /// between brains. Also exposes the key mappings for either continuous or discrete control
  13. /// depending on the Vector Action Space Type of the Brain Parameter. These mappings are the
  14. /// ones that will be used by the PlayerBrain.
  15. /// </summary>
  16. [CustomEditor(typeof(PlayerBrain))]
  17. public class PlayerBrainEditor : BrainEditor
  18. {
  19. private const string KeyContinuousPropName = "keyContinuousPlayerActions";
  20. private const string KeyDiscretePropName = "discretePlayerActions";
  21. private const string AxisContinuousPropName = "axisContinuousPlayerActions";
  22. public override void OnInspectorGUI()
  23. {
  24. EditorGUILayout.LabelField("Player Brain", EditorStyles.boldLabel);
  25. var brain = (PlayerBrain) target;
  26. var serializedBrain = serializedObject;
  27. base.OnInspectorGUI();
  28. serializedBrain.Update();
  29. if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
  30. {
  31. DrawContinuousKeyMapping(serializedBrain, brain);
  32. }
  33. else
  34. {
  35. DrawDiscreteKeyMapping(serializedBrain);
  36. }
  37. serializedBrain.ApplyModifiedProperties();
  38. }
  39. /// <summary>
  40. /// Draws the UI for continuous control key mapping to actions.
  41. /// </summary>
  42. /// <param name="serializedBrain"> The SerializedObject corresponding to the brain. </param>
  43. /// <param name="brain"> The Brain of which properties are displayed. </param>
  44. private static void DrawContinuousKeyMapping(
  45. SerializedObject serializedBrain, PlayerBrain brain)
  46. {
  47. GUILayout.Label("Edit the continuous inputs for your actions", EditorStyles.boldLabel);
  48. var keyActionsProp = serializedBrain.FindProperty(KeyContinuousPropName);
  49. var axisActionsProp = serializedBrain.FindProperty(AxisContinuousPropName);
  50. EditorGUILayout.PropertyField(keyActionsProp , true);
  51. EditorGUILayout.PropertyField(axisActionsProp, true);
  52. var keyContinuous = brain.keyContinuousPlayerActions;
  53. var axisContinuous = brain.axisContinuousPlayerActions;
  54. var brainParams = brain.brainParameters;
  55. if (keyContinuous == null)
  56. {
  57. keyContinuous = new PlayerBrain.KeyContinuousPlayerAction[0];
  58. }
  59. if (axisContinuous == null)
  60. {
  61. axisContinuous = new PlayerBrain.AxisContinuousPlayerAction[0];
  62. }
  63. foreach (var action in keyContinuous)
  64. {
  65. if (action.index >= brainParams.vectorActionSize[0])
  66. {
  67. EditorGUILayout.HelpBox(
  68. $"Key {action.key.ToString()} is assigned to index " +
  69. $"{action.index.ToString()} but the action size is only of size " +
  70. $"{brainParams.vectorActionSize.ToString()}",
  71. MessageType.Error);
  72. }
  73. }
  74. foreach (var action in axisContinuous)
  75. {
  76. if (action.index >= brainParams.vectorActionSize[0])
  77. {
  78. EditorGUILayout.HelpBox(
  79. $"Axis {action.axis} is assigned to index {action.index.ToString()} " +
  80. $"but the action size is only of size {brainParams.vectorActionSize}",
  81. MessageType.Error);
  82. }
  83. }
  84. GUILayout.Label("You can change axis settings from Edit->Project Settings->Input",
  85. EditorStyles.helpBox );
  86. }
  87. /// <summary>
  88. /// Draws the UI for discrete control key mapping to actions.
  89. /// </summary>
  90. /// <param name="serializedBrain"> The SerializedObject corresponding to the brain. </param>
  91. private static void DrawDiscreteKeyMapping(SerializedObject serializedBrain)
  92. {
  93. GUILayout.Label("Edit the discrete inputs for your actions",
  94. EditorStyles.boldLabel);
  95. var dhas = serializedBrain.FindProperty(KeyDiscretePropName);
  96. serializedBrain.Update();
  97. EditorGUILayout.PropertyField(dhas, true);
  98. }
  99. }
  100. }