| | 1 | | using DCL.Configuration; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.UI; |
| | 10 | |
|
| | 11 | | namespace DCL.Components |
| | 12 | | { |
| | 13 | | public class UIScreenSpace : UIShape |
| | 14 | | { |
| | 15 | | static bool VERBOSE = false; |
| | 16 | |
|
| | 17 | | public Canvas canvas; |
| | 18 | | public GraphicRaycaster graphicRaycaster; |
| | 19 | |
|
| | 20 | | private DCLCharacterPosition currentCharacterPosition; |
| | 21 | | private CanvasGroup canvasGroup; |
| | 22 | |
|
| 44 | 23 | | public UIScreenSpace() |
| | 24 | | { |
| 44 | 25 | | DCLCharacterController.OnCharacterMoved += OnCharacterMoved; |
| 44 | 26 | | model = new Model(); |
| 44 | 27 | | } |
| | 28 | |
|
| 0 | 29 | | public override int GetClassId() { return (int) CLASS_ID.UI_SCREEN_SPACE_SHAPE; } |
| | 30 | |
|
| | 31 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 32 | | { |
| 0 | 33 | | Debug.LogError( |
| | 34 | | "Aborted UIScreenShape attachment to an entity. UIShapes shouldn't be attached to entities."); |
| 0 | 35 | | } |
| | 36 | |
|
| 0 | 37 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 38 | |
|
| | 39 | | private bool initialized = false; |
| | 40 | |
|
| | 41 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 42 | | { |
| 52 | 43 | | if (!initialized) |
| | 44 | | { |
| 44 | 45 | | InitializeCanvas(); |
| 44 | 46 | | initialized = true; |
| 44 | 47 | | } |
| 8 | 48 | | else if (DCLCharacterController.i != null) |
| | 49 | | { |
| 8 | 50 | | OnCharacterMoved(DCLCharacterController.i.characterPosition); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | //We have to wait a frame for the Canvas Scaler to act |
| 52 | 54 | | yield return null; |
| 52 | 55 | | } |
| | 56 | |
|
| | 57 | | public override void Dispose() |
| | 58 | | { |
| 63 | 59 | | DCLCharacterController.OnCharacterMoved -= OnCharacterMoved; |
| 63 | 60 | | CommonScriptableObjects.allUIHidden.OnChange -= AllUIHidden_OnChange; |
| | 61 | |
|
| 63 | 62 | | if (childHookRectTransform != null) |
| | 63 | | { |
| 44 | 64 | | Utils.SafeDestroy(childHookRectTransform.gameObject); |
| | 65 | | } |
| 63 | 66 | | } |
| | 67 | |
|
| | 68 | | void OnCharacterMoved(DCLCharacterPosition newCharacterPosition) |
| | 69 | | { |
| 135 | 70 | | if (canvas == null) |
| 0 | 71 | | return; |
| | 72 | |
|
| 135 | 73 | | currentCharacterPosition = newCharacterPosition; |
| | 74 | |
|
| 135 | 75 | | UpdateCanvasVisibility(); |
| | 76 | |
|
| 135 | 77 | | if (VERBOSE) |
| | 78 | | { |
| 0 | 79 | | Debug.Log($"set screenspace = {currentCharacterPosition}"); |
| | 80 | | } |
| 135 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | private void AllUIHidden_OnChange(bool current, bool previous) { UpdateCanvasVisibility(); } |
| | 84 | |
|
| | 85 | | private void UpdateCanvasVisibility() |
| | 86 | | { |
| 135 | 87 | | if (canvas == null || scene == null) |
| 0 | 88 | | return; |
| | 89 | |
|
| 135 | 90 | | var model = (Model) this.model; |
| | 91 | |
|
| 135 | 92 | | bool isInsideSceneBounds = scene.IsInsideSceneBoundaries(Utils.WorldToGridPosition(currentCharacterPosition. |
| 135 | 93 | | bool shouldBeVisible = scene.isPersistent || (model.visible && isInsideSceneBounds && !CommonScriptableObjec |
| | 94 | |
|
| 135 | 95 | | canvasGroup.alpha = shouldBeVisible ? 1f : 0f; |
| 135 | 96 | | canvasGroup.blocksRaycasts = shouldBeVisible; |
| 135 | 97 | | } |
| | 98 | |
|
| | 99 | | void InitializeCanvas() |
| | 100 | | { |
| 44 | 101 | | if (VERBOSE) |
| | 102 | | { |
| 0 | 103 | | Debug.Log("Started canvas initialization in " + id); |
| | 104 | | } |
| | 105 | |
|
| 44 | 106 | | GameObject canvasGameObject = new GameObject("UIScreenSpace"); |
| 44 | 107 | | canvasGameObject.layer = LayerMask.NameToLayer("UI"); |
| 44 | 108 | | canvasGameObject.transform.SetParent(scene.GetSceneTransform()); |
| 44 | 109 | | canvasGameObject.transform.ResetLocalTRS(); |
| | 110 | |
|
| | 111 | | // Canvas |
| 44 | 112 | | canvas = canvasGameObject.AddComponent<Canvas>(); |
| 44 | 113 | | canvas.renderMode = RenderMode.ScreenSpaceOverlay; |
| | 114 | |
|
| | 115 | | // Canvas Scaler (for maintaining ui aspect ratio) |
| 44 | 116 | | CanvasScaler canvasScaler = canvasGameObject.AddComponent<CanvasScaler>(); |
| 44 | 117 | | canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; |
| 44 | 118 | | canvasScaler.referenceResolution = new Vector2(1280f, 720f); |
| 44 | 119 | | canvasScaler.matchWidthOrHeight = 1f; // Match height, recommended for landscape projects |
| | 120 | |
|
| | 121 | | // Graphics Raycaster (for allowing touch/click input on the ui components) |
| 44 | 122 | | graphicRaycaster = canvasGameObject.AddComponent<GraphicRaycaster>(); |
| | 123 | |
|
| 44 | 124 | | canvas.sortingOrder = -1; |
| | 125 | |
|
| | 126 | | // We create a middleman-gameobject to change the size of the parcel-devs accessible canvas, to have its bot |
| 44 | 127 | | GameObject resizedPanel = new GameObject("ResizeUIArea"); |
| | 128 | |
|
| 44 | 129 | | resizedPanel.AddComponent<CanvasRenderer>(); |
| 44 | 130 | | childHookRectTransform = resizedPanel.AddComponent<RectTransform>(); |
| 44 | 131 | | childHookRectTransform.SetParent(canvas.transform); |
| 44 | 132 | | childHookRectTransform.ResetLocalTRS(); |
| | 133 | |
|
| 44 | 134 | | childHookRectTransform.anchorMin = Vector2.zero; |
| 44 | 135 | | childHookRectTransform.anchorMax = new Vector2(1, 0); |
| | 136 | |
|
| | 137 | | // We scale the panel downwards to subtract the viewport's top 10% |
| 44 | 138 | | float canvasHeight = canvasScaler.referenceResolution.y; |
| 44 | 139 | | childHookRectTransform.pivot = new Vector2(0.5f, 0f); |
| 44 | 140 | | float canvasSubtraction = canvasHeight * UISettings.RESERVED_CANVAS_TOP_PERCENTAGE / 100; |
| 44 | 141 | | childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction); |
| | 142 | |
|
| | 143 | | // We scale the panel upwards to subtract the viewport's bottom 5% for Decentraland's taskbar |
| 44 | 144 | | canvasHeight = childHookRectTransform.sizeDelta.y; |
| 44 | 145 | | childHookRectTransform.pivot = new Vector2(0.5f, 1f); |
| 44 | 146 | | childHookRectTransform.anchoredPosition = new Vector3(0, canvasHeight, 0f); |
| 44 | 147 | | childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction / 2); |
| | 148 | |
|
| | 149 | | // Canvas group |
| 44 | 150 | | canvasGroup = canvas.gameObject.AddComponent<CanvasGroup>(); |
| 44 | 151 | | canvasGroup.alpha = 0f; // Alpha will be updated later when the player enters this scene |
| 44 | 152 | | canvasGroup.blocksRaycasts = false; |
| | 153 | |
|
| 44 | 154 | | if (VERBOSE) |
| | 155 | | { |
| 0 | 156 | | Debug.Log("canvas initialized, width: " + childHookRectTransform.rect.width); |
| 0 | 157 | | Debug.Log("canvas initialized, height: " + childHookRectTransform.rect.height); |
| | 158 | | } |
| | 159 | |
|
| 44 | 160 | | if (DCLCharacterController.i != null) |
| | 161 | | { |
| 44 | 162 | | OnCharacterMoved(DCLCharacterController.i.characterPosition); |
| | 163 | | } |
| | 164 | |
|
| 44 | 165 | | if (VERBOSE) |
| | 166 | | { |
| 0 | 167 | | Debug.Log("Finished canvas initialization in " + id); |
| | 168 | | } |
| | 169 | |
|
| 44 | 170 | | if (!scene.isPersistent) |
| | 171 | | { |
| 0 | 172 | | UpdateCanvasVisibility(); |
| 0 | 173 | | CommonScriptableObjects.allUIHidden.OnChange += AllUIHidden_OnChange; |
| | 174 | | } |
| 44 | 175 | | } |
| | 176 | | } |
| | 177 | | } |