- Home /
How to use U.I.Elements/ U.I.Builder to access bools/toggles?
Hello, and thank you. I have 3 main questions regarding U.I. Elements/ U.I. Builder.
Am I correct in thinking that the new U.I. Builder window can be used to expand the Editor/Inspector?
If U.I. Builder cannot or shouldn't be used to expand the Editor/Inspector, then I should use U.I. Elements?
What does a simple C#,USS,UXML. setup look like when it just displays an group of named Toggles/bools.
My final goal is just to have a Class or Struct contain 64 bools(eventually in an array), that I would like to have displayed in the inspector in 8x8 rows.(eventually color coded rows). I am struggling to understand or find an example of Toggle/Bool U.I. Elements.
I have read and watched the following material on the topic.
although some of the C#, USS, and UXML script confuses me
EDIT/UPDATE-
Thank you for all your answers. I have been able to achieve my goals for this particular UI. I have simplified my UI, the User sets true/false to the toggles then "refreshing" the level with a button that applies those toggle settings to the level (which obstacles are/aren't present). $$anonymous$$y original goal was to have each toggle refresh its individual obstacle with each click of the toggle. But in the grand scheme of ergonomic use, this button refresh scheme will be more efficient.
Answer by exploringunity · May 13, 2020 at 09:07 PM
Hey @funcookergames,
My new blog Exploring Unity has some tutorials on how to use UIElements. In particular, the post Extending Unity with UIElements - Part 3 covers how to use the <Toggle>
element. Take a look, and then let me know if you have any more specific questions.
If you just want a quick code sample, below is a very simple, but complete example of how to create a custom editor window with a toggle in UIElements and check its value when a button is clicked. First some preview screenshots:
Here's the menu item and hotkey that you will have to launch the custom editor window after creating the 3 files below.
And here's the custom editor window in action:
Assets/Editor/ToggleDemo.uxml
<?xml version="1.0" encoding="utf-8"?>
<engine:UXML
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:engine="UnityEngine.UIElements"
xmlns:editor="UnityEditor.UIElements"
xsi:noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd"
>
<engine:VisualElement>
<engine:Style src="ToggleDemo.uss" />
<engine:Toggle name="myToggle" label="Toggle Label" text="Toggle Text" />
<engine:Button name="myButton" text="Click here to log the toggle value" />
</engine:VisualElement>
</engine:UXML>
Assets/Editor/ToggleDemo.uss
* { font-size: 16px; -unity-font-style: bold; }
Toggle { border-width: 2px; border-color: black;
margin: 8px; padding: 8px 0 8px 8px; }
Button { margin: 8px 16px; padding: 8px 0; }
Assets/Editor/ToggleDemo.cs
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class ToggleDemo : EditorWindow
{
Toggle myToggle;
[MenuItem("ExploringUnity.com/ToggleDemo %#t")]
public static void ShowExample() { GetWindow<ToggleDemo>(); }
public void OnEnable()
{
// Set up UI
var uiTemplate = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/ToggleDemo.uxml");
var ui = uiTemplate.Instantiate();
rootVisualElement.Add(ui);
// Find the toggle and store a reference so we can check its value later
myToggle = ui.Q<Toggle>("myToggle");
// Find the button and set up a click handler
var myBtn = ui.Q<Button>("myButton");
myBtn.clicked += LogToggleValue;
}
void LogToggleValue() { Debug.Log($"myToggle.value: {myToggle.value}"); }
}
Hope this helps!
Answer by ProFiLeR4100 · Apr 05, 2020 at 06:18 PM
Hello, @funcookergames
Yes, you can create custom windows/inspectors with UIElements. The UIBuilder is just a WYSIWYG editor for UXML files. Think about UIBuilder as similar software to Adobe Dreamweaver and about UXML files as HTML files.
To get values from toggles you need to query element by its name or get list of elements queried by class:
Dictionary<string, bool> togglesValues = treeViewElement
.Query<Toggle>(null, "unity-toggle") // Default ("unity-toggle") or custom class can be used.
.Build()
.ToList()
.ToDictionary(toggle => toggle.name, toggle => toggle.value)
togglesValues["SomeToggleName"] // toggle can be accessed by this code
P.S. In case you haven't already added or you want to add Editor/Runtime support for UIElements.
Editor UIElements Support
To add UIElements development tools and support you need do next steps:
You must have Unity 2019.3+
Install "UI Builder" package from Package Manager window (Windows > Package Manager), also don't forget to change filter to "Package: All" to find the package.
Runtime UIElements Support
To add UIElements Runtime support you need do next steps:
You must have Unity 2019.3+
You must copy UIERuntime folder from example project
P.S. Be informed that UIERuntime not on release stage it is bulded for tests and can be unstable.
Examples
Sample project with UIElements runtime support can be found here or just import code samples from UI Builder package:
Your answer
Follow this Question
Related Questions
How to update text every time I press space? 1 Answer
In WebGL, is it possible to make UI Coordinate scale with CSS style? 1 Answer
A node in a childnode? 1 Answer
cross platform enterprise UI with Unity3D 2 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers