Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How to Ignore a TextField an allow input through to TextField behind.

Discussion in 'UI Toolkit' started by JasonsFreeTime, Jul 28, 2023.

  1. JasonsFreeTime

    JasonsFreeTime

    Joined:
    May 23, 2015
    Posts:
    31
    Hello, please don't ask me why, but I have several TextFields stacked exactly on top of each other. I want to control which TextField the user can interact with via script. I don't want to reorder them in the hierarchy. (Note: there is a separate controller that will allow the user to select which TextField is currently editable.)

    Currently I'm having not luck as the top-most TextField blocks being able to interact with the below TextField.

    Here is some code that I have tried:

    Code (CSharp):
    1.  
    2. if (count > 1 && elementPosition < count)
    3.             {
    4.  
    5.                 empty.Children().ToList().ForEach(element =>
    6.                     {
    7.                         element.Q<TextField>("TextBoxTextField").pickingMode = PickingMode.Ignore;
    8.                         element.Q<TextField>("TextBoxTextField").focusable = false;
    9.                         element.Q<TextField>("TextBoxTextField").SetEnabled(false);
    10.                      }
    11.  
    12.                 empty.ElementAt(elementPosition).Q<TextField>("TextBoxTextField").pickingMode = PickingMode.Position;
    13.                 empty.ElementAt(elementPosition).Q<TextField>("TextBoxTextField").focusable = true;
    14.                 empty.ElementAt(elementPosition).Q<TextField>("TextBoxTextField").SetEnabled(true);
    15.                 empty.ElementAt(elementPosition).Q<TextField>("TextBoxTextField").Focus();
    16.             }
    17.  
    Any help is appreciated!
     
  2. JasonsFreeTime

    JasonsFreeTime

    Joined:
    May 23, 2015
    Posts:
    31
    Nevermind. I realized I had a parent of each TextField with picking mode set to position. With that said, to disable each of my TextFields and allow passing through of a mouse click, this is the code:
    Code (CSharp):
    1.  
    2. empty.Children().ToList().ForEach(element =>
    3.                     {
    4.                         if (element.Q<TextField>(name: "TextBoxTextField") is { } textBoxField)
    5.                         {
    6.                             textBoxField.pickingMode = PickingMode.Ignore;
    7.  
    8.                             if (textBoxField.Q(name: "unity-text-input") is { } element1)
    9.                             {
    10.                                 element1.pickingMode = PickingMode.Ignore;
    11.                                 if (element1.Q().ElementAt(0) is { } element2)
    12.                                 {
    13.                                     element2.pickingMode = PickingMode.Ignore;
    14.                                     if (element2.Q().ElementAt(0) is { } element3)
    15.                                     {
    16.                                         element3.pickingMode = PickingMode.Ignore;
    17.                                     }
    18.                                 }
    19.                             }
    20.  
    21.                         }
    22.                     });
    23.  
    As you can see, it is still necessary for change picking mode to ignore for the TextField and each of 3 children below it. Then I enable it back for the TextField that I want the user to be able to interact with.