/* * Uses Factory pattern and polymorphism to return the desired * Sensor type. */ public static Sensors GetInstance(int sensorType, GameObject gObj) { Cube = gObj; Sensors _sensor; switch (sensorType) { case 1: _sensor = new ProximitySensor(); break; case 2: _sensor = new RangeSensor(); break; case 3: _sensor = new LidarSensor(); break; case 4: _sensor = new RadarSensor(); break; case 5: _sensor = new BumperSensor(); break; default: Debug.Log("The chosen sensor doesn't exist!"); _sensor = new ProximitySensor(); break; } sensors = _sensor; return(sensors); }
// the documentation lists the events as: // SensorEventHandler OnDetected (Invoked when a new GameObject is detected) // SensorEventHandler OnLostDetection (Invoked when a GameObject that was detected is no longer detected) // Our 'RangeSensor' object 'rangeSens' has access to these two 'SensorEventHandler' from its parent class, // the events are already invoked at the required time, we just need to add listeners. void Start() { // get the object rangeSens = GetComponent <RangeSensor>(); // listen for 'on lost detection' events // - Method to execute (lostDetection) must match signature of event, in this case a sensor and an object (declared in method below) // - notice the method doesnt pass any explicit parameters, as they are implicit, object comes from object causing event, // - and sensor is 'rangeSens' rangeSens.OnLostDetection.AddListener(lostDetection); //listen for 'on detected' events rangeSens.OnDetected.AddListener(Detection); }