-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathLevelStatusSystem.cs
106 lines (101 loc) · 3.22 KB
/
LevelStatusSystem.cs
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
#if (!UNITY_SERVER || UNITY_EDITOR)
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using Unity.Scenes;
using UnityEngine;
using UnityEngine.UI;
[RequireMatchingQueriesForUpdate]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation | WorldSystemFilterFlags.ClientSimulation)]
public partial class LevelStatusSystem : SystemBase
{
private Text m_StatusText;
private Button m_SyncButton;
private string m_Prefix;
private bool m_IsServer;
protected override void OnCreate()
{
var levelManager = Object.FindFirstObjectByType<LevelManager>();
if (levelManager == null)
{
Enabled = false;
return;
}
if (World.IsServer())
{
m_StatusText = levelManager.ServerStatus;
m_SyncButton = levelManager.ServerSyncButton;
m_IsServer = true;
}
else if (World.IsClient())
{
m_StatusText = levelManager.ClientStatus;
m_SyncButton = levelManager.ClientSyncButton;
}
else
{
Enabled = false;
}
m_Prefix = World.Name + " ";
}
protected override void OnUpdate()
{
var sceneSystem = World.GetExistingSystem<SceneSystem>();
m_StatusText.text = m_Prefix;
if (m_IsServer)
{
FixedString512Bytes inGame = "";
bool syncOn = false;
Entities.ForEach((Entity connEntity, in NetworkId netId) =>
{
if (SystemAPI.HasComponent<NetworkStreamInGame>(connEntity))
{
var appendMe = FixedString.Format("[{0}]:ON", netId.Value);
inGame.Append(appendMe);
syncOn = true;
}
else
{
var appendMe = FixedString.Format("[{0}]:OFF", netId.Value);
inGame.Append(appendMe);
syncOn = false;
}
}).Run();
m_StatusText.text += inGame;
// Flip toggle to off if it's is on currently but sync is disabled
if (syncOn)
m_SyncButton.image.color = Color.gray;
else
m_SyncButton.image.color = Color.white;
}
else
{
if (SystemAPI.HasSingleton<NetworkStreamInGame>())
{
m_StatusText.text += "ON";
m_SyncButton.image.color = Color.gray;
}
else
{
m_StatusText.text += "OFF";
m_SyncButton.image.color = Color.white;
}
}
m_StatusText.text += ": ";
FixedString512Bytes levels = "";
Entities.WithoutBurst().ForEach((Entity sceneEntity, in SceneReference subscene) =>
{
if (SceneSystem.IsSceneLoaded(World.Unmanaged, sceneEntity))
{
EntityManager.GetName(sceneEntity, out var debugName);
if (debugName.IsEmpty)
levels.Append(subscene.SceneGUID.ToString());
else
levels.Append(debugName);
levels += " ";
}
}).Run();
m_StatusText.text += levels;
}
}
#endif