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 2:
Gaming Visualizations: Testing Your Connection

⇠ Back to All Modules

Case-Code-hero

Introduction

This module builds directly on Module 1: Configuring Your Unity Application. You will need to complete that module before performing this module. Once the environment is set up, we need to add a class to handle DDS connections and reference an XML file that handles configuration of participants, subscribers, publishers, topics, data readers, and data writers. At the end of this module, you will have created your first utility class and will be publishing data that can be visualized within the Shapes Demo.

Step 1: Create the Base Files

We are going to start off setting up our file structure and creating a series of scripts that we will be editing.

  1. In Unity, create the following folders under the Scripts folder:
    1. Comms
    2. Object Managers
    3. Shapes
  2. In the Scripts folder create the following C# script:
    1. ObjectDefs
  3. Under the Comms folder create the following C# scripts:
    1. DDSHandler
    2. ShapeComms
  4. Under the Object Managers folder create the following C# scripts:
    1. ShapeManager
    2. CubeManager
    3. SphereManager
    4. PyramidManager
  5. Under the Shapes folder create the following C# script:
    1. PyramidMeshGenerator
Step 2: Set Up Object Definitions

In this project, we will need to convert data coming in and going to the DDS class from Dynamic Data to a native data class within our application. In this step we will create this base data class.

  1. Open up the ObjectDefs script and clear the file.
  2. For this file we will only need a single using and our Shapes class definition. It should look like this:

    using UnityEngine;

    public class Shape
    {
        public Color color;
        public int x;
        public int y;
        public int shapesize;
    }

  3. Save and close this file.
Step 3: Create the DDS Class

To begin, we are going to create a class within the Unity environment and use this class to make all connections to the DDS libraries. For this project, we used RTI System Designer to create an XML file that contains all our DDS configuration. For more information on System Designer click here. The XML file used in this project is linked to below.

  1. Open the DDSHandler script and modify as follows:
    1. Remove the MonoBehavior inheritance.
    2. Remove all code within the script.
    3. Add the following usings:

      using Rti.Dds.Core;
      using Rti.Dds.Domain;
      using Rti.Dds.Publication;
      using Rti.Dds.Subscription;
      using Rti.Types.Dynamic;
      using System;
      using UnityEngine;

    4. Add the following class members:

      private readonly QosProvider provider;
      private readonly DomainParticipant participant;
      private readonly Publisher publisher;
      private readonly Subscriber subscriber;

      protected DataReader<DynamicData> DataReaderCube { get; private set; }
      protected DataWriter<DynamicData> DataWriterCube { get; private set; }

    5. Create a protected constructor with a try, catch within it.

      protected DDSHandler()
          {
              try { }
              catch () { }
          }

    6. The catch should look like the following:

      catch (Exception exc)
              {
                  Debug.Log(exc);
              }

    7. The try should look like the following:

      provider = new QosProvider("Shapes.xml");

      participant = provider.CreateParticipantFromConfig("ShapeParticipantLibrary::ShapeParticipant");



      publisher = participant.LookupPublisher("publisher");
      subscriber = participant.LookupSubscriber("subscriber");

      DataReaderCube = (DataReader<DynamicData>)subscriber.LookupDataReaderByName("CubeReader");



      DataWriterCube = (DataWriter<DynamicData>)publisher.LookupDataWriterByName("CubeWriter");

  2. Now open the ShapeComms script and make the following changes:
    1. Change the MonoBehavior inheritance to DDSHandler.
    2. Remove all code within the script.
    3. Add the following usings:

      using Rti.Dds.Subscription;
      using Rti.Types.Dynamic;
      using System;
      using System.Collections.Generic;
      using UnityEngine;

    4. Add the following class member:

      private DynamicData d_CubeData;

    5. Create the constructor as follows:

      public ShapeComms() : base()
          {
              try
              {
                  d_CubeData = DataWriterCube.CreateData();            

              }
              catch (Exception exc)
              {
                  Debug.Log(exc);
              }
          }


      Here we are initializing the Dynamic Data member that will be used for writing to our Cube Writer.

    6. We are going to start off with the creation of only our Write function for this section. We will add the Reader functions in a future module.

      We will start with a generic function that will take a Dynamic Data reference and a Shape in order to copy the Shape information into the Dynamic Data object.

      private void SetShapeData(ref DynamicData data, Shape shape, String color)
      {
          data.ClearAllMembers();

          data.SetValue("color", color);
          data.SetValue("x", shape.x);
          data.SetValue("y", shape.y);
          data.SetValue("shapesize", shape.shapesize);
      }

    7. Now let us use this generic function within our Cube Writer.

      public void WriteCube(Shape shape, String color)
          {
              SetShapeData(ref d_CubeData, shape, color);

              DataWriterCube.Write(d_CubeData);
          }

    8. Save and close the file.
Step 4: Use the DDS Class

Now that the DDS class has been created, we can use the class within our GameManager class. Then, by linking our GameManager script to a GameObject within Unity we will then be ready to test this simple application.

  1. Once the Comms scripts are complete we need to add a small amount of code to the GameManager script.
    1. Add a single private member for DDS communications:

      private ShapeComms m_DDS;

    2. Add the following code to the Start function:

      m_DDS = new ShapeComms();

    3. Add the following code to the Update function:

      m_DDS.WriteCube(new Shape { shapesize = 20, x = Random.Range(0, 255), y = Random.Range(0, 255) }, "BLUE");

    4. Save and close the script.
  2. In order to connect the GameManager script to the application, it needs to be added to the scene through a Game Object. Create a GameManager GameObject within the scene in Unity.
  3. Once the Game Object is created, drag the GameManager script onto the Game Object and it will be connected.
  4. At this point make sure that your RTI Connext license is copied into your project folder.
Step 5: Test Using Shape 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. Subscribe 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 several squares randomly appearing within the RTI Shapes Demo.
  5. If shapes do 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.
  6. If you build the Unity application, you will need to copy the Shapes XML file to the same directory as the built executable.
 

Module 2 Demo

This video covers the steps for Module 2.

 

 

Keep Going! 

Module 3: Subscribing to a Single Topic