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.
- In Unity, create the following folders under the Scripts folder:
- Comms
- Object Managers
- Shapes
- In the Scripts folder create the following C# script:
- ObjectDefs
- Under the Comms folder create the following C# scripts:
- DDSHandler
- ShapeComms
- Under the Object Managers folder create the following C# scripts:
- ShapeManager
- CubeManager
- SphereManager
- PyramidManager
- Under the Shapes folder create the following C# script:
- 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.
- Open up the ObjectDefs script and clear the file.
- 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;
} - 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.
- Open the DDSHandler script and modify as follows:
- Remove the MonoBehavior inheritance.
- Remove all code within the script.
- 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; - 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; } - Create a protected constructor with a try, catch within it.
protected DDSHandler()
{
try { }
catch () { }
} - The catch should look like the following:
catch (Exception exc)
{
Debug.Log(exc);
} - 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"); - Now open the ShapeComms script and make the following changes:
- Change the MonoBehavior inheritance to DDSHandler.
- Remove all code within the script.
- Add the following usings:
using Rti.Dds.Subscription;
using Rti.Types.Dynamic;
using System;
using System.Collections.Generic;
using UnityEngine; - Add the following class member:
private DynamicData d_CubeData;
- 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.
-
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);
} - 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);
} - 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.
- Once the Comms scripts are complete we need to add a small amount of code to the GameManager script.
- Add a single private member for DDS communications:
private ShapeComms m_DDS;
- Add the following code to the Start function:
m_DDS = new ShapeComms();
- 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");
- Save and close the script.
- 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.
- Once the Game Object is created, drag the GameManager script onto the Game Object and it will be connected.
- 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.
- Launch the RTI Shapes demo from the RTI Launcher.
- Subscribe to the Square topic in the RTI Shapes demo.
- Click OK to use the default settings.
- Run the Unity application. You should see several squares randomly appearing within the RTI Shapes Demo.
- 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.
- 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.