Module 4: Using an Object Manager to Subscribe to Multiple Instances

This module builds directly on Module 3: Subscribing to a Single Topic. You will need to complete that module before performing this module. In this module, we will expand on our visualization by adding an object manager within Unity that will create Game Objects within the simulation, based on DDS instances.

Step 1: Creating an Object Manager

Before, we were handling the movement of our cube within the GameManager itself. Now we are going to create a separate script that will handle reading the Square topic and generating and updating cubes keyed to the color of the square.

  1. Open the ShapeManager script. We will put most of the functionality of the object manager into this generic class, with the specific shape managers directly inheriting from it.
    1. Remove all the code from within the class. We will not be using the Update and Start functions within this class.
    2. Add the following members to the class.

public GameObject m_ShapePrefab;

protected ShapeComms m_DDS;
private Dictionary<Color, GameObject> m_Shapes;

c. Add a protected Awake function to the class. This function will be called as soon as the class instantiates.

protected virtual void Awake()
        m_Shapes = new Dictionary<Color, GameObject>();
        enabled = false;

d. Add a public Setup function to the class. We will use this function to link the DDS class to the object manager and enable the class.

public void Setup(ShapeComms comms)
        m_DDS = comms;

        enabled = true;

e. Add a private Add Instance function to the class.

private void AddInstance(Shape shape)
    m_Shapes.Add(shape.color, Instantiate(m_ShapePrefab, new Vector3(shape.x, shape.y), new Quaternion()));
    m_Shapes[shape.color].GetComponent<MeshRenderer>().material.color = shape.color;
    m_Shapes[shape.color].transform.localScale = new Vector3(shape.shapesize, shape.shapesize, shape.shapesize);

f. Add a private Move Shape function to the class.

private void MoveShape (Shape shape)
        m_Shapes[shape.color].GetComponent<Rigidbody>().MovePosition(new Vector3(shape.x, shape.y));

g. Finally, add a protected Update Shape function to the class. This will be the function called by derived classes to create and update objects handled by our managers.

protected void UpdateShape(List<Shape> shapes)
        foreach (var shape in shapes)
            if (m_Shapes.ContainsKey(shape.color))

h. Save and close the file.

2. Open the CubeManager script. This will need to be changed to a derived instance of the ShapeManager class.

    1. Change the inheritance of the class from MonoBehavior to ShapeManager.
    2. Delete the Start function.
    3. Add the following Awake function to call the base Awake function.

protected override void Awake()

d. Add the following code to the Update function.


e. Save and close the file.

3. Now we need to set up the Cube Manager within the Unity environment.

    1. In Unity, add an empty GameObject to the Hierarchy by right-clicking in the Hierarchy and selecting Create Empty. Change its name to CubeManager.
    2. Drag the CubeManager script into the CubeManager GameObject.
    3. Drag the Cube object from the Prefabs folder into the Shape Prefab box in the Inspector subpanel.
    4. Save the scene.

4. Now that the cube manager is created and configured, we need to integrate it into the Game Manager.

    1. Replace the Cube Prefab variable with a reference to the CubeManager GameObject.

public GameObject m_CubeManager;

b. Delete the private m_Cube member, you won’t need it anymore.

c. Delete the Update function. Updates will be handled within the Object Manager(s).

d. Remove the instantiation code from the Start function. We will instead be configuring the Cube Manager and letting it handle everything. The Start function should look like this:

void Start()
    m_DDS = new ShapeComms();



e. Save and close the file.

5. The last thing we need to do is connect the Cube Manager to our Game Manager.

    1. In Unity, click on the GameManager.
    2. Drag the Cube Manager GameObject from the scene to the Cube Manager box within the Inspector subpanel.
    3. Save the scene.

Step 2: Testing the New Application

Here we are using the RTI Shapes Demo shipped with RTI Connext to test our new application.

  1. During testing, you should notice that the shapes will start rotating.
  2. To prevent this behavior, click on the Cube object in your Prefabs folder, and in the Rigidbody section, under Constraints, select X, Y, and Z for Freeze Rotation.

Next: Module 5: Expanding to Multiple Topics

This video covers Module 4.