Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Unity UI World Space Canvas + CursorLockMode.Locked incompatible?

Discussion in 'UGUI & TextMesh Pro' started by mrmcduck, Oct 9, 2018.

  1. mrmcduck

    mrmcduck

    Joined:
    Nov 26, 2015
    Posts:
    26
    Hello fellow Unity devs!

    I am working on a immersive UI system for a first person game which displays as much UI elements as possible on in-game computer screens. Obviously the mouse controls looking around, and therefore i'd like to use the center of the screen as mouse cursor for the UI elements when the player stands in front of a screen with a world space canvas.

    Sadly this seems impossible, according to a official unity post:
    "[...] Thus we have removed the interactability of a locked cursor with the UI. To simulate this behavor you would need to derive from StandaloneInputModule and override the GetMousePointerEventData function to force the position to anything you want. [...]"
    Source: https://forum.unity.com/threads/world-gui-locked-cursor-doesnt-work-anymore.418363/

    There are a few workarounds including in the statement above, but they all don't seem to be an option, because simulating the mouse cursor position by setting it manually in a custom InputModule is only a very small part of the issues. I need standard UI event system behaviors, e.g. Drag&Drop-, PointerIn-, PointerExit-, ... events.

    Am I missing something or is this just impossible - unless I rewrite the whole UI system?
     
  2. 00jknight

    00jknight

    Joined:
    Mar 28, 2014
    Posts:
    34
  3. DeveloperJake

    DeveloperJake

    Joined:
    May 25, 2020
    Posts:
    11
    I have the same problem, considering this is 2 years old and I'm using unity 2022, I don't think they plan on patching this any time soon
     
  4. JJTheOwO

    JJTheOwO

    Joined:
    Oct 18, 2020
    Posts:
    1
    I have found a solution, and it's extremely jank, but works (based on very limited testing, it might impact other systems, but oh well)

    First, you need to create a new script to make the cursor work, keep it disabled when not in locked mode, because it forces locked mode on (use singleton if you want or something).

    The script is just this:
    Code (CSharp):
    1. Class WorldSpaceUIEnabler {
    2.     private void Update() {
    3.         Cursor.lockState = CursorLockMode.Confined;
    4.     }
    5.  
    6.     private void LateUpdate() {
    7.         Cursor.lockState = CursorLockMode.Locked;
    8.     }
    9. }
    What's important is the execution order
    in Project Settings > Script Execution Order
    add this script above UnityEngine.EventSystems.EventSystem (earlier)
    and there you go, whenever this script is enabled, the cursor will be in locked mode, but still be able to interact with World Space UI.

    If there are issues, then oh well, not something i encountered in the 5minutes since finding this out.

    UPDATE: forgot to check if it works with sliders, it does surprisingly well
     
    Last edited: May 28, 2022
  5. coolcatcoder

    coolcatcoder

    Joined:
    Jun 29, 2022
    Posts:
    1
    Sorry for necrobumping, but I can't seem to get this working in the 2021 lts with the new input system, does this still work, or is there a new workaround?

    Edit: I have found that by having both the new and old input systems enabled, but using the old Standalone Input Module on the event system that it works fine! Yay!
     
    Last edited: Nov 26, 2022 at 2:48 AM
unityunity