Skip to the main content.

Did you know?

 

RTI is the world’s largest DDS supplier and Connext is the most trusted software framework for critical systems.

Success-Plan-Services-DSSuccess-Plan Services

Our Professional Services and Customer Success teams bring extensive experience to train, problem-solve, mentor, and accelerate customer success.

Learn more

Developers

From downloads to Hello World, we've got you covered. Find all of the tutorials, documentation, peer conversations and inspiration you need to get started using Connext today.

Try the Connectivity Selection Tool ⇢

Resources

RTI provides a broad range of technical and high-level resources designed to assist in understanding industry applications, the RTI Connext product line and its underlying data-centric technology.

Company

RTI is the infrastructure software company for smart-world systems. The company’s RTI Connext product is the world's leading software framework for intelligent distributed systems.

Contact Us

News & Events
Cooperation

MODULE 4:
Gaming Visualizations: Using an Object Manager to Subscribe to Multiple Instances

⇠ Back to All Modules

Case + Code

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.

  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;

    3. 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;
          }

    4. 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;
          }

    5. 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);
      }

    6. 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));
          }

    7. 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);
              }
          }

    8. 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()
          {
              base.Awake();
          }

    4. Add the following code to the Update function.

      UpdateShape(m_DDS.GetCubes());

    5. 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;

    2. Delete the private m_Cube member, you won’t need it anymore.
    3. Delete the Update function. Updates will be handled within the Object Manager(s).
    4. 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);

      }

    5. 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.
 

Module 4 Demo

This video covers the steps for Module 4.

 

 

Keep Going! 

Module 5: Expanding to Multiple Topics