From df6280df2ad3285b990c1dbc8050a83f80c2db0a Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 4 Mar 2020 14:52:55 -0800 Subject: [PATCH 1/4] public fields to properties, add custom editor --- .../Editor/CameraSensorComponentEditor.cs | 39 ++++++++++ .../RayPerceptionSensorComponentBaseEditor.cs | 8 +- .../RenderTextureSensorComponentEditor.cs | 36 +++++++++ .../Runtime/Sensors/CameraSensorComponent.cs | 75 ++++++++++++++++--- .../Sensors/RenderTextureSensorComponent.cs | 47 ++++++++++-- 5 files changed, 186 insertions(+), 19 deletions(-) create mode 100644 com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs create mode 100644 com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs diff --git a/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs b/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs new file mode 100644 index 0000000000..3dbfbd12a6 --- /dev/null +++ b/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs @@ -0,0 +1,39 @@ +using UnityEngine; +using UnityEditor; +using MLAgents.Sensors; + +namespace MLAgents.Editor +{ + [CustomEditor(typeof(CameraSensorComponent))] + [CanEditMultipleObjects] + internal class CameraSensorComponentEditor : UnityEditor.Editor + { + public override void OnInspectorGUI() + { + var so = serializedObject; + so.Update(); + + // Drawing the CameraSensorComponent + EditorGUI.BeginChangeCheck(); + + EditorGUI.BeginDisabledGroup(Application.isPlaying); + { + EditorGUILayout.PropertyField(so.FindProperty("m_Camera"), true); + EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true); + EditorGUILayout.PropertyField(so.FindProperty("m_Width"), true); + EditorGUILayout.PropertyField(so.FindProperty("m_Height"), true); + EditorGUILayout.PropertyField(so.FindProperty("m_Grayscale"), true); + EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true); + } + + EditorGUI.EndDisabledGroup(); + + if (EditorGUI.EndChangeCheck()) + { + // + } + + so.ApplyModifiedProperties(); + } + } +} diff --git a/com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs b/com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs index ae96288f41..6d1373c2ab 100644 --- a/com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs +++ b/com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs @@ -17,12 +17,12 @@ protected void OnRayPerceptionInspectorGUI(bool is3d) EditorGUI.BeginChangeCheck(); EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true); - - // Because the number of rays and the tags affect the observation shape, - // they are not editable during play mode. + // Don't allow certain fields to be modified during play mode. + // * SensorName affects the ordering of the Agent's observations + // * The number of tags and rays affects the size of the observations. EditorGUI.BeginDisabledGroup(Application.isPlaying); { + EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true); EditorGUILayout.PropertyField(so.FindProperty("m_DetectableTags"), true); EditorGUILayout.PropertyField(so.FindProperty("m_RaysPerDirection"), true); } diff --git a/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs b/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs new file mode 100644 index 0000000000..8b69b009b6 --- /dev/null +++ b/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs @@ -0,0 +1,36 @@ +using UnityEngine; +using UnityEditor; +using MLAgents.Sensors; +namespace MLAgents.Editor +{ + [CustomEditor(typeof(RenderTextureSensorComponent))] + [CanEditMultipleObjects] + internal class RenderTextureSensorComponentEditor : UnityEditor.Editor + { + public override void OnInspectorGUI() + { + var so = serializedObject; + so.Update(); + + // Drawing the RenderTextureComponent + EditorGUI.BeginChangeCheck(); + + EditorGUI.BeginDisabledGroup(Application.isPlaying); + { + EditorGUILayout.PropertyField(so.FindProperty("m_RenderTexture"), true); + EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true); + EditorGUILayout.PropertyField(so.FindProperty("m_Grayscale"), true); + EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true); + } + + EditorGUI.EndDisabledGroup(); + + if (EditorGUI.EndChangeCheck()) + { + // + } + + so.ApplyModifiedProperties(); + } + } +} diff --git a/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs b/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs index 2cde34be5d..6965f6a237 100644 --- a/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs +++ b/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs @@ -1,4 +1,5 @@ using UnityEngine; +using UnityEngine.Serialization; namespace MLAgents.Sensors { @@ -8,35 +9,89 @@ namespace MLAgents.Sensors [AddComponentMenu("ML Agents/Camera Sensor", (int)MenuGroup.Sensors)] public class CameraSensorComponent : SensorComponent { + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("camera")] + Camera m_Camera; + /// /// Camera object that provides the data to the sensor. /// - public new Camera camera; + public new Camera camera + { + get { return m_Camera; } + set { m_Camera = value; } + } + + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("sensorName")] + string m_SensorName = "CameraSensor"; /// /// Name of the generated object. /// - public string sensorName = "CameraSensor"; + public string sensorName + { + get { return m_SensorName; } + internal set { m_SensorName = value; } + } + + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("width")] + int m_Width = 84; /// - /// Width of the generated image. + /// Width of the generated observation. /// - public int width = 84; + public int width + { + get { return m_Width; } + internal set { m_Width = value; } + } + + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("height")] + int m_Height = 84; /// - /// Height of the generated image. + /// Height of the generated observation. /// - public int height = 84; + public int height + { + get { return m_Height; } + internal set { m_Height = value; } + } + + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("grayscale")] + public bool m_Grayscale; /// /// Whether to generate grayscale images or color. /// - public bool grayscale; + public bool grayscale + { + get { return m_Grayscale; } + internal set { m_Grayscale = value; } + } + + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("compression")] + SensorCompressionType m_Compression = SensorCompressionType.PNG; /// /// The compression type to use for the sensor. /// - public SensorCompressionType compression = SensorCompressionType.PNG; + public SensorCompressionType compression + { + get { return m_Compression; } + set { m_Compression = value; } + } /// /// Creates the @@ -44,7 +99,7 @@ public class CameraSensorComponent : SensorComponent /// The created object for this component. public override ISensor CreateSensor() { - return new CameraSensor(camera, width, height, grayscale, sensorName, compression); + return new CameraSensor(m_Camera, m_Width, m_Height, grayscale, m_SensorName, compression); } /// @@ -53,7 +108,7 @@ public override ISensor CreateSensor() /// The observation shape of the associated object. public override int[] GetObservationShape() { - return CameraSensor.GenerateShape(width, height, grayscale); + return CameraSensor.GenerateShape(m_Width, m_Height, grayscale); } } } diff --git a/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs b/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs index 18132dc86d..c031501838 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs @@ -1,4 +1,5 @@ using UnityEngine; +using UnityEngine.Serialization; namespace MLAgents.Sensors { @@ -12,22 +13,58 @@ public class RenderTextureSensorComponent : SensorComponent /// The instance that the associated /// wraps. /// - public RenderTexture renderTexture; + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("renderTexture")] + RenderTexture m_RenderTexture; + + public RenderTexture renderTexture + { + get { return m_RenderTexture; } + set { m_RenderTexture = value; } + } + + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("sensorName")] + string m_SensorName = "RenderTextureSensor"; /// - /// Name of the sensor. + /// Name of the generated . /// - public string sensorName = "RenderTextureSensor"; + public string sensorName + { + get { return m_SensorName; } + internal set { m_SensorName = value; } + } + + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("grayscale")] + public bool m_Grayscale; /// /// Whether the RenderTexture observation should be converted to grayscale or not. /// - public bool grayscale; + public bool grayscale + { + get { return m_Grayscale; } + internal set { m_Grayscale = value; } + } + + [HideInInspector] + [SerializeField] + [FormerlySerializedAs("compression")] + SensorCompressionType m_Compression = SensorCompressionType.PNG; /// /// Compression type for the render texture observation. /// - public SensorCompressionType compression = SensorCompressionType.PNG; + public SensorCompressionType compression + { + get { return m_Compression; } + set { m_Compression = value; } + } /// public override ISensor CreateSensor() From f30a029aa7d79a9980d5f891731447cd4f31100b Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 4 Mar 2020 15:37:19 -0800 Subject: [PATCH 2/4] changelog and meta files --- com.unity.ml-agents/CHANGELOG.md | 1 + .../Editor/CameraSensorComponentEditor.cs.meta | 11 +++++++++++ .../Editor/RenderTextureSensorComponentEditor.cs.meta | 11 +++++++++++ 3 files changed, 23 insertions(+) create mode 100644 com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs.meta create mode 100644 com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs.meta diff --git a/com.unity.ml-agents/CHANGELOG.md b/com.unity.ml-agents/CHANGELOG.md index bc39d05215..271d637ee3 100755 --- a/com.unity.ml-agents/CHANGELOG.md +++ b/com.unity.ml-agents/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - The method `GetStepCount()` on the Agent class has been replaced with the property getter `StepCount` - `RayPerceptionSensorComponent` and related classes now display the debug gizmos whenever the Agent is selected (not just Play mode). - 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. + - Most fields on `CameraSensorComponent` and `RenderTextureSensorComponent` were changed to private and replaced by properties with the same name. - Unused static methods from the `Utilities` class (ShiftLeft, ReplaceRange, AddRangeNoAlloc, and GetSensorFloatObservationSize) were removed. - The `Agent` class is no longer abstract. - SensorBase was moved out of the package and into the Examples directory. diff --git a/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs.meta b/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs.meta new file mode 100644 index 0000000000..70b1e31432 --- /dev/null +++ b/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fdda773c024894cf0ae47d1b1396c38d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs.meta b/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs.meta new file mode 100644 index 0000000000..fd7a57d05e --- /dev/null +++ b/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dab309e01d2964f0792de3ef914ca6b9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From c3c21db82c26383f9ebabacaec2ed0af9b335cfd Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 4 Mar 2020 16:15:20 -0800 Subject: [PATCH 3/4] attributes on same line --- .../Runtime/Policies/BehaviorParameters.cs | 4 +-- .../Runtime/Sensors/CameraSensorComponent.cs | 24 ++++---------- .../Sensors/RayPerceptionSensorComponent3D.cs | 8 ++--- .../RayPerceptionSensorComponentBase.cs | 31 +++++-------------- .../Sensors/RenderTextureSensorComponent.cs | 16 +++------- 5 files changed, 21 insertions(+), 62 deletions(-) diff --git a/com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs b/com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs index d9b763982e..515b7d8384 100644 --- a/com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs +++ b/com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs @@ -45,9 +45,7 @@ enum BehaviorType /// /// The team ID for this behavior. /// - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("m_TeamID")] + [HideInInspector, SerializeField, FormerlySerializedAs("m_TeamID")] public int TeamId; [FormerlySerializedAs("m_useChildSensors")] diff --git a/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs b/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs index 6965f6a237..c01477bb6a 100644 --- a/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs +++ b/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs @@ -9,9 +9,7 @@ namespace MLAgents.Sensors [AddComponentMenu("ML Agents/Camera Sensor", (int)MenuGroup.Sensors)] public class CameraSensorComponent : SensorComponent { - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("camera")] + [HideInInspector, SerializeField, FormerlySerializedAs("camera")] Camera m_Camera; /// @@ -23,9 +21,7 @@ public class CameraSensorComponent : SensorComponent set { m_Camera = value; } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("sensorName")] + [HideInInspector, SerializeField, FormerlySerializedAs("sensorName")] string m_SensorName = "CameraSensor"; /// @@ -37,9 +33,7 @@ public string sensorName internal set { m_SensorName = value; } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("width")] + [HideInInspector, SerializeField, FormerlySerializedAs("width")] int m_Width = 84; /// @@ -51,9 +45,7 @@ public int width internal set { m_Width = value; } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("height")] + [HideInInspector, SerializeField, FormerlySerializedAs("height")] int m_Height = 84; /// @@ -65,9 +57,7 @@ public int height internal set { m_Height = value; } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("grayscale")] + [HideInInspector, SerializeField, FormerlySerializedAs("grayscale")] public bool m_Grayscale; /// @@ -79,9 +69,7 @@ public bool grayscale internal set { m_Grayscale = value; } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("compression")] + [HideInInspector, SerializeField, FormerlySerializedAs("compression")] SensorCompressionType m_Compression = SensorCompressionType.PNG; /// diff --git a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponent3D.cs b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponent3D.cs index a4e444c42d..ff806982aa 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponent3D.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponent3D.cs @@ -9,9 +9,7 @@ namespace MLAgents.Sensors [AddComponentMenu("ML Agents/Ray Perception Sensor 3D", (int)MenuGroup.Sensors)] public class RayPerceptionSensorComponent3D : RayPerceptionSensorComponentBase { - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("startVerticalOffset")] + [HideInInspector, SerializeField, FormerlySerializedAs("startVerticalOffset")] [Range(-10f, 10f)] [Tooltip("Ray start is offset up or down by this amount.")] float m_StartVerticalOffset; @@ -25,9 +23,7 @@ public float startVerticalOffset set { m_StartVerticalOffset = value; UpdateSensor(); } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("endVerticalOffset")] + [HideInInspector, SerializeField, FormerlySerializedAs("endVerticalOffset")] [Range(-10f, 10f)] [Tooltip("Ray end is offset up or down by this amount.")] float m_EndVerticalOffset; diff --git a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs index 9226fe1fb5..cf07af35d6 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs @@ -10,9 +10,7 @@ namespace MLAgents.Sensors /// public abstract class RayPerceptionSensorComponentBase : SensorComponent { - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("sensorName")] + [HideInInspector, SerializeField, FormerlySerializedAs("sensorName")] string m_SensorName = "RayPerceptionSensor"; /// @@ -25,8 +23,7 @@ public string sensorName internal set => m_SensorName = value; } - [SerializeField] - [FormerlySerializedAs("detectableTags")] + [SerializeField, FormerlySerializedAs("detectableTags")] [Tooltip("List of tags in the scene to compare against.")] List m_DetectableTags; @@ -40,9 +37,7 @@ public List detectableTags internal set => m_DetectableTags = value; } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("raysPerDirection")] + [HideInInspector, SerializeField, FormerlySerializedAs("raysPerDirection")] [Range(0, 50)] [Tooltip("Number of rays to the left and right of center.")] int m_RaysPerDirection = 3; @@ -57,9 +52,7 @@ public int raysPerDirection internal set => m_RaysPerDirection = value; } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("maxRayDegrees")] + [HideInInspector, SerializeField, FormerlySerializedAs("maxRayDegrees")] [Range(0, 180)] [Tooltip("Cone size for rays. Using 90 degrees will cast rays to the left and right. " + "Greater than 90 degrees will go backwards.")] @@ -75,9 +68,7 @@ public float maxRayDegrees set { m_MaxRayDegrees = value; UpdateSensor(); } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("sphereCastRadius")] + [HideInInspector, SerializeField, FormerlySerializedAs("sphereCastRadius")] [Range(0f, 10f)] [Tooltip("Radius of sphere to cast. Set to zero for raycasts.")] float m_SphereCastRadius = 0.5f; @@ -91,9 +82,7 @@ public float sphereCastRadius set { m_SphereCastRadius = value; UpdateSensor(); } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("rayLength")] + [HideInInspector, SerializeField, FormerlySerializedAs("rayLength")] [Range(1, 1000)] [Tooltip("Length of the rays to cast.")] float m_RayLength = 20f; @@ -107,9 +96,7 @@ public float rayLength set { m_RayLength = value; UpdateSensor(); } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("rayLayerMask")] + [HideInInspector, SerializeField, FormerlySerializedAs("rayLayerMask")] [Tooltip("Controls which layers the rays can hit.")] LayerMask m_RayLayerMask = Physics.DefaultRaycastLayers; @@ -122,9 +109,7 @@ public LayerMask rayLayerMask set { m_RayLayerMask = value; UpdateSensor();} } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("observationStacks")] + [HideInInspector, SerializeField, FormerlySerializedAs("observationStacks")] [Range(1, 50)] [Tooltip("Whether to stack previous observations. Using 1 means no previous observations.")] int m_ObservationStacks = 1; diff --git a/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs b/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs index c031501838..e1ebb70ce0 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs @@ -13,9 +13,7 @@ public class RenderTextureSensorComponent : SensorComponent /// The instance that the associated /// wraps. /// - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("renderTexture")] + [HideInInspector, SerializeField, FormerlySerializedAs("renderTexture")] RenderTexture m_RenderTexture; public RenderTexture renderTexture @@ -24,9 +22,7 @@ public RenderTexture renderTexture set { m_RenderTexture = value; } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("sensorName")] + [HideInInspector, SerializeField, FormerlySerializedAs("sensorName")] string m_SensorName = "RenderTextureSensor"; /// @@ -38,9 +34,7 @@ public string sensorName internal set { m_SensorName = value; } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("grayscale")] + [HideInInspector, SerializeField, FormerlySerializedAs("grayscale")] public bool m_Grayscale; /// @@ -52,9 +46,7 @@ public bool grayscale internal set { m_Grayscale = value; } } - [HideInInspector] - [SerializeField] - [FormerlySerializedAs("compression")] + [HideInInspector, SerializeField, FormerlySerializedAs("compression")] SensorCompressionType m_Compression = SensorCompressionType.PNG; /// From 915274db0fce7a193f90b07de5f93c6ab4c39788 Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 4 Mar 2020 16:48:32 -0800 Subject: [PATCH 4/4] allow updating some RenderTexture/CameraComponent fields at runtime --- .../Editor/CameraSensorComponentEditor.cs | 21 +++++++++++++------ .../RayPerceptionSensorComponentBaseEditor.cs | 2 +- .../RenderTextureSensorComponentEditor.cs | 17 ++++++++++----- .../Runtime/Sensors/CameraSensor.cs | 19 +++++++++++++++++ .../Runtime/Sensors/CameraSensorComponent.cs | 21 ++++++++++++++++--- .../Runtime/Sensors/RenderTextureSensor.cs | 10 +++++++++ .../Sensors/RenderTextureSensorComponent.cs | 18 ++++++++++++++-- 7 files changed, 91 insertions(+), 17 deletions(-) diff --git a/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs b/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs index 3dbfbd12a6..3c8578743c 100644 --- a/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs +++ b/com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs @@ -16,24 +16,33 @@ public override void OnInspectorGUI() // Drawing the CameraSensorComponent EditorGUI.BeginChangeCheck(); + EditorGUILayout.PropertyField(so.FindProperty("m_Camera"), true); EditorGUI.BeginDisabledGroup(Application.isPlaying); { - EditorGUILayout.PropertyField(so.FindProperty("m_Camera"), true); + // These fields affect the sensor order or observation size, + // So can't be changed at runtime. EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true); EditorGUILayout.PropertyField(so.FindProperty("m_Width"), true); EditorGUILayout.PropertyField(so.FindProperty("m_Height"), true); EditorGUILayout.PropertyField(so.FindProperty("m_Grayscale"), true); - EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true); } - EditorGUI.EndDisabledGroup(); + EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true); + + var requireSensorUpdate = EditorGUI.EndChangeCheck(); + so.ApplyModifiedProperties(); - if (EditorGUI.EndChangeCheck()) + if (requireSensorUpdate) { - // + UpdateSensor(); } + } - so.ApplyModifiedProperties(); + void UpdateSensor() + { + var sensorComponent = serializedObject.targetObject as CameraSensorComponent; + sensorComponent?.UpdateSensor(); } + } } diff --git a/com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs b/com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs index 6d1373c2ab..3d90eb1398 100644 --- a/com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs +++ b/com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs @@ -56,8 +56,8 @@ protected void OnRayPerceptionInspectorGUI(bool is3d) m_RequireSensorUpdate = true; } - UpdateSensorIfDirty(); so.ApplyModifiedProperties(); + UpdateSensorIfDirty(); } diff --git a/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs b/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs index 8b69b009b6..d452b740ae 100644 --- a/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs +++ b/com.unity.ml-agents/Editor/RenderTextureSensorComponentEditor.cs @@ -20,17 +20,24 @@ public override void OnInspectorGUI() EditorGUILayout.PropertyField(so.FindProperty("m_RenderTexture"), true); EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true); EditorGUILayout.PropertyField(so.FindProperty("m_Grayscale"), true); - EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true); } - EditorGUI.EndDisabledGroup(); - if (EditorGUI.EndChangeCheck()) + EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true); + + var requireSensorUpdate = EditorGUI.EndChangeCheck(); + so.ApplyModifiedProperties(); + + if (requireSensorUpdate) { - // + UpdateSensor(); } + } - so.ApplyModifiedProperties(); + void UpdateSensor() + { + var sensorComponent = serializedObject.targetObject as RenderTextureSensorComponent; + sensorComponent?.UpdateSensor(); } } } diff --git a/com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs b/com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs index faedf4998d..b9b750b2a8 100644 --- a/com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs +++ b/com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs @@ -15,6 +15,25 @@ public class CameraSensor : ISensor int[] m_Shape; SensorCompressionType m_CompressionType; + /// + /// The Camera used for rendering the sensor observations. + /// + public Camera camera + { + get { return m_Camera; } + set { m_Camera = value; } + } + + /// + /// The compression type used by the sensor. + /// + public SensorCompressionType compressionType + { + get { return m_CompressionType; } + set { m_CompressionType = value; } + } + + /// /// Creates and returns the camera sensor. /// diff --git a/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs b/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs index c01477bb6a..0125c6d5e5 100644 --- a/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs +++ b/com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs @@ -12,13 +12,15 @@ public class CameraSensorComponent : SensorComponent [HideInInspector, SerializeField, FormerlySerializedAs("camera")] Camera m_Camera; + CameraSensor m_Sensor; + /// /// Camera object that provides the data to the sensor. /// public new Camera camera { get { return m_Camera; } - set { m_Camera = value; } + set { m_Camera = value; UpdateSensor(); } } [HideInInspector, SerializeField, FormerlySerializedAs("sensorName")] @@ -78,7 +80,7 @@ public bool grayscale public SensorCompressionType compression { get { return m_Compression; } - set { m_Compression = value; } + set { m_Compression = value; UpdateSensor(); } } /// @@ -87,7 +89,8 @@ public SensorCompressionType compression /// The created object for this component. public override ISensor CreateSensor() { - return new CameraSensor(m_Camera, m_Width, m_Height, grayscale, m_SensorName, compression); + m_Sensor = new CameraSensor(m_Camera, m_Width, m_Height, grayscale, m_SensorName, compression); + return m_Sensor; } /// @@ -98,5 +101,17 @@ public override int[] GetObservationShape() { return CameraSensor.GenerateShape(m_Width, m_Height, grayscale); } + + /// + /// Update fields that are safe to change on the Sensor at runtime. + /// + internal void UpdateSensor() + { + if (m_Sensor != null) + { + m_Sensor.camera = m_Camera; + m_Sensor.compressionType = m_Compression; + } + } } } diff --git a/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs b/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs index 42a207f0f5..a906c8978b 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs @@ -13,6 +13,16 @@ public class RenderTextureSensor : ISensor int[] m_Shape; SensorCompressionType m_CompressionType; + /// + /// The compression type used by the sensor. + /// + public SensorCompressionType compressionType + { + get { return m_CompressionType; } + set { m_CompressionType = value; } + } + + /// /// Initializes the sensor. /// diff --git a/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs b/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs index e1ebb70ce0..6c18844fe8 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs @@ -9,6 +9,8 @@ namespace MLAgents.Sensors [AddComponentMenu("ML Agents/Render Texture Sensor", (int)MenuGroup.Sensors)] public class RenderTextureSensorComponent : SensorComponent { + RenderTextureSensor m_Sensor; + /// /// The instance that the associated /// wraps. @@ -55,13 +57,14 @@ public bool grayscale public SensorCompressionType compression { get { return m_Compression; } - set { m_Compression = value; } + set { m_Compression = value; UpdateSensor(); } } /// public override ISensor CreateSensor() { - return new RenderTextureSensor(renderTexture, grayscale, sensorName, compression); + m_Sensor = new RenderTextureSensor(renderTexture, grayscale, sensorName, compression); + return m_Sensor; } /// @@ -72,5 +75,16 @@ public override int[] GetObservationShape() return new[] { height, width, grayscale ? 1 : 3 }; } + + /// + /// Update fields that are safe to change on the Sensor at runtime. + /// + internal void UpdateSensor() + { + if (m_Sensor != null) + { + m_Sensor.compressionType = m_Compression; + } + } } }