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.
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}";
}
}
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Unity2D/com...