简体   繁体   English

Unity UI按钮具有疯狂的过渡状态行为-单击后仍突出显示

[英]Unity UI Button has insane transition state behaviour - it remains highlighted after being clicked

It took me a while to figure out the problem with Unity UI Button Transition: 我花了一段时间才弄清楚Unity UI Button Transition的问题:

Problem: I hover on the button object, it goes to highlighted state, that's Fine. 问题:我将鼠标悬停在按钮对象上,它进入突出显示状态,这很好。 If I press mouse on button and it goes to pressed state then I move mouse outside of button so its no longer over button. 如果我在按钮上按下鼠标,鼠标进入按下状态,那么我将鼠标移出按钮,使其不再位于按钮上方。 The button goes to highlighted state instead of normal state. 该按钮将转为突出显示状态,而不是正常状态。 I need to click in empty space to get the normal state of button. 我需要在空白处单击以获取按钮的正常状态。

TLDR: TLDR:

This is the default behaviour for a Button element in Unity - it retains focus after the initial interaction, causing it to show the Highlighted Color. 这是Unity中Button元素的默认行为-初始交互后它会保留焦点,从而使其显示突出显示的颜色。 Clicking away clears the focus, so it no longer becomes highlighted then. 单击以清除焦点,因此焦点不再突出显示。

To change this behaviour, you can switch the Navigation setting. 要更改此行为,可以切换“ 导航”设置。

在此处输入图片说明

Currently, it's set to Automatic . 当前,它设置为Automatic According to the documentation , the option you want to use instead is None , which results in: 根据文档 ,您要使用的选项是None ,结果是:

No keyboard navigation. 没有键盘导航。 Also ensures that it does not receive focus from clicking/tapping on it. 还要确保它不会因单击/点击而获得焦点。

Hope this helps! 希望这可以帮助! Let me know if you have any questions. 如果您有任何疑问,请告诉我。

If you want to use keyboard navigation and also get rid of this problem you can add this function to update: 如果您想使用键盘导航并且也摆脱了这个问题,可以添加以下功能进行更新:

void Update()
{
    if (Input.GetMouseButtonUp(0))
    {
        EventSystem.current.SetSelectedGameObject(null);
    }
}

To retain keyboard automatic navigation, you probably want to inherit from IPointerExitHandler and deselect on exit: 要保留键盘自动导航,您可能想要继承自IPointerExitHandler并在退出时取消选择:

public void OnPointerExit(PointerEventData data)
{
    EventSystem.current.SetSelectedGameObject(null);
}

You could add checks to only deselect gameObject if already selected. 您可以添加检查以仅取消选择gameObject如果已选择)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM