Search Unity

Question UI Appears only When Moving the mouse Slightly

Discussion in 'UI Toolkit' started by Entchenklein, Sep 25, 2021.

  1. Entchenklein

    Entchenklein

    Joined:
    Feb 25, 2016
    Posts:
    12

    The UI Only Appears whenThe mouse is Slightly getting moved

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5. using Cursor = UnityEngine.Cursor;
    6.  
    7. public class MainManager : MonoBehaviour
    8. {
    9.     private Button nag;
    10.     private VisualElement rootVisualElement;
    11.  
    12.     [SerializeField]
    13.     private GameObject mainUI;
    14.     private void OnEnable()
    15.     {
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.         Cursor.visible = false;
    18.  
    19.         rootVisualElement = mainUI.GetComponent<UIDocument>().rootVisualElement;
    20.         //rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
    21.  
    22.         nag = rootVisualElement.Q<Button>("NAG");
    23.  
    24.         nag.RegisterCallback<ClickEvent>(ev => sayNag());
    25.     }
    26.  
    27.     private void sayNag()
    28.     {
    29.         Debug.Log("NAG");
    30.     }
    31.  
    32.     private void Update()
    33.     {
    34.         if (Input.GetKeyDown(KeyCode.E))
    35.         {
    36.             rootVisualElement.MarkDirtyRepaint();
    37.  
    38.             if (Cursor.lockState == CursorLockMode.None)
    39.             {
    40.                 Cursor.lockState = CursorLockMode.Locked;
    41.                 Cursor.visible = false;
    42.  
    43.                 //mainUI.SetActive(false);
    44.                 rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
    45.             }
    46.             else
    47.             {
    48.                 Cursor.lockState = CursorLockMode.None;
    49.                 Cursor.visible = true;
    50.                 //mainUI.SetActive(true);
    51.                 rootVisualElement.panel.visualTree.style.display = DisplayStyle.Flex;
    52.             }
    53.             //Canvas.ForceUpdateCanvases();
    54.             //rootVisualElement.Focus();
    55.         }
    56.     }
    57. }
    58.  
    UI Builder and UI Toolkit are on Version 1.0.0-preview.17

    So basically i have no idear how to make the UI Instantly appear i found rootVisualElement.MarkDirtyRepaint(); witch helps sometimes but not really

    Help would be Apreciated Thanks in advance
    Entchenklein
     
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi Entchenklein!

    Does moving the code from Update() to rootVisualElement.RegisterCallback<KeyDownEvent>() help ?
     
  3. Entchenklein

    Entchenklein

    Joined:
    Feb 25, 2016
    Posts:
    12
    Thank you very mutch hugobd

    This fixes my problem however the KeyDownEvent fires Twice witch i currently prevent by using the once boolean in my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5. using Cursor = UnityEngine.Cursor;
    6.  
    7. public class MainManager : MonoBehaviour
    8. {
    9.     private VisualElement container;
    10.     private VisualElement rootVisualElement;
    11.  
    12.     [SerializeField]
    13.     private GameObject mainUI;
    14.     private void OnEnable()
    15.     {
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.         Cursor.visible = false;
    18.  
    19.         rootVisualElement = mainUI.GetComponent<UIDocument>().rootVisualElement;
    20.         //rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
    21.  
    22.         rootVisualElement.RegisterCallback<KeyDownEvent>(OnKeyDown);
    23.         container = rootVisualElement.Q<VisualElement>("Inventory");
    24.  
    25.         //nag.RegisterCallback<ClickEvent>(ev => sayNag());
    26.     }
    27.  
    28.     private void sayNag()
    29.     {
    30.         Debug.Log("NAG");
    31.     }
    32.  
    33.     bool once = true;
    34.     void OnKeyDown(KeyDownEvent ev)
    35.     {
    36.         if (once)
    37.         {
    38.             once = false;
    39.         }
    40.         else
    41.         {
    42.             once = true;
    43.             return;
    44.         }
    45.         Debug.Log("NAG1");
    46.         if (Input.GetKeyDown(KeyCode.E))
    47.         {
    48.             //rootVisualElement.MarkDirtyRepaint();
    49.             Debug.Log("ASD");
    50.  
    51.             if (container.style.display == DisplayStyle.Flex)
    52.             {
    53.                 Debug.Log("HIDDEN");
    54.  
    55.                 Cursor.lockState = CursorLockMode.Locked;
    56.                 Cursor.visible = false;
    57.  
    58.                 //mainUI.SetActive(false);
    59.                 //rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
    60.  
    61.                 //container.style.visibility = Visibility.Hidden;
    62.                 container.style.display = DisplayStyle.None;
    63.             }
    64.             else
    65.             {
    66.                 Debug.Log("VISIBLE");
    67.  
    68.                 Cursor.lockState = CursorLockMode.None;
    69.                 Cursor.visible = true;
    70.                 //mainUI.SetActive(true);
    71.                 //container.style.visibility = Visibility.Visible;
    72.                 container.style.display = DisplayStyle.Flex;
    73.  
    74.  
    75.                 //rootVisualElement.panel.visualTree.style.display = DisplayStyle.Flex;
    76.             }
    77.             //Canvas.ForceUpdateCanvases();
    78.             //rootVisualElement.Focus();
    79.         }
    80.     }
    81. }
     
    Last edited: Sep 28, 2021
  4. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Glad it help!
    Is it sending the same key twice ? You can also find the Keycode from the event like so ev.keyCode.
     
  5. Entchenklein

    Entchenklein

    Joined:
    Feb 25, 2016
    Posts:
    12
    Thank you for all the help

    Its not the same key when i press for example E it will have the keycode E and another with keycode None

    Well this code now works fine its just weard to me it fires a second time so if there is a solution for that aswell i would be gratefull but it doesnt matter that mutch

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5. using Cursor = UnityEngine.Cursor;
    6.  
    7. public class MainManager : MonoBehaviour
    8. {
    9.     private VisualElement container;
    10.     private VisualElement rootVisualElement;
    11.  
    12.     [SerializeField]
    13.     private GameObject mainUI;
    14.     private void OnEnable()
    15.     {
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.         Cursor.visible = false;
    18.  
    19.         rootVisualElement = mainUI.GetComponent<UIDocument>().rootVisualElement;
    20.         //rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
    21.  
    22.         rootVisualElement.RegisterCallback<KeyDownEvent>(OnKeyDown);
    23.         container = rootVisualElement.Q<VisualElement>("Inventory");
    24.  
    25.         //nag.RegisterCallback<ClickEvent>(ev => sayNag());
    26.     }
    27.  
    28.     private void sayNag()
    29.     {
    30.         Debug.Log("NAG");
    31.     }
    32.  
    33.     bool once = true;
    34.     void OnKeyDown(KeyDownEvent ev)
    35.     {
    36.         Debug.Log("KEY:" + ev.keyCode);
    37.  
    38.         if (ev.keyCode == KeyCode.E)
    39.         {
    40.             //rootVisualElement.MarkDirtyRepaint();
    41.  
    42.             if (container.style.display == DisplayStyle.Flex)
    43.             {
    44.                 Cursor.lockState = CursorLockMode.Locked;
    45.                 Cursor.visible = false;
    46.  
    47.                 //mainUI.SetActive(false);
    48.                 //rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
    49.  
    50.                 //container.style.visibility = Visibility.Hidden;
    51.                 container.style.display = DisplayStyle.None;
    52.             }
    53.             else
    54.             {
    55.                 Cursor.lockState = CursorLockMode.None;
    56.                 Cursor.visible = true;
    57.                 //mainUI.SetActive(true);
    58.                 //container.style.visibility = Visibility.Visible;
    59.                 container.style.display = DisplayStyle.Flex;
    60.  
    61.  
    62.                 //rootVisualElement.panel.visualTree.style.display = DisplayStyle.Flex;
    63.             }
    64.             //Canvas.ForceUpdateCanvases();
    65.             //rootVisualElement.Focus();
    66.         }
    67.     }
    68. }
    69.  
     
    HugoBD-Unity likes this.