| | 1 | | using MainScripts.DCL.Helpers.Utils; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Pool; |
| | 4 | |
|
| | 5 | | namespace DCLServices.MapRendererV2.CommonBehavior |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Represents Poolable behaviour of the map object |
| | 9 | | /// </summary> |
| | 10 | | internal struct MapMarkerPoolableBehavior<T> where T : MonoBehaviour |
| | 11 | | { |
| | 12 | | internal readonly IUnityObjectPool<T> objectsPool; |
| | 13 | |
|
| 5760 | 14 | | internal T instance { get; private set; } |
| | 15 | |
|
| 5760 | 16 | | internal bool isVisible { get; private set; } |
| | 17 | |
|
| 0 | 18 | | internal Vector3 currentPosition { get; private set; } |
| | 19 | |
|
| 5760 | 20 | | internal MapMarkerPoolableBehavior(IUnityObjectPool<T> objectsPool) : this() |
| | 21 | | { |
| 5760 | 22 | | this.objectsPool = objectsPool; |
| 5760 | 23 | | } |
| | 24 | |
|
| | 25 | | public void SetCurrentPosition(Vector3 pos) |
| | 26 | | { |
| 0 | 27 | | currentPosition = pos; |
| | 28 | |
|
| 0 | 29 | | if (isVisible) |
| 0 | 30 | | instance.transform.localPosition = pos; |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | public T OnBecameVisible() |
| | 34 | | { |
| 0 | 35 | | instance = objectsPool.Get(); |
| 0 | 36 | | instance.transform.localPosition = currentPosition; |
| 0 | 37 | | isVisible = true; |
| 0 | 38 | | return instance; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public void OnBecameInvisible() |
| | 42 | | { |
| 5760 | 43 | | if (instance) |
| | 44 | | { |
| 0 | 45 | | objectsPool.Release(instance); |
| 0 | 46 | | instance = null; |
| | 47 | | } |
| | 48 | |
|
| 5760 | 49 | | isVisible = false; |
| 5760 | 50 | | } |
| | 51 | | } |
| | 52 | | } |