The UI Only Appears whenThe mouse is Slightly getting moved
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Cursor = UnityEngine.Cursor;
public class MainManager : MonoBehaviour
{
private Button nag;
private VisualElement rootVisualElement;
[SerializeField]
private GameObject mainUI;
private void OnEnable()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
rootVisualElement = mainUI.GetComponent<UIDocument>().rootVisualElement;
//rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
nag = rootVisualElement.Q<Button>("NAG");
nag.RegisterCallback<ClickEvent>(ev => sayNag());
}
private void sayNag()
{
Debug.Log("NAG");
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
rootVisualElement.MarkDirtyRepaint();
if (Cursor.lockState == CursorLockMode.None)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//mainUI.SetActive(false);
rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
//mainUI.SetActive(true);
rootVisualElement.panel.visualTree.style.display = DisplayStyle.Flex;
}
//Canvas.ForceUpdateCanvases();
//rootVisualElement.Focus();
}
}
}
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
Hi Entchenklein!
Does moving the code from Update() to rootVisualElement.RegisterCallback() help ?
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Cursor = UnityEngine.Cursor;
public class MainManager : MonoBehaviour
{
private VisualElement container;
private VisualElement rootVisualElement;
[SerializeField]
private GameObject mainUI;
private void OnEnable()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
rootVisualElement = mainUI.GetComponent<UIDocument>().rootVisualElement;
//rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
rootVisualElement.RegisterCallback<KeyDownEvent>(OnKeyDown);
container = rootVisualElement.Q<VisualElement>("Inventory");
//nag.RegisterCallback<ClickEvent>(ev => sayNag());
}
private void sayNag()
{
Debug.Log("NAG");
}
bool once = true;
void OnKeyDown(KeyDownEvent ev)
{
if (once)
{
once = false;
}
else
{
once = true;
return;
}
Debug.Log("NAG1");
if (Input.GetKeyDown(KeyCode.E))
{
//rootVisualElement.MarkDirtyRepaint();
Debug.Log("ASD");
if (container.style.display == DisplayStyle.Flex)
{
Debug.Log("HIDDEN");
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//mainUI.SetActive(false);
//rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
//container.style.visibility = Visibility.Hidden;
container.style.display = DisplayStyle.None;
}
else
{
Debug.Log("VISIBLE");
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
//mainUI.SetActive(true);
//container.style.visibility = Visibility.Visible;
container.style.display = DisplayStyle.Flex;
//rootVisualElement.panel.visualTree.style.display = DisplayStyle.Flex;
}
//Canvas.ForceUpdateCanvases();
//rootVisualElement.Focus();
}
}
}
Glad it help!
Is it sending the same key twice ? You can also find the Keycode from the event like so ev.keyCode.
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Cursor = UnityEngine.Cursor;
public class MainManager : MonoBehaviour
{
private VisualElement container;
private VisualElement rootVisualElement;
[SerializeField]
private GameObject mainUI;
private void OnEnable()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
rootVisualElement = mainUI.GetComponent<UIDocument>().rootVisualElement;
//rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
rootVisualElement.RegisterCallback<KeyDownEvent>(OnKeyDown);
container = rootVisualElement.Q<VisualElement>("Inventory");
//nag.RegisterCallback<ClickEvent>(ev => sayNag());
}
private void sayNag()
{
Debug.Log("NAG");
}
bool once = true;
void OnKeyDown(KeyDownEvent ev)
{
Debug.Log("KEY:" + ev.keyCode);
if (ev.keyCode == KeyCode.E)
{
//rootVisualElement.MarkDirtyRepaint();
if (container.style.display == DisplayStyle.Flex)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//mainUI.SetActive(false);
//rootVisualElement.panel.visualTree.style.display = DisplayStyle.None;
//container.style.visibility = Visibility.Hidden;
container.style.display = DisplayStyle.None;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
//mainUI.SetActive(true);
//container.style.visibility = Visibility.Visible;
container.style.display = DisplayStyle.Flex;
//rootVisualElement.panel.visualTree.style.display = DisplayStyle.Flex;
}
//Canvas.ForceUpdateCanvases();
//rootVisualElement.Focus();
}
}
}
1 Like