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