7

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.

trollingchar
  • 760
  • 2
  • 6
  • 18

1 Answers1

4

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

Doh09
  • 2,324
  • 1
  • 16
  • 31
  • Unfortunately this does not answer my question. Docs show only methods signatures, with stub descriptions or none at all. I've explicitly stated that I tried `DeactivateInputField` and it did not work. `DoStateTransition` as I know is a method of `Selectable` that only affects visual state (Normal-Highlighted-Pressed-Disabled). – trollingchar May 15 '19 at 10:06
  • I see, must have missed the DeactivateInputField part in your description. Have you considered that maybe an event is called after you call DeactivateInputField, reactivating it again? Another thing is, maybe "deactivate" is not the same as "deselect". – Doh09 May 15 '19 at 10:13
  • Hm, maybe it reactivates later. I searched for method `Deselect` but found none. – trollingchar May 15 '19 at 10:15
  • If TextMesh Pro uses Unity's event system. You could try grabbing the event system and then deselecting the field through that. – Doh09 May 15 '19 at 10:17
  • 1
    This is better. It works but gives error when I try to select another field: Attempting to select while already selecting an object. – trollingchar May 15 '19 at 10:26
  • Sounds like the event system needs to be "cleared" after you deselect? So it's in a state ready to be used. I found this explanation saying it gets "locked" on selecting something, likely to deal with concurrency issues: https://answers.unity.com/questions/1315276/event-system-setselectedgameobject-error-but-code.html You could try manually unlock it and see if that allows you to select something anew. – Doh09 May 15 '19 at 10:30
  • 1
    Thank you, it works. And that's how: when we select another object, `SetSelectedGameObject` takes it as a parameter, sets lock, and after that a deselection event fires, so I check EventSystem.current.alreadySelecting to avoid error. Now that's the good answer, post it please, I will accept and upvote. – trollingchar May 15 '19 at 10:38
  • Done. You may want to add code and links to an edit of your question. With examples. So people can understand more easily how you did it. – Doh09 May 15 '19 at 10:45