FriendManagerInitializer
Scripts/Managers/FriendManagerInitializer.cs
The FriendManagerInitializer is a MonoBehaviour that bootstraps the Universal Friend List system. It creates the FriendManager, sets up the network system based on your configuration, and handles the connection between settings and runtime behavior.
Why Use the Initializer?
The initializer solves several important problems:
- User ID Management: The friend system needs to know who the current player is. The initializer handles setting this after your authentication system provides a user ID.
- Network System Creation: Based on your BackendType selection, the initializer creates the appropriate network-specific friend system implementation.
- Settings Application: It applies your FriendManagerSettings ScriptableObject to the runtime FriendManager.
- Lifecycle Control: You can control WHEN initialization happens (on game start vs after login).
Inspector Fields
| Field | Type | Description |
|---|---|---|
Settings | FriendManagerSettings | Reference to your settings asset (required) |
InitializeOnStart | bool | If true, auto-initialize when game starts |
UserId | string | Optional override for user ID (for custom auth) |
When to Initialize
Automatic (InitializeOnStart = true)
Best for: Quick setup, games without login screens, prototypes
The system initializes automatically when the game starts. The UserId will come from the network backend (for native implementations) or the UserId field (for custom backends).
Manual (InitializeOnStart = false)
Best for: Games with authentication, controlling initialization order
You'll call Initialize() or Initialize(userId) manually at the right time (after login is complete).
// Example: Initialize after login
public void OnLoginComplete(string userId) {
var initializer = FindObjectOfType();
initializer.Initialize(userId);
}
Public Methods
| Method | Description |
|---|---|
Initialize() | Initialize using settings and auto-detected user ID |
Initialize(string userId) | Initialize with a specific user ID |
CreateNetworkSystem() | Create the network system based on settings |
GetUserId() | Get the current user ID (from network or override) |
Setup Instructions
Step-by-Step Setup
- Create an empty GameObject in your first scene (e.g., "GameManager")
- Add the
FriendManagerInitializercomponent - Assign your
FriendManagerSettingsasset to the Settings field - Set InitializeOnStart based on your game flow:
- true = Initialize immediately when game loads
- false = You'll call Initialize() manually
- For custom backends without native auth, set the UserId field or override it programmatically
Integration with Authentication
For games with custom authentication systems:
public class MyAuthManager : MonoBehaviour {
public FriendManagerInitializer friendInitializer;
public void OnPlayerLoggedIn(PlayerAccount account) {
// Initialize friend system with the authenticated user ID
if (friendInitializer != null) {
friendInitializer.Initialize(account.UserId);
}
}
}
For Photon-based games, the initializer automatically uses the Photon user ID when using native implementation.