Skip to content

Commit 7764863

Browse files
author
Chris Elion
authored
[change] public SensorComponent fields to properties, add custom editor (#3564)
1 parent d55c0b2 commit 7764863

13 files changed

+276
-53
lines changed

com.unity.ml-agents/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3535
- The method `GetStepCount()` on the Agent class has been replaced with the property getter `StepCount`
3636
- `RayPerceptionSensorComponent` and related classes now display the debug gizmos whenever the Agent is selected (not just Play mode).
3737
- Most fields on `RayPerceptionSensorComponent` can now be changed while the editor is in Play mode. The exceptions to this are fields that affect the number of observations.
38+
- Most fields on `CameraSensorComponent` and `RenderTextureSensorComponent` were changed to private and replaced by properties with the same name.
3839
- Unused static methods from the `Utilities` class (ShiftLeft, ReplaceRange, AddRangeNoAlloc, and GetSensorFloatObservationSize) were removed.
3940
- The `Agent` class is no longer abstract.
4041
- SensorBase was moved out of the package and into the Examples directory.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using MLAgents.Sensors;
4+
5+
namespace MLAgents.Editor
6+
{
7+
[CustomEditor(typeof(CameraSensorComponent))]
8+
[CanEditMultipleObjects]
9+
internal class CameraSensorComponentEditor : UnityEditor.Editor
10+
{
11+
public override void OnInspectorGUI()
12+
{
13+
var so = serializedObject;
14+
so.Update();
15+
16+
// Drawing the CameraSensorComponent
17+
EditorGUI.BeginChangeCheck();
18+
19+
EditorGUILayout.PropertyField(so.FindProperty("m_Camera"), true);
20+
EditorGUI.BeginDisabledGroup(Application.isPlaying);
21+
{
22+
// These fields affect the sensor order or observation size,
23+
// So can't be changed at runtime.
24+
EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true);
25+
EditorGUILayout.PropertyField(so.FindProperty("m_Width"), true);
26+
EditorGUILayout.PropertyField(so.FindProperty("m_Height"), true);
27+
EditorGUILayout.PropertyField(so.FindProperty("m_Grayscale"), true);
28+
}
29+
EditorGUI.EndDisabledGroup();
30+
EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true);
31+
32+
var requireSensorUpdate = EditorGUI.EndChangeCheck();
33+
so.ApplyModifiedProperties();
34+
35+
if (requireSensorUpdate)
36+
{
37+
UpdateSensor();
38+
}
39+
}
40+
41+
void UpdateSensor()
42+
{
43+
var sensorComponent = serializedObject.targetObject as CameraSensorComponent;
44+
sensorComponent?.UpdateSensor();
45+
}
46+
47+
}
48+
}

com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ protected void OnRayPerceptionInspectorGUI(bool is3d)
1717
EditorGUI.BeginChangeCheck();
1818
EditorGUI.indentLevel++;
1919

20-
EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true);
21-
22-
// Because the number of rays and the tags affect the observation shape,
23-
// they are not editable during play mode.
20+
// Don't allow certain fields to be modified during play mode.
21+
// * SensorName affects the ordering of the Agent's observations
22+
// * The number of tags and rays affects the size of the observations.
2423
EditorGUI.BeginDisabledGroup(Application.isPlaying);
2524
{
25+
EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true);
2626
EditorGUILayout.PropertyField(so.FindProperty("m_DetectableTags"), true);
2727
EditorGUILayout.PropertyField(so.FindProperty("m_RaysPerDirection"), true);
2828
}
@@ -56,8 +56,8 @@ protected void OnRayPerceptionInspectorGUI(bool is3d)
5656
m_RequireSensorUpdate = true;
5757
}
5858

59-
UpdateSensorIfDirty();
6059
so.ApplyModifiedProperties();
60+
UpdateSensorIfDirty();
6161
}
6262

