Module 3: Subscribing to a Single Topic
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.
- Create a utility function to convert a color string to an Unity color as follows:
private Color ConvertStringToColor(string color)
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)
3. Finally create a function to return the Shape list.
public List<Shape> GetCubes()
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.
- Create a cube prefab within Unity by right-clicking in the Hierarchy subpanel and choosing 3D Object»Cube.
- Click on the Cube component in the Hierarchy and in the Inspector, click Add Component. Add a RigidBody.
- 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.
Add a public member for the prefab and a private member for the resulting GameObject.
public GameObject m_CubePrefab;
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())
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.
- Launch the RTI Shapes demo from the RTI Launcher.
- Publish to the Square topic in the RTI Shapes demo.
- Click OK to use the default settings.
- Run the Unity application. You should see a cube moving across the screen.
- 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.
This video covers Module 3.