-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionMarkerManager.cs
58 lines (49 loc) · 2.41 KB
/
SelectionMarkerManager.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
using SimpleCrowdsSpawn.Systems;
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;
namespace SimpleCrowdsSpawn {
/// <summary>
/// There's no Animator counterpart in ECS.
/// It keeps track of a selected entity and moves the selection marker to its position on every frame lateUpdate.
/// </summary>
public class SelectionMarkerManager : MonoBehaviour {
[SerializeField] private GameObject selectionMarkerPrefab;
private GameObject _selectionMarkerInstance;
private World _world;
private PlaceSpawnerSystem _placeSpawnerSystem;
private Entity _selectedEntity;
private void OnEnable() {
_selectionMarkerInstance = Instantiate(selectionMarkerPrefab);
_selectionMarkerInstance.SetActive(false);
_world = World.DefaultGameObjectInjectionWorld;
if (_world.IsCreated) {
_placeSpawnerSystem = _world.GetExistingSystemManaged<PlaceSpawnerSystem>();
_placeSpawnerSystem.EntitySelected += OnEntitySelected;
}
}
private void OnDisable() {
if (_world.IsCreated && _placeSpawnerSystem != null) _placeSpawnerSystem.EntitySelected -= OnEntitySelected;
if (_selectionMarkerInstance) Destroy(_selectionMarkerInstance);
}
private void OnEntitySelected(Entity entity) {
_selectedEntity = entity;
}
private void LateUpdate() {
if (_selectedEntity != Entity.Null && _world.IsCreated && _world.EntityManager.Exists(_selectedEntity)) {
if (!_selectionMarkerInstance.activeSelf) _selectionMarkerInstance.SetActive(true);
_selectionMarkerInstance.transform.position = _world.EntityManager.GetComponentData<LocalTransform>(_selectedEntity).Position;
} else {
_selectedEntity = Entity.Null;
if (_selectionMarkerInstance.activeSelf) _selectionMarkerInstance.SetActive(false);
}
}
// private void OnDrawGizmos() {
// if (_selectedEntity != Entity.Null && _world.IsCreated && _world.EntityManager.Exists(_selectedEntity)) {
// var position = _world.EntityManager.GetComponentData<LocalTransform>(_selectedEntity).Position;
// Gizmos.color = Color.red;
// Gizmos.DrawSphere(position, 0.5f);
// }
// }
}
}