Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Get values from VisualElement on runtime.

Discussion in 'UI Toolkit' started by ZeFirestarter, Feb 17, 2020.

  1. ZeFirestarter

    ZeFirestarter

    Joined:
    Oct 24, 2014
    Posts:
    17
    Hello everybody.

    I am trying to make the world easier for my designer.
    I have made a simple script that can override the values of a Button (Visual Element) on runtime.
    My designer can now use normal animations to animate the different values of color, size, position of the visual element. This is working great.
    Code (CSharp):
    1.     public float t_opacity;
    2.     public Color t_backgroundColor;
    3.     public DisplayStyle t_displayStyle;
    4.     public Visibility t_visibility;
    5.     public Align t_alignSelf;
    6.     public Position t_position;
    7.     public float t_right;
    8.     public int t_left;
    9.     public int t_top;
    10.     public int t_bottom;
    11.     public string t_textBtn;
    My problem is, I can't read the values of the Visual Element.
    I can store the values like this:
    Code (CSharp):
    1.         public StyleFloat s_opacity;
    2.         public StyleColor s_backgroundColor;
    3.         public StyleEnum<DisplayStyle> s_displayStyle;
    4.         public StyleEnum<Visibility> s_visibility;
    5.         public StyleEnum<Align> s_alignSelf;
    6.         public StyleEnum<Position> s_position;
    7.         public StyleLength s_right;
    8.         public StyleLength s_left;
    9.         public StyleLength s_top;
    10.         public StyleLength s_bottom;
    11.         public string s_textBtn;
    And then I have the reset values from the start.
    What I am looking for is to be able to read this values as normal float, int, color instead of StyleFloat, StyleColor, etc.

    I have tried a couple of things, but nothing is working.
    My last attempt was:
    Code (CSharp):
    1.        float t_opacity = (float)t_panelRenderer.visualTree.Q<VisualElement>(t_visualElementID).style.opacity.value;
    2.  
    3.        Color t_backgroundColor = (Color)t_panelRenderer.visualTree.Q<VisualElement>(t_visualElementID).style.backgroundColor.value;
    But nothing works.

    I forgot to mention. The text in the Button does work.
    Code (CSharp):
    1. string t_textBtn = t_panelRenderer.visualTree.Q<VisualElement>(t_visualElementID).text;
    Works perfectly, just everything after "Q<VisualElement>(t_visualElementID).style.", I can't manage to get working.
    Any suggestions?
    Thank you in advance

    Best Regards

    Roland
     
    Last edited: Feb 18, 2020
    Mj-Kkaya likes this.
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi Roland, you're problem is that you are reading VisualElement styles from the IStyle interface, this only provides access to a VisualElement inline style data. What you need to use instead is the IResolvedStyle interface.

    Be aware that some values are only computed after a layout pass.
     
    Mj-Kkaya and ZeFirestarter like this.
  3. ZeFirestarter

    ZeFirestarter

    Joined:
    Oct 24, 2014
    Posts:
    17
    Perfect, thank you so much for the help. Everything is working.
     
    jonathanma_unity likes this.