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
144 lines (130 loc) · 4.44 KB
/
Copy pathLevelManager.cs
File metadata and controls
144 lines (130 loc) · 4.44 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#if !UNITY_DOTSRUNTIME && (!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;
private World m_ServerWorld;
private World ServerWorld
{
get
{
if (m_ServerWorld == null)
{
foreach (var world in World.All)
{
if (world.IsServer())
{
m_ServerWorld = world;
break;
}
}
}
return m_ServerWorld;
}
}
public void SendLoadNextLevelCommand()
{
World clientOne = null;
foreach (var world in World.All)
{
if (world.IsClient() && !world.IsThinClient())
{
clientOne = world;
break;
}
}
if (clientOne == null)
{
UnityEngine.Debug.LogError("No client world found");
return;
}
var rpcCmd = clientOne.EntityManager.CreateEntity();
clientOne.EntityManager.AddComponentData(rpcCmd, new LoadNextLevelCommand());
clientOne.EntityManager.AddComponent<SendRpcCommandRequest>(rpcCmd);
}
public void ServerLoadLevel(int number)
{
ServerWorld.GetExistingSystemManaged<LevelLoader>().LoadLevel(number);
}
public void AllClientsLoadLevel(int number)
{
foreach (var world in World.All)
{
if (world.IsClient())
world.GetExistingSystemManaged<LevelLoader>().LoadLevel(number);
}
}
public void ServerUnloadLevel(int number)
{
ServerWorld.GetExistingSystemManaged<LevelLoader>().UnloadLevel(number);
}
public void AllClientsUnloadLevel(int number)
{
foreach (var world in World.All)
{
if (world.IsClient())
world.GetExistingSystemManaged<LevelLoader>().UnloadLevel(number);
}
}
public void ServerToggleSync()
{
ServerWorld.GetExistingSystemManaged<LevelLoader>().ToggleSync();
}
public void AllClientsToggleSync()
{
World clientOne = null;
foreach (var world in World.All)
{
if (world.IsClient())
{
world.GetExistingSystemManaged<LevelLoader>().ToggleSync();
if (clientOne == null)
clientOne = world;
}
}
bool toggleOn = false;
if (clientOne != null)
{
var conQuery = clientOne.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>());
var cons = conQuery.ToEntityArray(Allocator.Temp);
if (cons.Length != 1)
{
UnityEngine.Debug.LogError($"First client {clientOne} goes not have any connection established");
return;
}
if (clientOne.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 = 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 (ServerWorld.EntityManager.HasComponent<NetworkStreamInGame>(cons[i]))
{
UnityEngine.Debug.Log($"[{ServerWorld}] Disable sync on {conIds[i].Value}");
ServerWorld.EntityManager.RemoveComponent<NetworkStreamInGame>(cons[i]);
}
}
}
}
}
}
#endif