Using the Wii U API

Using the Wii U API

When using the Wii U API provided with Unity to get input for controllers, there are four things you must always do:

  1. Import the API module
  2. Create an object to access the controller
  3. Get the state object of the controller
  4. Check for a specific state

You must get the new state of the controller every time before you check for button presses, etc. This is generally done every cycle in Update(). Also note that the Balance Board and Pro Controller API is built on the Wii Remote API. If there is a function that you cannot find in the Balance Board or Pro Controller API, you may be able to use the Wii Remote API. An example would be that you can check to see if a Pro Controller is out of batteries using UnityEngine.WiiU.Remote.status.

Code samples are given for the following:

  • GamePad
  • Wii Remote
  • MotionPlus
  • Nunchuk
  • Classic Controller and Classic Controller Pro*
  • Pro Controller*
  • Knowing What Controller is Connected
Note

The Pro Controller and Classic Controller Pro are not the same controller. The Classic Controller Pro plugs into a Wii Remote, and the Pro Controller is a stand alone unit with it's own batteries that are charged via USB cable. The button layout is the same, but getting input from them is different.

GamePad

Using the GamePad API, you have access to sensor data, button presses, error statuses and more. See  WiiU.GamePadState in the Unity for Wii U Scripting API for a list of all functions. The sample below shows how to implement some of them. For touch input, see the GamePad Touch Screen Input tutorial page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using UnityEngine;
using WiiU = UnityEngine.WiiU;

public class gamePadInputTest : MonoBehaviour
{
    WiiU.GamePad gamePad;
    void Start()
    {
        // Create a GamePad object
        gamePad = WiiU.GamePad.access;
    }
    void Update()
    {
        // Get the new state of the GamePad
        WiiU.GamePadState gamePadState = gamePad.state;

        if (gamePadState.gamePadErr == WiiU.GamePadError.None)
        {
            // Check for the A button
            if (gamePadState.IsTriggered(WiiU.GamePadButton.A))
            {
                // The A button was pressed
            }
            
            // Read data from the gyroscope
            Vector3 gyroData = gamePadState.gyro;
            
            // Read data from the left stick
            Vector2 leftStick = gamePadState.lStick;
        }
    }
}

Wii Remote

If you don't enable the MotionPlus features of a Wii Remote Plus, it acts as a regular Wii Remote. The following code shows how to set up two Wii Remotes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using UnityEngine;
using WiiU = UnityEngine.WiiU;

public class wiiRemoteInputTest : MonoBehaviour
{
    WiiU.Remote remote0;
    WiiU.Remote remote1;

     void Start()
    {
        // Create Wii Remote objects using channel 0 and channel 1
        remote0 = WiiU.Remote.Access(0);
        remote1 = WiiU.Remote.Access(1);
    }

    void Update()
    {
        // Get the new state of the controllers
        WiiU.RemoteState r0State = remote0.state;
        WiiU.RemoteState r1State = remote1.state;
    
        // Check for input from player 1
        if (r0State.err == WiiU.RemoteError.None)
        {
            if (r0State.IsTriggered(WiiU.RemoteButton.A))
            {
                // The player 1 A button was pressed
            }
            // Get the orientation of the controller
            Vector3 r0Acc = r0State.acc;
        }

        // Check for input from player 2
        if (r1State.err == WiiU.RemoteError.None)
        {
            if (r1State.IsTriggered(WiiU.RemoteButton.A))
            {
                // The player 2 A button was pressed
            }
        }
    }
}

MotionPlus

Note

If you enable MotionPlus, other Wii Remote attachments (classic controller, nunchuk) will not work. This is due to a limitation in Unity.

There are extra steps to detect if a Wii Remote has MotionPlus features and to enable them. You can initially enable MotionPlus before the controller is on, but if the user disconnects/reconnects the MotionPlus attachment, you need to disable/re-enable MotionPlus. You can check if MotionPlus is connected by using the MotionPlus remote mode (see sample below) occasionally. Calling this function too often can cause problems with the Wii Remote, such as sound from the Wii Remote might stop playing, so we recommend not checking every frame. Leaving motion plus on consumes the battery faster. It is prohibited to turn on MotionPlus in applications that don't use it.

This code sample builds on what was used in the Wii Remote sample.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Create a MotionPlus object
WiiU.MotionPlus mpRemote0 = remote0.motionPlus; // Using remote0 from the Wii Remote sample
// Enable MotionPlus
mpRemote0.Enable(WiiU.MotionPlusMode.Standard);

// Check if the MotionPlus attachment was removed (every several frames)
if (mpRemote0.mode == WiiU.MotionPlusMode.Off)
{
    // Alert the user to connect MotionPlus, and wait for them to do so, or disable MotionPlus
    mpRemote0.Disable();
}

// Get the 3D attitude of the X axis
Vector3 xAttitude = r0State.motionPlus.dir.X;

Nunchuk

Using the Nunchuk is simple, but the API for it is split between WiiU.RemoteState (for buttons) and WiiU.RemoteState.nunchuk (for joystick and sensor data). Buttons are the WiiU.RemoteButton type. The following code can be added to the above Wii Remote sample code.

1
2
3
4
5
6
7
if (r0State.IsPressed(WiiU.RemoteButton.NunchukC)) // Using remote0 from the Wii Remote sample
{
    // The C button was pressed
}

// Get input from the nunchuk joystick
Vector2 nunchukStick = r0State.nunchuk.stick;

Classic Controller and Classic Controller Pro

The Classic Controller and Classic Controller Pro are the same controller with a different shape, so the API calls are the same. All button presses and joystick data are available from WiiU.RemoteState.classic, and the buttons are the WiiU.ClassicButton type.

1
2
3
4
5
6
if (r0State.classic.IsPressed(WiiU.ClassicButton.A)) // Using remote0 from the Wii Remote sample
{
    // The A button on the Classic Controller was pressed
}
// Get input from the left joystick
Vector2 r0LeftStick = r0State.classic.leftStick;

Pro Controller

Pro Controller input is similar to input for Classic Controllers. All button presses and joystick data are available from WiiU.RemoteState.pro, and the buttons are the WiiU.ProControllerButton type. The initial setup is the same as with Wii Remotes.

1
2
3
4
5
6
if (r0State.pro.IsPressed(WiiU.ProControllerButton.A)) // Using remote0 from the Wii Remote sample
{
    // The A button on the Pro Controller was pressed
}
// Get input from the left joystick
Vector2 r0LeftStick = r0State.pro.leftStick;

Knowing What Controller is Connected

There are two ways to tell what controller is connected, either with WiiU.RemoteState.devType or WiiU.Remote.Probe(). They both give the same, up-to-date results, but RemoteState.devType is far simpler to use. It returns a WiiU.RemoteDevType. See the Unity API for a full list of the devTypes.

1
2
3
4
if (r0State.devType == WiiU.RemoteDevType.Nunchuk) // Using remote0 from the Wii Remote sample
{
    // The nunchuk is attached
}

 


CONFIDENTIAL