Create sessions in the Editor
Create a session in the Unity Editor using ScriptableObjects.
阅读时间2 分钟最后更新于 13 小时前
You can create and manage sessions using ScriptableObjects in the Unity Editor. There are two ScriptableObjects related to multiplayer sessions:
- : Stores sessions and makes lifecycles events available within the Editor.
MultiplayerSession - : Populates the
SessionConnectorScriptableObject using the existing operations such as Create and Join.MultiplayerSession
Use sessions ScriptableObjects
Follow these steps to use ScriptableObjects in your multiplayer project:
- Add a and
MultiplayerSessionScriptableObject.SessionConnector - Connect the ScriptableObject to the
MutliplayerSessionScriptableObject.SessionConnector - Configure the ScriptableObjects to match the needs of your project.
- (Optional) Configure component
SessionConnectorBehavior
Add ScriptableObjects to your scene
To add multiplayer ScriptableObjects to your project, open the Project window and right-click in the folder your want to create the assets in. Then navigate to Create > Services > Multiplayer. Select Multiplayer Session and then repeat the process and select Session Connector along with your preferred connection type to create both.
Connect the ScriptableObjects
A ScriptableObject requires a 'MultiplayerSession' ScriptableObject to work.
SessionConnectorAdd a ScriptableObject to a 'SessionConnector' ScriptableObject in the Multiplayer Session field of the ScriptableObject in the Inspector window.
MultiplayerSessionSessionConnectorConfigure 'SessionConnector' ScriptableObject
SessionConnectorCreateCreateOrJoinJoin
From the Inspector window, you can also configure the following settings:
- The maximum number of players (default is four).
- The session name.
- If the session is password protected, private, or locked.
- Network options such as IP, port, listen IP.
- Network type: direct, Relay, or none.
- Session operation events.
- Session lifecycle events.
CreateOrJoin
The connection type creates a session when the first user triggers the operation and adds the user to the session. When additional users arrive, they're added to already created session.
CreateOrJoinThis setting is recommended during experimentation and testing because all users are added to the same session.
Create
The connection type creates a new session with a unique ID.
CreateJoin
The connection type connects players to a specific session by using a session code or a session ID.
JoinConfigure SessionConnectorBehavior
You can optionally use a MonoBehaviour to manage session creation and sign-in in the Inspector window instead of in code. Assign a ScriptableObject. The MonoBehaviour determines whether to run the on sign in or only programatically. The determines the connection type (, , or ) while subscribes to all events.
SessionConnectorBehaviourSessionConnectorSessionConnectorSessionConnectorCreateJoinCreateOrJoinMultiplayerSessionISessionThe optional setting Execute On Sign In controls timing. When enabled, the behavior subscribes to during so the session operation fires automatically the moment the player authenticates. When disabled, you have to call manually, for example from a Join Game button.
AuthenticationService.SignedInOnServicesInitialized()Execute()Additional settings
You can enable verbose logging within the Project Settings window. Navigate to Project settings > Services > Multiplayer then select the box for Verbose Logging.