Class PlayerInputManager | Package Manager UI website
docs.unity3d.com
    Show / Hide Table of Contents

    Class PlayerInputManager

    Manages joining and leaving of players.

    Inheritance
    System.Object
    PlayerInputManager
    Namespace: UnityEngine.InputSystem
    Syntax
    public class PlayerInputManager : MonoBehaviour
    Remarks

    This is a singleton component. Only one instance is meant to be active in a game at any one time. To retrieve the current instance, use instance.

    Note that a PlayerInputManager is not strictly required to have multiple PlayerInput components. What PlayerInputManager provides is the implementation of specific player join mechanisms (joinBehavior) as well as automatic assignment of split-screen areas (splitScreen). However, you can always implement your own custom logic instead and simply instantiate multiple GameObjects with PlayerInput yourself.

    Fields

    PlayerJoinedMessage

    Name of the message that is sent when a player joins the game.

    Declaration
    public const string PlayerJoinedMessage = "OnPlayerJoined"
    Field Value
    Type Description
    System.String

    PlayerLeftMessage

    Declaration
    public const string PlayerLeftMessage = "OnPlayerLeft"
    Field Value
    Type Description
    System.String

    Properties

    fixedNumberOfSplitScreens

    Declaration
    public int fixedNumberOfSplitScreens { get; }
    Property Value
    Type Description
    System.Int32

    instance

    Declaration
    public static PlayerInputManager instance { get; }
    Property Value
    Type Description
    PlayerInputManager

    joinAction

    Declaration
    public InputActionProperty joinAction { get; set; }
    Property Value
    Type Description
    InputActionProperty

    joinBehavior

    Determines the mechanism by which players can join when joining is enabled (joiningEnabled).

    Declaration
    public PlayerJoinBehavior joinBehavior { get; set; }
    Property Value
    Type Description
    PlayerJoinBehavior

    joiningEnabled

    Whether new players can currently join.

    Declaration
    public bool joiningEnabled { get; }
    Property Value
    Type Description
    System.Boolean
    Remarks

    While this is true, new players can join via the mechanism determined by joinBehavior.

    See Also
    EnableJoining()
    DisableJoining()

    maintainAspectRatioInSplitScreen

    If splitScreen is enabled, this property determines whether subdividing the screen is allowed to produce screen areas that have an aspect ratio different from the screen resolution.

    Declaration
    public bool maintainAspectRatioInSplitScreen { get; }
    Property Value
    Type Description
    System.Boolean
    Remarks

    By default, when splitScreen is enabled, the manager will add or remove screen subdivisions in steps of two. This means that when, for example, the second player is added, the screen will be subdivided into a left and a right screen area; the left one allocated to the first player and the right one allocated to the second player.

    This behavior makes optimal use of screen real estate but will result in screen areas that have aspect ratios different from the screen resolution. If this is not acceptable, this property can be set to true to enforce split-screen to only create screen areas that have the same aspect ratio of the screen.

    This results in the screen being subdivided more aggressively. When, for example, a second player is added, the screen will immediately be divided into a four-way split-screen setup with the lower two screen areas not being used.

    This property is irrelevant if fixedNumberOfSplitScreens is used.

    maxPlayerCount

    Maximum number of players allowed concurrently in the game.

    Declaration
    public int maxPlayerCount { get; }
    Property Value
    Type Description
    System.Int32
    Remarks

    If this limit is reached, joining is turned off automatically.

    By default this is set to -1. Any negative value deactivates the player limit and allows arbitrary many players to join.

    notificationBehavior

    Declaration
    public PlayerNotifications notificationBehavior { get; set; }
    Property Value
    Type Description
    PlayerNotifications

    playerCount

    The current number of active players.

    Declaration
    public int playerCount { get; }
    Property Value
    Type Description
    System.Int32
    Remarks

    This count corresponds to all PlayerInput instances that are currently enabled.

    playerJoinedEvent

    Declaration
    public PlayerInputManager.PlayerJoinedEvent playerJoinedEvent { get; }
    Property Value
    Type Description
    PlayerInputManager.PlayerJoinedEvent

    playerLeftEvent

    Declaration
    public PlayerInputManager.PlayerLeftEvent playerLeftEvent { get; }
    Property Value
    Type Description
    PlayerInputManager.PlayerLeftEvent

    playerPrefab

    Declaration
    public GameObject playerPrefab { get; set; }
    Property Value
    Type Description
    GameObject

    splitScreen

    If enabled, each player will automatically be assigned a portion of the available screen area.

    Declaration
    public bool splitScreen { get; set; }
    Property Value
    Type Description
    System.Boolean
    Remarks

    For this to work, each PlayerInput component must have an associated object through camera.

    Note that as player join, the screen may be increasingly subdivided and players may see their previous screen area getting resized.

    splitScreenArea

    The normalized screen rectangle available for allocating player split-screens into.

    Declaration
    public Rect splitScreenArea { get; }
    Property Value
    Type Description
    Rect
    Remarks

    This is only used if splitScreen is true.

    By default it is set to (0,0,1,1), i.e. the entire screen area will be used for player screens. If, for example, part of the screen should display a UI/information shared by all players, this property can be used to cut off the area and not have it used by PlayerInputManager.

    Methods

    DisableJoining()

    Declaration
    public void DisableJoining()

    EnableJoining()

    Declaration
    public void EnableJoining()

    JoinPlayer(Int32, Int32, String, InputDevice)

    Declaration
    public void JoinPlayer(int playerIndex = -1, int splitScreenIndex = -1, string controlScheme = null, InputDevice pairWithDevice = null)
    Parameters
    Type Name Description
    System.Int32 playerIndex
    System.Int32 splitScreenIndex
    System.String controlScheme
    InputDevice pairWithDevice

    JoinPlayer(Int32, Int32, String, InputDevice[])

    Declaration
    public void JoinPlayer(int playerIndex = -1, int splitScreenIndex = -1, string controlScheme = null, params InputDevice[] pairWithDevices)
    Parameters
    Type Name Description
    System.Int32 playerIndex
    System.Int32 splitScreenIndex
    System.String controlScheme
    InputDevice[] pairWithDevices

    JoinPlayerFromAction(InputAction.CallbackContext)

    Join a new player based on input received through an InputAction.

    Declaration
    public void JoinPlayerFromAction(InputAction.CallbackContext context)
    Parameters
    Type Name Description
    InputAction.CallbackContext context

    JoinPlayerFromActionIfNotAlreadyJoined(InputAction.CallbackContext)

    Declaration
    public void JoinPlayerFromActionIfNotAlreadyJoined(InputAction.CallbackContext context)
    Parameters
    Type Name Description
    InputAction.CallbackContext context

    JoinPlayerFromUI()

    Join a new player based on input on a UI element.

    Declaration
    public void JoinPlayerFromUI()
    Remarks

    This should be called directly from a UI callback such as . The device that the player joins with is taken from the device that was used to interact with the UI element.

    Events

    onPlayerJoined

    Declaration
    public event Action<PlayerInput> onPlayerJoined
    Event Type
    Type Description
    System.Action<PlayerInput>

    onPlayerLeft

    Declaration
    public event Action<PlayerInput> onPlayerLeft
    Event Type
    Type Description
    System.Action<PlayerInput>
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023