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.

HeuristicBrainEditor.cs 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace MLAgents
  4. {
  5. /// <summary>
  6. /// CustomEditor for the Heuristic Brain class. Defines the default Inspector view for a
  7. /// HeuristicBrain.
  8. /// Shows the BrainParameters of the Brain and expose a tool to deep copy BrainParameters
  9. /// between brains. Provides a drag box for a Decision Monoscript that will be used by
  10. /// the Heuristic Brain.
  11. /// </summary>
  12. [CustomEditor(typeof(HeuristicBrain))]
  13. public class HeuristicBrainEditor : BrainEditor
  14. {
  15. public override void OnInspectorGUI()
  16. {
  17. EditorGUILayout.LabelField("Heuristic Brain", EditorStyles.boldLabel);
  18. var brain = (HeuristicBrain) target;
  19. base.OnInspectorGUI();
  20. // Expose the Heuristic Brain's Monoscript for decision in a drag and drop box.
  21. brain.decisionScript = EditorGUILayout.ObjectField(
  22. "Decision Script", brain.decisionScript, typeof(MonoScript), true) as MonoScript;
  23. CheckIsDecision(brain);
  24. // Draw an error box if the Decision is not set.
  25. if (brain.decisionScript == null)
  26. {
  27. EditorGUILayout.HelpBox("You need to add a 'Decision' component to this Object",
  28. MessageType.Error);
  29. }
  30. }
  31. /// <summary>
  32. /// Ensures tht the Monoscript for the decision of the HeuristicBrain is either null or
  33. /// an implementation of Decision. If the Monoscript is not an implementation of
  34. /// Decision, it will be set to null.
  35. /// </summary>
  36. /// <param name="brain">The HeuristicBrain with the decision script attached</param>
  37. private static void CheckIsDecision(HeuristicBrain brain)
  38. {
  39. if (brain.decisionScript != null)
  40. {
  41. var decisionInstance = (CreateInstance(brain.decisionScript.name) as Decision);
  42. if (decisionInstance == null)
  43. {
  44. Debug.LogError(
  45. "Instance of " + brain.decisionScript.name + " couldn't be created. " +
  46. "The the script class needs to derive from Decision.");
  47. brain.decisionScript = null;
  48. }
  49. }
  50. }
  51. }
  52. }