MODULE 4:
Gaming Visualizations: Using an Object Manager to Subscribe to Multiple Instances
Introduction
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.
- 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.
- Remove all the code from within the class. We will not be using the Update and Start functions within this class.
- Add the following members to the class.
public GameObject m_ShapePrefab;
protected ShapeComms m_DDS;
private Dictionary<Color, GameObject> m_Shapes; - 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;
} - 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;
} - 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);
} - 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));
} - 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))
MoveShape(shape);
else
AddInstance(shape);
}
} - Save and close the file.
- Open the CubeManager script. This will need to be changed to a derived instance of the ShapeManager class.
- Change the inheritance of the class from MonoBehavior to ShapeManager.
- Delete the Start function.
- Add the following Awake function to call the base Awake function.
protected override void Awake()
{
base.Awake();
} - Add the following code to the Update function.
UpdateShape(m_DDS.GetCubes());
- Save and close the file.
- Now we need to set up the Cube Manager within the Unity environment.
- In Unity, add an empty GameObject to the Hierarchy by right-clicking in the Hierarchy and selecting Create Empty. Change its name to CubeManager.
- Drag the CubeManager script into the CubeManager GameObject.
- Drag the Cube object from the Prefabs folder into the Shape Prefab box in the Inspector subpanel.
- Save the scene.
- Now that the cube manager is created and configured, we need to integrate it into the Game Manager.
- Replace the Cube Prefab variable with a reference to the CubeManager GameObject.
public GameObject m_CubeManager;
- Delete the private m_Cube member, you won’t need it anymore.
- Delete the Update function. Updates will be handled within the Object Manager(s).
- 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();
m_CubeManager.SetActive(true);
m_CubeManager.GetComponent<CubeManager>().Setup(m_DDS);}
- Save and close the file.
- The last thing we need to do is connect the Cube Manager to our Game Manager.
- In Unity, click on the GameManager.
- Drag the Cube Manager GameObject from the scene to the Cube Manager box within the Inspector subpanel.
- 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.
- During testing, you should notice that the shapes will start rotating.
- 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.
Module 4 Demo
This video covers the steps for Module 4.