6363

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using MLAgents.Sensors;
4+
namespace MLAgents.Editor
5+
{
6+
[CustomEditor(typeof(RenderTextureSensorComponent))]
7+
[CanEditMultipleObjects]
8+
internal class RenderTextureSensorComponentEditor : UnityEditor.Editor
9+
{
10+
public override void OnInspectorGUI()
11+
{
12+
var so = serializedObject;
13+
so.Update();
14+
15+
// Drawing the RenderTextureComponent
16+
EditorGUI.BeginChangeCheck();
17+
18+
EditorGUI.BeginDisabledGroup(Application.isPlaying);
19+
{
20+
EditorGUILayout.PropertyField(so.FindProperty("m_RenderTexture"), true);
21+
EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true);
22+
EditorGUILayout.PropertyField(so.FindProperty("m_Grayscale"), true);
23+
}
24+
EditorGUI.EndDisabledGroup();
25+
26+
EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true);
27+
28+
var requireSensorUpdate = EditorGUI.EndChangeCheck();
29+
so.ApplyModifiedProperties();
30+
31+
if (requireSensorUpdate)
32+
{
33+
UpdateSensor();
34+
}
35+
}
36+
37+
void UpdateSensor()
38+
{
39+
var sensorComponent = serializedObject.targetObject as RenderTextureSensorComponent;
40+
sensorComponent?.UpdateSensor();
41+
}
42+
}
43+
}

com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ internal class BehaviorParameters : MonoBehaviour
5757
/// <summary>
5858
/// The team ID for this behavior.
5959
/// </summary>
60-
[HideInInspector]
61-
[SerializeField]
62-
[FormerlySerializedAs("m_TeamID")]
60+
[HideInInspector, SerializeField, FormerlySerializedAs("m_TeamID")]
6361
public int TeamId;
6462

6563
[FormerlySerializedAs("m_useChildSensors")]

com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ public class CameraSensor : ISensor
1515
int[] m_Shape;
1616
SensorCompressionType m_CompressionType;
1717

18+
/// <summary>
19+
/// The Camera used for rendering the sensor observations.
20+
/// </summary>
21+
public Camera camera
22+
{
23+
get { return m_Camera; }
24+
set { m_Camera = value; }
25+
}
26+
27+
/// <summary>
28+
/// The compression type used by the sensor.
29+
/// </summary>
30+
public SensorCompressionType compressionType
31+
{
32+
get { return m_CompressionType; }
33+
set { m_CompressionType = value; }
34+
}
35+
36+
1837
/// <summary>
1938
/// Creates and returns the camera sensor.
2039
/// </summary>
Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using UnityEngine.Serialization;
23

