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.
- 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. - 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;
} - Finally create a function to return the Shape list.
public List<Shape> GetCubes()
{
return ParseShapes(DataReaderCube.Take());
} - 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;
private GameObject m_Cube; - Instantiate the Cube within the Start function.
m_Cube = Instantiate(m_CubePrefab, new Vector3(), new Quaternion());
- 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));
} - Save and close the file.
- 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.
- 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.
Module 3 Demo
This video covers the steps for Module 3.