forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelManager.cs
More file actions
103 lines (89 loc) · 3.92 KB
/
Copy pathLevelManager.cs
File metadata and controls
103 lines (89 loc) · 3.92 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#if (!UNITY_SERVER || UNITY_EDITOR)
using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using Unity.Scenes;
using UnityEngine;
using UnityEngine.UI;
using Hash128 = Unity.Entities.Hash128;
public class LevelManager : MonoBehaviour
{
// TODO: subscene names are printed via entity names, which works in debug builds, we could
// save the names during conversion to get a more permanent place for them
// TODO: is using ghost sync on/off instead of ingame on/off, should probably be changed...
public Text ServerStatus;
public Text ClientStatus;
public Button ClientSyncButton;
public Button ServerSyncButton;
public void SendLoadNextLevelCommand()
{
if (ClientServerBootstrap.ClientWorld == null)
{
UnityEngine.Debug.LogError("No client world found");
return;
}
var rpcCmd = ClientServerBootstrap.ClientWorld.EntityManager.CreateEntity();
ClientServerBootstrap.ClientWorld.EntityManager.AddComponentData(rpcCmd, new LoadNextLevelCommand());
ClientServerBootstrap.ClientWorld.EntityManager.AddComponent<SendRpcCommandRequest>(rpcCmd);
}
public void ServerLoadLevel(int number)
{
if (ClientServerBootstrap.ServerWorld != null)
ClientServerBootstrap.ServerWorld.GetExistingSystemManaged<LevelLoader>().LoadLevel(number);
}
public void AllClientsLoadLevel(int number)
{
if (ClientServerBootstrap.ClientWorld != null)
ClientServerBootstrap.ClientWorld.GetExistingSystemManaged<LevelLoader>().LoadLevel(number);
}
public void ServerUnloadLevel(int number)
{
if (ClientServerBootstrap.ServerWorld != null)
ClientServerBootstrap.ServerWorld.GetExistingSystemManaged<LevelLoader>().UnloadLevel(number);
}
public void AllClientsUnloadLevel(int number)
{
if (ClientServerBootstrap.ClientWorld != null)
ClientServerBootstrap.ClientWorld.GetExistingSystemManaged<LevelLoader>().UnloadLevel(number);
}
public void ServerToggleSync()
{
if (ClientServerBootstrap.ServerWorld != null)
ClientServerBootstrap.ServerWorld.GetExistingSystemManaged<LevelLoader>().ToggleSync();
}
public void AllClientsToggleSync()
{
if (ClientServerBootstrap.ClientWorld == null)
return;
ClientServerBootstrap.ClientWorld.GetExistingSystemManaged<LevelLoader>().ToggleSync();
bool toggleOn = false;
var conQuery = ClientServerBootstrap.ClientWorld.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>());
var cons = conQuery.ToEntityArray(Allocator.Temp);
if (cons.Length != 1)
{
UnityEngine.Debug.LogError($"First client {ClientServerBootstrap.ClientWorld} goes not have any connection established");
return;
}
if (ClientServerBootstrap.ClientWorld.EntityManager.HasComponent<NetworkStreamInGame>(cons[0]))
toggleOn = true;
// When switching all clients off the server sync on their connections must be disabled as well or bad things happen
if (!toggleOn)
{
conQuery = ClientServerBootstrap.ServerWorld.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>());
cons = conQuery.ToEntityArray(Allocator.Temp);
var conIds = conQuery.ToComponentDataArray<NetworkId>(Allocator.Temp);
for (int i=0; i < cons.Length; ++i)
{
if (ClientServerBootstrap.ServerWorld.EntityManager.HasComponent<NetworkStreamInGame>(cons[i]))
{
UnityEngine.Debug.Log($"[{ClientServerBootstrap.ServerWorld}] Disable sync on {conIds[i].Value}");
ClientServerBootstrap.ServerWorld.EntityManager.RemoveComponent<NetworkStreamInGame>(cons[i]);
}
}
}
}
}
#endif