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 3:
Gaming Visualizations: Subscribing to a Single Topic

⇠ Back to All Modules

Case-Code-hero

Introduction

This module builds directly on Module 2: Testing Your Connection. You will need to complete that module before performing this module. In this module, we will update our existing class to include Shape reading capability. We will use this class to handle the DDS interface and visualize a single shape within Unity®.

Step 1: Set Up Read Function

Here we are going to add two utility functions that will be used more in future functions, as well as a reader for our Cube Shape.

  1. Create a utility function to convert a color string to an Unity color as follows:

    private Color ConvertStringToColor(string color)
    {
        switch (color)
        {
            case "PURPLE": return new Color(0.5f, 0.0f, 0.5f);
            case "BLUE": return Color.blue;
            case "RED": return Color.red;
            case "GREEN": return Color.green;
            case "YELLOW": return Color.yellow;
            case "CYAN": return Color.cyan;
            case "MAGENTA": return Color.magenta;
            case "ORANGE": return new Color(1.0f, 0.647f, 0.0f);
            default: return Color.clear;
        }
    }


    Although most of the possible colors have Unity constant equivalents, we have some design decision to make regarding purple and orange.

  2. Create a utility function to parse the data returned from the Data Reader to a List of type Shape. We will be reusing this function with future shapes.

    private List<Shape> ParseShapes(LoanedSamples<DynamicData> shapes)
        {
            List<Shape> returnList = new List<Shape>();

            foreach (var shape in shapes)
            {
                returnList.Add(new Shape()
                {
                    color = ConvertStringToColor((string)shape.Data.GetAnyValue("color")),
                    x = (Int32)shape.Data.GetAnyValue("x"),
                    y = (Int32)shape.Data.GetAnyValue("y"),
                    shapesize = (Int32)shape.Data.GetAnyValue("shapesize")
                });
            }

            return returnList;
        }

  3. Finally create a function to return the Shape list.

    public List<Shape> GetCubes()
        {
            return ParseShapes(DataReaderCube.Take());
        }

  4. Save and close the file.
Step 2: Create Cube Prefab

In this step we will create a simple cube prefab that will be used to visualize the movement of a blue square in the 3-dimensional Unity space.

  1. Create a cube prefab within Unity by right-clicking in the Hierarchy subpanel and choosing 3D Object»Cube.
  2. Click on the Cube component in the Hierarchy and in the Inspector, click Add Component. Add a RigidBody.
  3. Drag the Cube object into the Prefabs folder.
Step 3: Connecting the Prefab to DDS

At this point we will use data coming in from the databus to update the position of the cube and show it within the Unity environment.

  1. Add a public member for the prefab and a private member for the resulting GameObject.

    public GameObject m_CubePrefab;
    private GameObject m_Cube;

  2. Instantiate the Cube within the Start function.

    m_Cube = Instantiate(m_CubePrefab, new Vector3(), new Quaternion());

  3. Replace the current Update code with the following:

    foreach (var shape in m_DDS.GetCubes())
    {
        m_Cube.GetComponent<MeshRenderer>().material.color = shape.color;
        m_Cube.transform.localScale = new Vector3(shape.shapesize, shape.shapesize, shape.shapesize);
        m_Cube.GetComponent<Rigidbody>().MovePosition(new Vector3(shape.x, shape.y));
    }

  4. Save and close the file.
  5. In Unity, click on the GameManager object within the scene. Drag the Cube from the prefabs folder into the Cube Prefab field in the Inspector subpanel.
  6. In Unity, modify the Main Camera position to 128, 128, -255.
Step 4: Test Using Shapes Demo

By running our application alongside the Shapes Demo that ships with RTI Connext we can test our application and ensure that everything is correctly configured and working correctly.

  1. Launch the RTI Shapes demo from the RTI Launcher.
  2. Publish to the Square topic in the RTI Shapes demo.
  3. Click OK to use the default settings.
  4. Run the Unity application. You should see a cube moving across the screen.
  5. If a shape does not appear, check the debug log within Unity to see if there are any problems within your application. Common problems include a missing license or XML file, along with misspellings of references within the XML file.
 

Module 3 Demo

This video covers the steps for Module 3.

 

 

Keep Going! 

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