-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathGameServerManager.cs
More file actions
58 lines (50 loc) · 1.74 KB
/
GameServerManager.cs
File metadata and controls
58 lines (50 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Threading.Tasks;
using UnityEngine;
namespace Unity.Services.Samples.GameServerHosting
{
public class GameServerManager : MonoBehaviour
{
[field: SerializeField] public GameServerInfo_Data serverInfo { get; private set; }
public GameHostingServices gameHostingServices { get; private set; }
PlayerAuthentication m_AuthService;
public void Init(GameServerInfo_Data info)
{
DontDestroyOnLoad(gameObject);
serverInfo = info;
m_AuthService = new PlayerAuthentication();
}
public async Task<bool> InitServices(string profileName = null)
{
try
{
await m_AuthService.Init(profileName);
gameHostingServices = new GameHostingServices();
return true;
}
catch (Exception ex)
{
Debug.LogWarning($"[GSH] Game Service Initialization failed, continuing without.\n{ex}");
}
return false;
}
public async Task TryStartServer(string endpoint)
{
try
{
if (await gameHostingServices.StartGameServicesAsync(endpoint, serverInfo.Data))
Debug.Log($"[GSH] Started Game Server Hosting on {endpoint} with:\n{serverInfo.Data}!");
else
Debug.LogWarning($"[GSH] Failed to start Game server on {endpoint}");
}
catch (Exception ex)
{
Debug.LogError($"[GSH] Exception when starting Game server on {endpoint}.\n{ex}");
}
}
void OnDestroy()
{
gameHostingServices?.Dispose();
}
}
}