Integrating Table Realms in Unity

Please Note: This section is under construction. Please check back regularly for updates.

Overview

In order to use the Table Realms functionality within the Unity development engine, you need to do the following:

  • Import the Table Realms prefab into the scene
  • Create a canvas with an image to host the QR code
  • Set up the script to load the QR code from the URL
  • Set up a device class to facilitate the Table Realms player behaviour
  • Create listeners for the player interactions
  • Update the PlayerController script to replace the keyboard controls with the device controls

But First:

  • Create an Aug. If you’re unsure of how to do this, please refer to this chapter.
  • Place the Aug file in the Assets folder

Please Note: This tutorial is based off the Roll a Ball tutorial available from Unity. The PlayerController.cs script referred to in the tutorial is from the Roll a Ball tutorial, and adapted to fit Table Realms.

Creating the QR Hosting Canvas

  • Right click on the “Hierarchy” pane on the left hand side of the screen
  • Mouse over the “UI” menu option, and then select the “canvas” option in the secondary menu that appears
  • With the canvas object selected, add an Image UI element in the same way as previously

Loading the QR Code from the URL

  • Navigate to Assets/Plugins/TableRealms/Scripts folder
  • Drag the TableRealmsQRCodeInjector.cs script onto the Image in the Hierarchy.

Using the Device Class

We need to create a script that extends the TableRealms TableRealmsPlayerActionBehavior class to account for the commands issued by our player controller. Create a new C# script in your Assets\Scripts folder called TableRealmsDevice.cs:

Before editing the script, let’s complete the outstanding information for the TableRealms prefab. Open the TableRealms prefab in the Inspector and type the name of the TableRealmsDevice script into the Player Command Script field:

Creating the Listeners

  • Create a script to extend the Table Realms TableRealmsPlayerActionBehavior class to account for the commands issued by our player controller.
  • Create a new C# script in your Assets\Scripts folder called TableRealmsDevice.cs
  • Complete the outstanding information for the Table Realms prefab

    • Open the TableRealms prefab in the Inspector and type the name of the TableRealmsDevice script into the Player Command Script field
    • Open the Script in your default C# editor
    • Create a private variable to contain the playerController object from our scene > Our new class extends the parent class and we also need to make sure that the parent class’s Start () function is executed during our new class’s Start () routine.
    • Add the following statement to the Start () function: base.Start (); > When this class is instantiated, we need to attach it to the PlayerController script that drives the control of the Player object in a way that identifies this device as the interpreter for player behaviour on the controller.
    • add the following statements to the Start () function:

      playerController = GameObject.Find ("Player").GetComponent<PlayerController> ();
      playerController.device = this; 
      

      Replacing the Keyboard controls

Interaction between the controller and the game can be facilitated in one of two ways. * Firing a command from the controller to the game that will then trigger an in-game action * Continuously polling the controller from the game to see if any player actions have been taken

The polling method for thumbsticks and buttons will be discussed first.

Polling Thumbsticks

To facilitate the polling of the thumbstick, add the following to the script, after the Start() function:

// Joystick behaviour.
public Vector2 PollThumbStick () {
return new Vector2(TableRealmsModel.instance.GetData<float>(gameObject.name + ".ThumbStick.T1.x"), TableRealmsModel.instance.GetData<float>(gameObject.name + ".ThumbStick.T1.y"));
}

When called, this function will return a Vector2 object that contains the horizontal and vertical thumbstick coordinates (as float values) for the ThumbStick.T1 component gameObject as identified by its name, which in this case is the controller object that has been attached to the scene at start-up. The first part of ThumbStick.T1 identifies the type of object that we are polling, and the second part identifies the name of the specific object as specified in the Table Realms IDE:

Polling Buttons

We could also use polling to track player action through the Jump and Stop buttons, but this is less efficient that firing the action from the controller and affecting the game directly and immediately. Setting up an action method consists of two parts. Firstly, we need to create the action method in our TableRealmsDevice script:

    // Jump button behaviour.
    public void ActionJump () {
        playerController.Jump ();
    }

This method will execute the Jump() function of the playerController script that we’ve attached to the Player object.

Secondly, we need to link this method to our button on the controller. Open the Designer and navigate to the Jump button in the tree-view. In the Button’s Script field, add the following:

The SendAction(model.id .. “.ActionJump”) statement will perform the button’s SendAction method to trigger the ActionJump method in the TableRealmsDevice.cs script.

Now that we can start polling the components on the controller to see what the player is doing, we need to update the PlayerController script to interpret and run the results.

Open the PlayerController script and amend the FixedUpdate() method as follows:

    void FixedUpdate () {
        
        if (!(device == null)) {
            Vector2 stick = device.PollThumbStick ();
            moveHorizontal = stick.x;
            moveVertical = stick.y;
            Move ();
        } else {
            moveHorizontal = Input.GetAxis ("Horizontal");
            moveVertical = Input.GetAxis ("Vertical");
            Move ();

            if (Input.GetButtonDown ("Jump")) {
                Jump ();
            }

            if (Input.GetButtonDown ("Fire1")) {
                Stop ();
            }
        }

    }

You’ll notice the only real changes are that we only execute the previous movement code if there is no device attached to the scene and if there is a device, we poll the thumb-stick to check for movement. You will recall that the Jump and Stop actions are executed directly by the controller through the associated TabeRealmsDevice methods. Let’s take a closer look at the new code: Vector2 thumbStickMovement = device.PollThumbStick (); Here we call the PollThumbStick() method that we wrote in the TableRealmsDevice script. The result is returned to the thumbStickMovement Vector2 object as the x and y movement values. moveHorizontal = thumbStickMovement.x; moveVertical = thumbStickMovement.y; We now assign the x and y values to the moveHorizontal and moveVertical variables so that we can apply movement to our ball through the Move() method.