Beispiel #1
0
    /// <summary>
    /// Called when an action is triggered from a device that isn't assigned to any user.
    /// </summary>
    /// <param name="user"></param>
    /// <param name="action"></param>
    /// <param name="control"></param>
    /// <remarks>
    /// In single-player mode, we concurrently enable all our bindings from all available control schemes.
    /// This means that the actions will bind to whatever devices are available. However, we only assign
    /// the devices actively used with the current control scheme to the user. This means that we always know
    /// how the player is currently controlling the game.
    ///
    /// When the player uses a binding that isn't part of the current control scheme, this method will
    /// be called. In here we automatically switch control schemes by looking at which control scheme is
    /// suited to the unassigned device.
    ///
    /// Note that the logic here also covers the case where there are multiple devices meant to be used
    /// with the same control scheme. For example, there may be two gamepads and the player is free to
    /// switch from one or the other. In that case, while we will stay on the Gamepad control scheme,
    /// we will still unassign the previously used gamepad from the player and assign the newly used one.
    /// </remarks>
    private void OnUnassignedDeviceUsed(IInputUser user, InputAction action, InputControl control)
    {
        // We only support control scheme switching in single player.
        if (!m_SinglePlayer)
        {
            return;
        }

        // All our IInputUsers are expected to be DemoPlayerControllers.
        var player = user as DemoPlayerController;

        if (player == null)
        {
            return;
        }

        ////REVIEW: should we just look at the binding that triggered and go by the binding group it is in?

        // Select a control scheme based on the device that was used.
        var device        = control.device;
        var controlScheme = player.SelectControlSchemeBasedOnDevice(device);

        // Give the device to the user and then switch control schemes.
        // If the control scheme requires additional devices, we select them automatically using
        // AndAssignMissingDevices().
        user.ClearAssignedInputDevices();
        user.AssignInputDevice(device);
        user.AssignControlScheme(controlScheme)
        .AndAssignMissingDevices();
    }