34
namespace MLAgents.Sensors
45
{
@@ -8,43 +9,88 @@ namespace MLAgents.Sensors
89
[AddComponentMenu("ML Agents/Camera Sensor", (int)MenuGroup.Sensors)]
910
public class CameraSensorComponent : SensorComponent
1011
{
12+
[HideInInspector, SerializeField, FormerlySerializedAs("camera")]
13+
Camera m_Camera;
14+
15+
CameraSensor m_Sensor;
16+
1117
/// <summary>
1218
/// Camera object that provides the data to the sensor.
1319
/// </summary>
14-
public new Camera camera;
20+
public new Camera camera
21+
{
22+
get { return m_Camera; }
23+
set { m_Camera = value; UpdateSensor(); }
24+
}
25+
26+
[HideInInspector, SerializeField, FormerlySerializedAs("sensorName")]
27+
string m_SensorName = "CameraSensor";
1528

1629
/// <summary>
1730
/// Name of the generated <see cref="CameraSensor"/> object.
1831
/// </summary>
19-
public string sensorName = "CameraSensor";
32+
public string sensorName
33+
{
34+
get { return m_SensorName; }
35+
internal set { m_SensorName = value; }
36+
}
37+
38+
[HideInInspector, SerializeField, FormerlySerializedAs("width")]
39+
int m_Width = 84;
2040

2141
/// <summary>
22-
/// Width of the generated image.
42+
/// Width of the generated observation.
2343
/// </summary>
24-
public int width = 84;
44+
public int width
45+
{
46+
get { return m_Width; }
47+
internal set { m_Width = value; }
48+
}
49+
50+
[HideInInspector, SerializeField, FormerlySerializedAs("height")]
51+
int m_Height = 84;
2552

2653
/// <summary>
27-
/// Height of the generated image.
54+
/// Height of the generated observation.
2855
/// </summary>
29-
public int height = 84;
56+
public int height
57+
{
58+
get { return m_Height; }
59+
internal set { m_Height = value; }
60+
}
61+
62+
[HideInInspector, SerializeField, FormerlySerializedAs("grayscale")]
63+
public bool m_Grayscale;
3064

3165
/// <summary>
3266
/// Whether to generate grayscale images or color.
3367
/// </summary>
34-
public bool grayscale;
68+
public bool grayscale
69+
{
70+
get { return m_Grayscale; }
71+
internal set { m_Grayscale = value; }
72+
}
73+
74+
[HideInInspector, SerializeField, FormerlySerializedAs("compression")]
75+
SensorCompressionType m_Compression = SensorCompressionType.PNG;
3576

3677
/// <summary>
3778
/// The compression type to use for the sensor.
3879
/// </summary>
39-
public SensorCompressionType compression = SensorCompressionType.PNG;
80+
public SensorCompressionType compression
81+
{
82+
get { return m_Compression; }
83+
set { m_Compression = value; UpdateSensor(); }
84+
}
4085

4186
/// <summary>
4287
/// Creates the <see cref="CameraSensor"/>
4388
/// </summary>
4489
/// <returns>The created <see cref="CameraSensor"/> object for this component.</returns>
4590
public override ISensor CreateSensor()
4691
{
47-
return new CameraSensor(camera, width, height, grayscale, sensorName, compression);
92+
m_Sensor = new CameraSensor(m_Camera, m_Width, m_Height, grayscale, m_SensorName, compression);
93+
return m_Sensor;
4894
}
4995

5096
/// <summary>
@@ -53,7 +99,19 @@ public override ISensor CreateSensor()
5399
/// <returns>The observation shape of the associated <see cref="CameraSensor"/> object.</returns>
54100
public override int[] GetObservationShape()
55101
{
56-
return CameraSensor.GenerateShape(width, height, grayscale);
102+
return CameraSensor.GenerateShape(m_Width, m_Height, grayscale);
103+
}
104+
105+
/// <summary>
106+
/// Update fields that are safe to change on the Sensor at runtime.
107+
/// </summary>
108+
internal void UpdateSensor()
109+
{
110+
if (m_Sensor != null)
111+
{
112+
m_Sensor.camera = m_Camera;
113+
m_Sensor.compressionType = m_Compression;
114+
}
57115
}
58116
}
59117
}

com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponent3D.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ namespace MLAgents.Sensors
99
[AddComponentMenu("ML Agents/Ray Perception Sensor 3D", (int)MenuGroup.Sensors)]
1010
public class RayPerceptionSensorComponent3D : RayPerceptionSensorComponentBase
1111
{
12-
[HideInInspector]
13-
[SerializeField]
14-
[FormerlySerializedAs("startVerticalOffset")]
12+
[HideInInspector, SerializeField, FormerlySerializedAs("startVerticalOffset")]
1513
[Range(-10f, 10f)]
1614
[Tooltip("Ray start is offset up or down by this amount.")]
1715
float m_StartVerticalOffset;
@@ -25,9 +23,7 @@ public float startVerticalOffset
2523
set { m_StartVerticalOffset = value; UpdateSensor(); }
2624
}
2725

28-
[HideInInspector]
29-
[SerializeField]
30-
[FormerlySerializedAs("endVerticalOffset")]
26+
[HideInInspector, SerializeField, FormerlySerializedAs("endVerticalOffset")]
3127
[Range(-10f, 10f)]
3228
[Tooltip("Ray end is offset up or down by this amount.")]
3329
float m_EndVerticalOffset;

0 commit comments

Comments
 (0)