| | 1 | | using UnityEngine; |
| | 2 | | using System.Collections.Generic; |
| | 3 | |
|
| | 4 | | namespace Builder.MeshLoadIndicator |
| | 5 | | { |
| | 6 | | public class DCLBuilderMeshLoadIndicatorController : MonoBehaviour |
| | 7 | | { |
| | 8 | | [SerializeField] private DCLBuilderMeshLoadIndicator baseIndicator = null; |
| | 9 | |
|
| | 10 | | private Queue<DCLBuilderMeshLoadIndicator> indicatorsAvailable; |
| | 11 | | private List<DCLBuilderMeshLoadIndicator> indicatorsInUse; |
| | 12 | |
|
| | 13 | | private bool isGameObjectActive = false; |
| | 14 | | private bool isPreviewMode = false; |
| | 15 | |
|
| 2 | 16 | | private void Awake() { Init(); } |
| | 17 | |
|
| | 18 | | private void OnEnable() |
| | 19 | | { |
| 1 | 20 | | if (!isGameObjectActive) |
| | 21 | | { |
| 1 | 22 | | DCLBuilderEntity.OnEntityAddedWithTransform += OnEntityAdded; |
| 1 | 23 | | DCLBuilderEntity.OnEntityShapeUpdated += OnShapeUpdated; |
| 1 | 24 | | DCLBuilderBridge.OnResetBuilderScene += OnResetBuilderScene; |
| 1 | 25 | | DCLBuilderBridge.OnPreviewModeChanged += OnPreviewModeChanged; |
| | 26 | | } |
| 1 | 27 | | isGameObjectActive = true; |
| 1 | 28 | | } |
| | 29 | |
|
| | 30 | | private void OnDisable() |
| | 31 | | { |
| 1 | 32 | | isGameObjectActive = false; |
| 1 | 33 | | DCLBuilderEntity.OnEntityAddedWithTransform -= OnEntityAdded; |
| 1 | 34 | | DCLBuilderEntity.OnEntityShapeUpdated -= OnShapeUpdated; |
| 1 | 35 | | DCLBuilderBridge.OnResetBuilderScene -= OnResetBuilderScene; |
| 1 | 36 | | DCLBuilderBridge.OnPreviewModeChanged -= OnPreviewModeChanged; |
| 1 | 37 | | } |
| | 38 | |
|
| | 39 | | public void Init() |
| | 40 | | { |
| 3 | 41 | | indicatorsAvailable = new Queue<DCLBuilderMeshLoadIndicator>(); |
| 3 | 42 | | indicatorsInUse = new List<DCLBuilderMeshLoadIndicator>(); |
| 3 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Dispose() |
| | 46 | | { |
| 20 | 47 | | if (indicatorsAvailable == null || indicatorsInUse == null) |
| 18 | 48 | | return; |
| | 49 | |
|
| 6 | 50 | | foreach (DCLBuilderMeshLoadIndicator indicator in indicatorsAvailable) |
| | 51 | | { |
| 1 | 52 | | Destroy(indicator.gameObject); |
| | 53 | | } |
| | 54 | |
|
| 6 | 55 | | foreach (DCLBuilderMeshLoadIndicator indicator in indicatorsInUse) |
| | 56 | | { |
| 1 | 57 | | Destroy(indicator.gameObject); |
| | 58 | | } |
| 2 | 59 | | } |
| | 60 | |
|
| | 61 | | private void OnEntityAdded(DCLBuilderEntity entity) |
| | 62 | | { |
| 1 | 63 | | if (!entity.HasShape() && !isPreviewMode) |
| | 64 | | { |
| 1 | 65 | | ShowIndicator(entity.transform.position, entity.rootEntity.entityId); |
| | 66 | | } |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | private void OnShapeUpdated(DCLBuilderEntity entity) |
| | 70 | | { |
| 1 | 71 | | if (!isPreviewMode) |
| | 72 | | { |
| 1 | 73 | | HideIndicator(entity.rootEntity.entityId); |
| | 74 | | } |
| 1 | 75 | | } |
| | 76 | |
|
| 2 | 77 | | private void OnResetBuilderScene() { HideAllIndicators(); } |
| | 78 | |
|
| | 79 | | private void OnPreviewModeChanged(bool isPreview) |
| | 80 | | { |
| 0 | 81 | | isPreviewMode = isPreview; |
| 0 | 82 | | if (isPreview) |
| | 83 | | { |
| 0 | 84 | | HideAllIndicators(); |
| | 85 | | } |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | public DCLBuilderMeshLoadIndicator ShowIndicator(Vector3 position, string entityId) |
| | 89 | | { |
| | 90 | | DCLBuilderMeshLoadIndicator ret; |
| | 91 | |
|
| 8 | 92 | | if (indicatorsAvailable == null) |
| 4 | 93 | | return null; |
| | 94 | |
|
| 4 | 95 | | if (indicatorsAvailable.Count > 0) |
| | 96 | | { |
| 1 | 97 | | ret = indicatorsAvailable.Dequeue(); |
| 1 | 98 | | ret.transform.position = position; |
| 1 | 99 | | } |
| | 100 | | else |
| | 101 | | { |
| 3 | 102 | | ret = Object.Instantiate(baseIndicator, position, Quaternion.identity, transform); |
| | 103 | | } |
| | 104 | |
|
| 4 | 105 | | ret.loadingEntityId = entityId; |
| 4 | 106 | | ret.gameObject.SetActive(true); |
| 4 | 107 | | indicatorsInUse.Add(ret); |
| 4 | 108 | | return ret; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | public void HideIndicator(string entityId) |
| | 112 | | { |
| 6 | 113 | | if (indicatorsInUse == null) |
| 3 | 114 | | return; |
| | 115 | |
|
| 6 | 116 | | for (int i = 0; i < indicatorsInUse.Count; i++) |
| | 117 | | { |
| 3 | 118 | | if (indicatorsInUse[i].loadingEntityId == entityId) |
| | 119 | | { |
| 3 | 120 | | indicatorsInUse[i].gameObject.SetActive(false); |
| 3 | 121 | | indicatorsAvailable.Enqueue(indicatorsInUse[i]); |
| 3 | 122 | | indicatorsInUse.RemoveAt(i); |
| 3 | 123 | | break; |
| | 124 | | } |
| | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | public void HideAllIndicators() |
| | 129 | | { |
| 1 | 130 | | if (indicatorsInUse == null) |
| 0 | 131 | | return; |
| | 132 | |
|
| 2 | 133 | | for (int i = 0; i < indicatorsInUse.Count; i++) |
| | 134 | | { |
| 0 | 135 | | indicatorsInUse[i].gameObject.SetActive(false); |
| 0 | 136 | | indicatorsAvailable.Enqueue(indicatorsInUse[i]); |
| | 137 | | } |
| 1 | 138 | | indicatorsInUse.Clear(); |
| 1 | 139 | | } |
| | 140 | | } |
| | 141 | | } |