Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

5
How do I add and use a text field with the new UI toolkit?
Post Flair (click to view more posts with a particular flair)
Post Body

I've added a button, and easily set the OnClick method to do what I want. But I created a text field, and I can't edit it when I run the game. Upon clicking it, nothing happens.

Older videos, before UI Toolkit was added, show downloading a packaged for UI Toolkit, which includes other packages that don't seem to be a part of vanilla Unity.

I watched this video and the guy set up an event system to capture input. Is that really necessary with the new UI Toolkit? We have methods like OnClick. My button is working just fine without such a system.

Here is the script for my UIController game object, which includes script for a button and a working clock, just to show what I've done so far:

public class UIController : MonoBehaviour
{
public Button CreateGridButton;
public Label systemClockDisplay; //not implimented yet
public TextField textField;
public VisualElement devSettings;
//drag-in the object from the hierachy to here
public GameController gameControllerGameObject;

// Start is called before the first frame update
void Start()
{
    //get the root for the button
    var root = GetComponent<UIDocument>().rootVisualElement;

    //get the button itself (without dragging I guess!)
    CreateGridButton = root.Q<Button>( "CreateGridButton" );
    CreateGridButton.clicked  = CreateGridButtonPressed;

    //get the root of the container
    devSettings = root.Q<VisualElement>( "DevSettings" );
    //get the root of the text field NumberOfRowsTextField
    textField = root.Q<TextField>( "NumberOfRowsTextField" );
    print( textField.text );

    //get the clock
    systemClockDisplay = root.Q<Label>( "SystemClock" );
    systemClockDisplay.text = "Look ma I changed the text";

}


void CreateGridButtonPressed()
{
    print( "CreateGridButtonPressed" );
    hideDevSettings();

    GameController gameController = gameControllerGameObject.GetComponent<GameController>();
    //get info from text
    Int32.TryParse( textField.text, out int result );

    if( result < 1 )
    {
        //throw new ArgumentOutOfRangeException( $"result {result} was too low" );
        Debug.LogWarning( $"input {textField.text} was too low or invalid, will use default value {GameState.Instance.SetNumberOfRows}." );
    }
    else
    {
        print( $"setting SetNumberOfRows to {result}" );
        GameState.Instance.SetNumberOfRows = result;
    }
    gameController.GenerateTilesAndDisableMapClicks();
}

private bool devSettingsAreHidden = false;
private void hideDevSettings()
{
    devSettings.style.display = DisplayStyle.None;
    devSettingsAreHidden = true;
}

private void LateUpdate()
{
    advanceSystemClockDisplay();
}

private void advanceSystemClockDisplay()
{
    if( devSettingsAreHidden )
    {
        return;
    }
    var time = DateTime.Now;
    var minute = time.Minute;
    var minuteString = minute.ToString();
    if( minute < 10 )
    {
        minuteString = "0"   minuteString;
    }
    var second = time.Second;
    var secondString = time.Second.ToString();
    if( second < 10 )
    {
        secondString = "0"   secondString;
    }
    systemClockDisplay.text = $"{time.Hour}:{minuteString}:{secondString}";
}

}

Author
Account Strength
100%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
635,608
Link Karma
107,612
Comment Karma
524,749
Profile updated: 4 days ago
Posts updated: 2 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago