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.

ResetParameterDrawer.cs 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Linq;
  5. using UnityEditor.SceneManagement;
  6. namespace MLAgents
  7. {
  8. /// <summary>
  9. /// PropertyDrawer for ResetParameters. Defines how ResetParameters are displayed in the
  10. /// Inspector.
  11. /// </summary>
  12. [CustomPropertyDrawer(typeof(ResetParameters))]
  13. public class ResetParameterDrawer : PropertyDrawer
  14. {
  15. private ResetParameters _parameters;
  16. // The height of a line in the Unity Inspectors
  17. private const float LineHeight = 17f;
  18. // This is the prefix for the key when you add a reset parameter
  19. private const string NewKeyPrefix = "Param-";
  20. /// <summary>
  21. /// Computes the height of the Drawer depending on the property it is showing
  22. /// </summary>
  23. /// <param name="property">The property that is being drawn.</param>
  24. /// <param name="label">The label of the property being drawn.</param>
  25. /// <returns>The vertical space needed to draw the property.</returns>
  26. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  27. {
  28. LazyInitializeParameters(property, label);
  29. return (_parameters.Count + 2) * LineHeight;
  30. }
  31. /// <inheritdoc />
  32. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  33. {
  34. LazyInitializeParameters(property, label);
  35. position.height = LineHeight;
  36. EditorGUI.LabelField(position, label);
  37. position.y += LineHeight;
  38. var width = position.width / 2 - 24;
  39. var keyRect = new Rect(position.x + 20, position.y, width, position.height);
  40. var valueRect = new Rect(position.x + width + 30, position.y, width, position.height);
  41. DrawAddRemoveButtons(keyRect, valueRect);
  42. EditorGUI.BeginProperty(position, label, property);
  43. foreach (var parameter in _parameters)
  44. {
  45. var key = parameter.Key;
  46. var value = parameter.Value;
  47. keyRect.y += LineHeight;
  48. valueRect.y += LineHeight;
  49. EditorGUI.BeginChangeCheck();
  50. var newKey = EditorGUI.TextField(keyRect, key);
  51. if (EditorGUI.EndChangeCheck())
  52. {
  53. MarkSceneAsDirty();
  54. try
  55. {
  56. _parameters.Remove(key);
  57. _parameters.Add(newKey, value);
  58. }
  59. catch (Exception e)
  60. {
  61. Debug.Log(e.Message);
  62. }
  63. break;
  64. }
  65. EditorGUI.BeginChangeCheck();
  66. value = EditorGUI.FloatField(valueRect, value);
  67. if (EditorGUI.EndChangeCheck())
  68. {
  69. MarkSceneAsDirty();
  70. _parameters[key] = value;
  71. break;
  72. }
  73. }
  74. EditorGUI.EndProperty();
  75. }
  76. /// <summary>
  77. /// Draws the Add and Remove buttons.
  78. /// </summary>
  79. /// <param name="addRect">The rectangle for the Add New button.</param>
  80. /// <param name="removeRect">The rectangle for the Remove Last button.</param>
  81. private void DrawAddRemoveButtons(Rect addRect, Rect removeRect)
  82. {
  83. // This is the Add button
  84. if (_parameters.Count == 0)
  85. {
  86. addRect.width *= 2;
  87. }
  88. if (GUI.Button(addRect,
  89. new GUIContent("Add New", "Add a new item to the default reset parameters"),
  90. EditorStyles.miniButton))
  91. {
  92. MarkSceneAsDirty();
  93. AddParameter();
  94. }
  95. // If there are no items in the ResetParameters, Hide the Remove button
  96. if (_parameters.Count == 0)
  97. {
  98. return;
  99. }
  100. // This is the Remove button
  101. if (GUI.Button(removeRect,
  102. new GUIContent(
  103. "Remove Last", "Remove the last item from the default reset parameters"),
  104. EditorStyles.miniButton))
  105. {
  106. MarkSceneAsDirty();
  107. RemoveLastParameter();
  108. }
  109. }
  110. /// <summary>
  111. /// Signals that the property has been modified and requires the scene to be saved for
  112. /// the changes to persist. Only works when the Editor is not playing.
  113. /// </summary>
  114. private static void MarkSceneAsDirty()
  115. {
  116. if (!EditorApplication.isPlaying)
  117. {
  118. EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  119. }
  120. }
  121. /// <summary>
  122. /// Ensures that the state of the Drawer is synchronized with the property.
  123. /// </summary>
  124. /// <param name="property">The SerializedProperty of the ResetParameters
  125. /// to make the custom GUI for.</param>
  126. /// <param name="label">The label of this property.</param>
  127. private void LazyInitializeParameters(SerializedProperty property, GUIContent label)
  128. {
  129. if (_parameters != null)
  130. {
  131. return;
  132. }
  133. var target = property.serializedObject.targetObject;
  134. _parameters = fieldInfo.GetValue(target) as ResetParameters;
  135. if (_parameters == null)
  136. {
  137. _parameters = new ResetParameters();
  138. fieldInfo.SetValue(target, _parameters);
  139. }
  140. }
  141. /// <summary>
  142. /// Removes the last ResetParameter from the ResetParameters
  143. /// </summary>
  144. private void RemoveLastParameter()
  145. {
  146. if (_parameters.Count > 0)
  147. {
  148. string key = _parameters.Keys.ToList()[_parameters.Count - 1];
  149. _parameters.Remove(key);
  150. }
  151. }
  152. /// <summary>
  153. /// Adds a new ResetParameter to the ResetParameters with a default name.
  154. /// </summary>
  155. private void AddParameter()
  156. {
  157. string key = NewKeyPrefix + _parameters.Count;
  158. var value = default(float);
  159. try
  160. {
  161. _parameters.Add(key, value);
  162. }
  163. catch (Exception e)
  164. {
  165. Debug.Log(e.Message);
  166. }
  167. }
  168. }
  169. }