Unity package for creating virtual session games.
Avaliable on Asset Store: https://assetstore.unity.com/packages/slug/309268
Forum thread: https://discussions.unity.com/t/virtual-session-manager-open-source/1603167
This Unity package provides a set of customizable prefabs and scripts designed to facilitate virtual sessions. It includes:
- Customizable Prefabs: Prebuilt objects with everything needed for virtual sessions.
- Session Management Scripts: Scripts to control visibility and interaction between sessions.
- Demo Scene: A simple scenario demonstrating the package's functionality.
There are 3 different ways of adding the package to your project:
- import VirtualSession.unitypackage via Assets-Import Package
- clone/download this repository and move the root folder to your Unity project's Assets folder
- import it from Asset Store
The package includes two basics prefabs for quick integration:
- NetworkPlayer.prefab: PlayerObject with the basic functionalities necessary for the correct functioning of inter-session visibility. Open to its extension in the customisation of attributes or methods.
- Session.prefab: NetworkObject in charge of managing visibility between clients connected to the same session.
To use these two prefabs, make sure that they are included in the Network Prefab List managed by the NetworkManager, and select the NetworkPlayer.prefab as the Default Player Prefab
in the NetworkManager configuration.
Session.prefab is instantiated from the SessionManager.cs, so be sure to indicate the path where the prefab is located.
A sample scene is included to demonstrate a basic implementation. Open it via VirtualSession/DemoScene
to explore.
Description: Handles the current active sessions.
Methods:
GameObject GetSession(string sessionId)
: Gets the GameObject associated to the session.void AssignToSession(string session, ulong clientId)
: Assigns the new connected client to the session. If it does not exist, it creates one by callingCreateGameSession(session)
.void CreateGameSession(string sessionId)
: Instantiates a session and spawns it. The new session is added to active sessions.void RemoveGameSession(string sessionId)
: Removes the session from the actives sessions and destroys its GameObject.
Description: Manages the logic inside a single session.
Methods:
void OnNetworkSpawn()
: Overrides default implementation when client-side code running. It requests the server to parent the client to the session's GameObject and makes visible already connected clients.void ShowClientInSessionServerRpc(ulong clientObjectId, ulong clientIdToShow)
: ServerRPC used to make visible a NetworkObject to a single client.void PlayersChanged(NetworkListEvent<ulong> changeEvent)
: Listens to changes on the players list and performs the corresponding actions. When a new client is added to the session,ShowClientInSessionServerRpc(ulong clientObjectId, ulong clientIdToShow)
is called for every connected client.void AddPlayer(ulong clientId)
: Called from SessionManager.cs, it makes visible the session to the new client and adds the client to the players list.void ParentClientToSessionObjectServerRpc(ServerRpcParams serverRpcParams = default)
: ServerRPC used for parenting the client to the session.
Description: Manages the logic of a single client.
Methods:
void OnNetworkSpawn()
: Overrides default implementation for both client-side (only the owner) and server-side. If it is the owner, it asks the server to be assigned to the session. If it is the server, it subscribesCheckVisibility
to the client's visibility delegate.bool CheckVisibility(ulong clientId)
: As they spawn with no observers, it is changed theCheckObjectVisibility
delegate of the client to return true for the owner of the script, i.e. the client itself.void UnsubscribeVisibility()
: Unsubscribes the visibility delegate to reset to default behaviour.void AddPlayer(ulong clientId)
: Called from SessionManager.cs, it makes visible the session to the new client and it adds the client to the players list.AssignToSessionServerRpc(string session, ServerRpcParams serverRpcParams = default)
: ServerRPC called fromOnNetworkSpawn
to assign the client to the session. It callsAssigntToSession
from SessionManager.cs.
Feel free to submit issues, feature requests, or pull requests to improve the package.
This project is licensed under the MIT License. See LICENSE
for details.
For questions or support, reach out via the repository's issue tracker.