Search code examples
unity-game-engineaugmented-realityhololensmrtk

Unity - MRTK - HoloLens: Modify Collider of 2D-Buttons so the Cursor gets closer


My current problem is that the cursor appears too far away from a button. You can see in the screenshot what I mean. Hovering over a button from the list lookes like this:

enter image description here

Question: What can I do so the cursor get closer to the button, because on the HoloLens you see the distance?

Looking somewhere else on the canvas except the buttons, the cursor gets closer:

enter image description here

--Edit--
I should mention that the scene has a scaled cube (the gray thing in the screenshot) and in front of that a world canvas (white thing), which contains the scrollview/list.


Solution

  • I saw the same behaviour for UI elements.

    I can only provide you a workaround. It is a bit hacky but it works:

    1. Go through all UI elements especially Text and Image and disable the option RayCast Target.

      enter image description here

      This makes the Cursor be sitting right on top of them ... but you will notice your Buttons are now non-responsive and you can not interact with them anymore.

      enter image description here

      This happens because the Physics system requires either a RayCastTarget or a Collider in order to fire it's pointer events like e.g. PointerEnter, PointerDown etc.

    2. Therefore now add a BoxCollider (not BoxCollider2D!) to your Buttons and scale it to the correct size. It looks like you are using a VerticalLayoutGroup so you can simply correct the positioning of the BoxCollider by setting the RectTransform to centered once (the VerticalLayoutgroup will anyway re-enforce the Top-Left anchoring). In my case the BoxCollider needs with 0.8 and height 0.1 ... and for the z I choose 0.01 but it can be smaller if you whish

      enter image description here

    Hurray, now the buttons are interactable again and the Cursor only has it's usual distance + the half of the choosen z thikness of the BoxColliders.

    enter image description here

    Since the Background cube has it's own BoxCollider anyway we don't need to add further Colliders for the ScrollView and UI panels.

    You might have to add some though for the ScrollBars as well if you need them!


    As said this is more like a quick workaround and might not be a final solution since whenever a size of the Button or the ScrollRect is changed you have to rework those hardcoded BoxCollider dimesnions as well ...