Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by funcookergames · Nov 19, 2019 at 06:20 PM · uixmlcss

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.


  1. Am I correct in thinking that the new U.I. Builder window can be used to expand the Editor/Inspector?

  2. If U.I. Builder cannot or shouldn't be used to expand the Editor/Inspector, then I should use U.I. Elements?

  3. 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.

  • https://docs.unity3d.com/2019.1/Documentation/Manual/UIElements.html?_ga=2.266540230.955561532.1574130369-226322024.1570674277

  • https://www.youtube.com/watch?v=CZ39btQ0XlE

  • https://www.youtube.com/watch?v=sVEmJ5-dr5E&t=1075s

  • https://www.youtube.com/watch?v=t4tfgI1XvGs&t=1433s

  • https://www.youtube.com/watch?v=MNNURw0LeoQ&t=744s

  • https://blogs.unity3d.com/2019/04/23/whats-new-with-uielements-in-2019-1/

although some of the C#, USS, and UXML script confuses me

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image funcookergames · Jul 02, 2020 at 08:31 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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. the menu item to launch the custom editor window

And here's the custom editor window in action: alt text


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!


screenshot-7.png (12.6 kB)
screenshot-8.png (15.2 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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:

  1. You must have Unity 2019.3+

  2. 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:

  1. You must have Unity 2019.3+

  2. 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: Package Manager > All > UI Builder


ui-builder.png (133.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

202 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges