• 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 AlexDavies · Jul 01, 2020 at 02:43 PM · editorguieditor windowuitoolkit

UIElements display:none not working properly

I'm getting some really strange behavior when setting the display-style of a button. The expected behavior is that the install button acts as if it doesn't exist (can be seen at end of gif) - it is invisible and removed from the layout. Setting the style inline or through a style sheet has the expected behavior, but is not dynamic (and is therefore not very useful). My code: C#:

 var installButton = rootVisualElement.Q<Button>("plugin-install-button");
 var removeButton = rootVisualElement.Q<Button>("plugin-remove-button");
 var updateButton = rootVisualElement.Q<Button>("plugin-update-button");
 
 installButton.style.display = pluginInstalled ? DisplayStyle.None : DisplayStyle.Flex;
 removeButton.style.display = pluginInstalled ? DisplayStyle.Flex : DisplayStyle.None;
 updateButton.style.display = pluginInstalled ? DisplayStyle.Flex : DisplayStyle.None;

UXML:

 <engine:Button name="plugin-install-button" text="Install"/>
 <engine:Button name="plugin-update-button" text="Update"/>
 <engine:Button name="plugin-remove-button" text="Remove"/>

First, the button remains visible until I resize the window. The layout is still incorrect even after it dissappears. Calling MarkDirtyRepaint() on either the buttons, their parents, or the root does nothing, and calling EditorWindow.Repaint() also does nothing. The only way that I can get the layout to update correctly is by changing the display property in the UIElements Debugger to 'flex' and then back to 'none'. example gif Note: I'm using Unity 2020.1.0b4

EDIT: I think that my problem may be caused by calling the C# code below from asynchronous code. (I think from another thread). This might be incorrect though, as using VisualElement.schedule to effectively queue actions didn't solve the problem.

Comment
Add comment · Show 7
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 exploringunity · Jul 13, 2020 at 04:53 PM 0
Share

Hey @AlexDavies, I don't think there's enough context to solve this issue -- could you share some more code? Is that C# code in your EditorWindow class's OnEnable? You mentioned async/threading?

avatar image AlexDavies · Jul 13, 2020 at 05:30 PM 0
Share

The code is called in a callback method from System.Diagnostics.Process.Start(), or more specifically, in the callback for Process.Exited (which is run when the process has completed its task). In this case the process call is to run some git commands.

The original call to Process.Start is in the EditorWindow’s OnEnable.

avatar image andrew-lukasik · Jul 13, 2020 at 08:50 PM 0
Share

Try running this async code in try/catch block, where catch uses Debug.LogException. You may see hidden exception here.

avatar image AlexDavies · Jul 13, 2020 at 10:19 PM 0
Share

Fortunately (or unfortunately for the future), I no longer have the code for it as I refactored it to not need to change the buttons this way. I think it's unlikely to be an exception, given that it correctly runs the code - the display property does change, it just doesn't seem to trigger a repaint or layout update. It's almost as if it's not detecting the change.

avatar image andrew-lukasik AlexDavies · Jul 13, 2020 at 10:30 PM 0
Share

Unity throws exceptions when foreign thread calls !thread-safe functions,`Repaint` for example

avatar image exploringunity andrew-lukasik · Jul 13, 2020 at 11:20 PM 1
Share

+1 to what @andrew-lukasik said. Here's a minimal demo -- if you create a basic UIElements editor window and add a simple async call like this...

     public void OnEnable {
 ... // default stuff from Create -> UIElements -> Editor Window
         root.Add(labelWithStyle);
         Task.Run(() => HideLabel(labelWithStyle));
     }
 
     public void HideLabel(Label lbl)
     {
         try
         {
             Debug.Log("Starting sleep.");
             System.Threading.Thread.Sleep(2000);
             Debug.Log("Sleep complete.");
             lbl.style.display = DisplayStyle.None;
         }
         catch (System.Exception e)
         {
             Debug.Log($"{e}");
         }
     }

... you'll get an Exception like so:

An exception was logged that says

If you tried to modify the layout, for example using rootVisualElement.Clear(), you'd get a similar exception -- UnityEngine.UnityException: YGNodeRemoveChild can only be called from the main thread.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

291 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 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 do I get a reference to the default editor windows (Hierarchy, Console, and Inspector)? 1 Answer

Blurry editor UI 2 Answers

How to draw two button overlap in editor window and keep it correct render and event?, 1 Answer

Initialising List array for use in a custom Editor 1 Answer

How to make an Editor Window with a border 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges