Search Unity

Wrapped text is crushed after visibility toggle (display: flex -> none -> flex)

Discussion in 'UI Toolkit' started by carlosfritz, Apr 8, 2020.

  1. carlosfritz

    carlosfritz

    Joined:
    Feb 17, 2018
    Posts:
    32
    I am using Unity 2020.1.0b4 with UI Builder 0.10.2 and UIElements Runtime 0.0.4.

    That's the initial state in the game window:

    good.png

    After i click the middle button on the bottom, and then the right one to go back, it looks like this:

    bad.png

    When i select the label...

    crushed text.png

    ...and in the debugger change the value for "white-space" from "Normal" to "No Wrap" and back to "Normal"...

    debugger.png

    ...it looks good again.

    better.png

    Here is the code i use to manage the ui:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using Unity.UIElements.Runtime;
    4. using UnityEngine.UIElements;
    5.  
    6. namespace Jupiter.UI
    7. {
    8.   [RequireComponent(typeof(PanelRenderer))]
    9.   [RequireComponent(typeof(UIElementsEventSystem))]
    10.   public class Play : MonoBehaviour
    11.   {
    12.     public VisualTreeAsset _presetsTemplate;
    13.     public VisualTreeAsset _presetTemplate;
    14.     public VisualTreeAsset _avatarsTemplate;
    15.     public VisualTreeAsset _avatarTemplate;
    16.     public VisualTreeAsset _dialogTemplate;
    17.     public VisualTreeAsset _optionTemplate;
    18.  
    19.     PanelRenderer _panelRenderer;
    20.     List<UnityEngine.Object> _liveAssets;
    21.     ScrollView _presetsView;
    22.     ListView _avatarsView;
    23.     ScrollView _dialogView;
    24.     VisualElement _visibleContainer;
    25.  
    26.     void Awake()
    27.     {
    28.       _panelRenderer = GetComponent<PanelRenderer>();
    29.       _liveAssets = new List<UnityEngine.Object>();
    30.       _liveAssets.Add(_presetsTemplate);
    31.       _liveAssets.Add(_presetTemplate);
    32.       _liveAssets.Add(_avatarsTemplate);
    33.       _liveAssets.Add(_avatarTemplate);
    34.       _liveAssets.Add(_dialogTemplate);
    35.       _liveAssets.Add(_optionTemplate);
    36.     }
    37.  
    38.     void OnEnable()
    39.     {
    40.       _panelRenderer.postUxmlReload = BindScreen;
    41.     }
    42.  
    43.     void OnDisable()
    44.     {
    45.       _panelRenderer.postUxmlReload = null;
    46.     }
    47.  
    48.     public void SetPresets(List<Messages.Preset> presets)
    49.     {
    50.       _presetsView.Clear();
    51.  
    52.       foreach (Messages.Preset preset in presets)
    53.       {
    54.         _presetsView.Add(_presetTemplate.Instantiate());
    55.       }
    56.     }
    57.  
    58.     public void SetDialog(Messages.Dialog dialog)
    59.     {
    60.       var title = _dialogView.Q<Label>("title");
    61.       var text = _dialogView.Q<Label>("text");
    62.       var options = _dialogView.Q<VisualElement>("options");
    63.  
    64.       //title.text = dialog.title;
    65.       //text.text = dialog.text;
    66.       options.Clear();
    67.  
    68.       foreach (Messages.DialogOption dialogOption in dialog.options)
    69.       {
    70.         var option = _optionTemplate.Instantiate();
    71.         var subtitle = option.Q<Label>("title");
    72.         var subtext = option.Q<Label>("text");
    73.  
    74.         //subtitle.text = dialogOption.title;
    75.         //subtext.text = dialogOption.text;
    76.  
    77.         options.Add(option);
    78.       }
    79.     }
    80.  
    81.     public void SetAvatars(List<Messages.Avatar> avatars)
    82.     {
    83.       _avatarsView.itemsSource = avatars;
    84.       _avatarsView.Refresh();
    85.     }
    86.  
    87.     IEnumerable<UnityEngine.Object> BindScreen()
    88.     {
    89.       var root = _panelRenderer.visualTree;
    90.  
    91.       var container = root.Q<VisualElement>("container");
    92.       var tabPresets = root.Q<Button>("tab-presets");
    93.       var tabAvatars = root.Q<Button>("tab-avatars");
    94.       var tabDialog = root.Q<Button>("tab-dialog");
    95.  
    96.       var presetsContainer = _presetsTemplate.Instantiate();
    97.       var avatarsContainer = _avatarsTemplate.Instantiate();
    98.       var dialogContainer = _dialogTemplate.Instantiate();
    99.  
    100.       _presetsView = presetsContainer.Q<ScrollView>("presets");
    101.       _avatarsView = avatarsContainer.Q<ListView>("avatars");
    102.       _dialogView = dialogContainer.Q<ScrollView>("dialog");
    103.  
    104.       presetsContainer.Hide();
    105.       avatarsContainer.Hide();
    106.       _visibleContainer = dialogContainer;
    107.  
    108.       container.Add(presetsContainer);
    109.       container.Add(avatarsContainer);
    110.       container.Add(dialogContainer);
    111.  
    112.       tabPresets.clicked += () => { ShowContainer(presetsContainer); };
    113.       tabAvatars.clicked += () => { ShowContainer(avatarsContainer); };
    114.       tabDialog.clicked += () => { ShowContainer(dialogContainer); };
    115.  
    116.       return _liveAssets;
    117.     }
    118.  
    119.     void ShowContainer(VisualElement element)
    120.     {
    121.       _visibleContainer.Hide();
    122.       _visibleContainer = element;
    123.       _visibleContainer.Show();
    124.     }
    125.  
    126.     void MakeAvatar()
    127.     {
    128.     }
    129.  
    130.     void BindAvatar()
    131.     {
    132.     }
    133.   }
    134. }
    Code (CSharp):
    1. using UnityEngine.UIElements;
    2.  
    3. namespace Jupiter
    4. {
    5.   public static class UIElementsExtensions
    6.   {
    7.     public static void Hide(this VisualElement element)
    8.     {
    9.       element.style.display = DisplayStyle.None;
    10.     }
    11.  
    12.     public static void Show(this VisualElement element)
    13.     {
    14.       element.style.display = DisplayStyle.Flex;
    15.     }
    16.   }
    17. }
    Side question: Is the crappy text quality normal for now or can i do sth?
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello, this looks like a bug that needs to be reported. Attaching your project or a minimal reproduction project would help greatly.

    As for the text quality, this is on our radar and will be fixed in the official preview.
     
  3. carlosfritz

    carlosfritz

    Joined:
    Feb 17, 2018
    Posts:
    32
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    Sorry for the very long delay getting back to you on this.
    Last month we have a released a new UI Toolkit package which replaces the old UIElements Runtime package:
    https://forum.unity.com/threads/ui-toolkit-1-0-preview-available.927822/

    The layout issue should be fixed now.
    As for the text quality, it is still something we are working on along with a bunch of new text features that are closer to TextMesh Pro.

    Thanks
     
    carlosfritz likes this.