The suspension highlighting status such as Unity Button does not reset (Selectable)

In Unity 2018.4 and below, when the UI is hidden and displayed again in hover status, the highlighted status does not reset, most of which are Button.

Problem description

UGUI button A, which hides itself when clicked( SetActive(false) )。 Later, it was turned on by other means, and it was found that A still remained highlighted and did not reset.

This is an occasional bug. For cloned objects+custom EventSystem event distribution components, the frequency is slightly higher.

For those who use UGUI native EventSystem Navigation Change to None A similar problem can be solved, but this is not the case discussed in this article.

Causes

Because UGUI is open source, you can directly browse the UGUI source code.

stay Selectable.cs In, there is such a paragraph:

 // Select on enable and add to the list. protected override void OnEnable() { base.OnEnable(); s_List.Add(this); var state = SelectionState.Normal; // The button will be highlighted even in some cases where it shouldn't. // For example: We only want to set the State as Highlighted if the StandaloneInputModule.m_CurrentInputMode == InputMode. Buttons // But we dont have access to this,  and it might not apply to other InputModules. // TODO: figure out how to solve this.  Case 617348. if (hasSelection) state = SelectionState.Highlighted; m_CurrentSelectionState = state; InternalEvaluateAndTransitionToSelectionState(true); }

The TODO in the notes is a bit interesting.

Continue to follow. After setting the state, InternalEvaluateAndTransitionToSelectionState The method of setting state will be called DoStateTransition

 var transitionState = m_CurrentSelectionState; if (IsActive() && ! IsInteractable()) transitionState = SelectionState.Disabled; DoStateTransition(transitionState, instant);

So, in some cases hasSelection It is not reset to false, so that the UI inherited from Selectable remains highlighted when it is enabled.

Check hasSelection Reference of:

 public virtual void OnSelect(BaseEventData eventData) { hasSelection = true; EvaluateAndTransitionToSelectionState(eventData); }
 public virtual void OnDeselect(BaseEventData eventData) { hasSelection = false; EvaluateAndTransitionToSelectionState(eventData); }

The problem is clear. UI objects are hidden in the Hover state, and in some cases they will not be triggered OnDeselect , especially for custom EventSystem event distributors.

After understanding these, look at the TODO comments in OnEnable.

resolvent

about Confirm that you want to reset the status on shutdown Selectable UI of, which can be called manually OnDeselect method:

 private void OnDisable(){ mBtn.OnDeselect(null); }
Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/7922.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*