using System; using Unity.MLAgents.Sensors; namespace Unity.MLAgentsExamples { /// /// A simple example of a SensorComponent. /// This should be added to the same GameObject as the BasicController /// public class BasicSensorComponent : SensorComponent { public BasicController basicController; /// /// Creates a BasicSensor. /// /// public override ISensor CreateSensor() { return new BasicSensor(basicController); } /// public override int[] GetObservationShape() { return new[] { BasicController.k_Extents }; } } /// /// Simple Sensor implementation that uses a one-hot encoding of the Agent's /// position as the observation. /// public class BasicSensor : SensorBase { public BasicController basicController; public BasicSensor(BasicController controller) { basicController = controller; } /// /// Generate the observations for the sensor. /// In this case, the observations are all 0 except for a 1 at the position of the agent. /// /// public override void WriteObservation(float[] output) { // One-hot encoding of the position Array.Clear(output, 0, output.Length); output[basicController.position] = 1; } /// public override int[] GetObservationShape() { return new[] { BasicController.k_Extents }; } /// public override string GetName() { return "Basic"; } } }