-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathHostMigrationSystem.cs
More file actions
34 lines (32 loc) · 1.34 KB
/
HostMigrationSystem.cs
File metadata and controls
34 lines (32 loc) · 1.34 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
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;
namespace Asteroids.Client
{
/// <summary>
/// Custom host migration logic for clients in the Asteroids sample. Place the client immediately in game again
/// after a host migration.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation|WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct HostMigrationSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<NetworkStreamDriver>();
state.RequireForUpdate<LevelComponent>();
}
public void OnUpdate(ref SystemState state)
{
foreach (var evt in SystemAPI.GetSingleton<NetworkStreamDriver>().ConnectionEventsForTick)
{
var reconnected = SystemAPI.GetComponentLookup<NetworkStreamIsReconnected>();
if (evt.State == ConnectionState.State.Connected && reconnected.HasComponent(evt.ConnectionEntity))
{
state.EntityManager.AddComponent<NetworkStreamInGame>(evt.ConnectionEntity);
// Remove the reconnection tag as we're done with it
state.EntityManager.RemoveComponent<NetworkStreamIsReconnected>(evt.ConnectionEntity);
}
}
}
}
}