forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnHealthBarSystem.cs
More file actions
72 lines (67 loc) · 2.5 KB
/
Copy pathSpawnHealthBarSystem.cs
File metadata and controls
72 lines (67 loc) · 2.5 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
using System;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;
namespace Samples.HelloNetcode
{
#if !UNITY_DISABLE_MANAGED_COMPONENTS
public class HealthUI : IComponentData, IDisposable, ICloneable
{
public Transform HealthBar;
public Image HealthSlider;
public float3 Offset;
public void Dispose()
{
//The Healbar is disposed by the client in two cases:
//- By the DespawnHealthBarSystem (if the healt < 0)
//- When a character is respawn (so the ghost get destroyed). Being the HealthUI this method is called in that case.
if (HealthBar != null)
Object.Destroy(HealthBar.gameObject);
}
public object Clone()
{
if (HealthBar == null || HealthBar.gameObject == null)
return new HealthUI();
var newHealtbar = Object.Instantiate(HealthBar.gameObject);
var images = HealthBar.gameObject.GetComponentsInChildren<Image>();
return new HealthUI
{
HealthBar = newHealtbar.GetComponent<Transform>(),
HealthSlider = images[1]
};
}
}
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[UpdateInGroup(typeof(PresentationSystemGroup))]
public partial struct SpawnHealthBarSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<HealthBarSpawner>();
state.RequireForUpdate<Health>();
}
public void OnUpdate(ref SystemState state)
{
var ecb = new EntityCommandBuffer(Allocator.Temp);
var query = state.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<HealthBarSpawner>());
var spawner = query.GetSingleton<HealthBarSpawner>();
foreach (var (_, entity) in SystemAPI.Query<RefRO<Health>>()
.WithEntityAccess().WithNone<HealthUI>())
{
var go = Object.Instantiate(spawner.HealthBarPrefab);
var image = go.GetComponentsInChildren<Image>();
ecb.AddComponent(entity, new HealthUI
{
HealthBar = go.transform,
HealthSlider = image[1],
Offset = spawner.Offset,
});
}
ecb.Playback(state.EntityManager);
}
}
#endif
}