Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Textmesh Pro Input Field deselect on enter key?

I have an input field with some important handlers on selection and deselection. There are its settings:

enter image description here

Nothing else was changed; I do not include these handlers because the problem is reproducible without them.


Expectation

When I press Enter, the input field confirms input, deselects and calls the event. The same applies to Esc key except that the input should be cancelled.

Reality

When I press Enter, the input field seems to stay selected, event handler is not called, I can't type text though, and if I press Enter again, the input field seems to activate again (it selects all text and I have On Focus - Select All enabled). Same problem is with Esc key.


I figured out that On End Edit event gets called upon Enter press, but how to deselect the input field? It should call On Deselect event and should not actvate again on Enter. I tried to call DeactivateInputField, it didn't work.

like image 316
trollingchar Avatar asked Oct 15 '22 15:10

trollingchar


1 Answers

New answer, solved the issue, see comments for clarification:

TextMesh Pro uses Unity's built in EventSystem. For this reason, de-select and the like are done through the EventSystem:

EventSystem.current.SetSelectedGameObject(null);

Thus the "OnDeselect" event is called through EventSystem's selection management, not TextMesh Pro.

When you select something new with the event system, you also need to make sure that something else is not currently selected in a way that locks. There is a protection/lock in EventSystem, which prevents setting no object selected in this case, but it prints an error:

Attempting to select while already selecting an object.
UnityEngine.EventSystems.EventSystem:SetSelectedGameObject(GameObject)

You should check if it's already selecting another object:

var eventSystem = EventSystem.current;
if (!eventSystem.alreadySelecting) eventSystem.SetSelectedGameObject (null);

Other post where they discuss the 'lock' part of the EventSystem.




Old answer, didn't solve issue but useful for understanding comment trail below:

My understanding

I'm not experienced with TextMesh Pro, so I cannot for sure say how the cycle of events is when you hit the Enter key.

However, you explain that it at the very least calls the "On End Edit" method. I'd look through their documentation and test what other methods it calls, if I was you. Because this should help you get a better overview of what goes on. Which methods are called and so on.

What to do?

The TextMesh Pro documentation shows various methods available, for various parts of its lifecycle/events.

Documentation

One of them is

public void DeactivateInputField()

Another is

protected override void DoStateTransition(SelectionState state, bool instant)

I suspect one of these could be used for deactivating the field as you intend. If you call them manually at the right time.

An example of how to check the locking state of the currently dealt with event, can be seen in this link: https://answers.unity.com/questions/1315276/event-system-setselectedgameobject-error-but-code.html


Screenshot from documentation

Image from documentation

like image 133
Doh09 Avatar answered Oct 21 '22 04:10

Doh09