Search Unity

Question on stylesheet best practice and performance implications

Discussion in 'UI Toolkit' started by MostHated, May 2, 2022.

  1. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    I was wondering if there is a best practice when creating more complex editors when it comes to putting smaller-sized specific StyleSheets on individual child elements vs. having a more "all-inclusive" StyleSheet on a parent object, giving access to a larger range of classes down to all the children?

    Example:

    The top item is an EditorWindow that has a basic StyleSheet attached to it, and also has several complex child objects. Each child item has a smaller individual StyleSheet attached directly to it's tree.

    The lower item is the same EditorWindow with the complex child items, but a single large StyleSheet attached with all of the items for the child objects all in one.

    Is there a reason that one is better than the other, or is there any sort of impact, either positive or negative of doing one or the other?


     
  2. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    248
    The boring answer is that it really depends on your project, how big your stylesheets are and where your bottleneck is.

    Having separate stylesheets will most likely reduce the computation time when we are resolving style on the editor window hierarchy, because you then avoid the complex element stylesheets on everything that does not directly reference it. So if you have a lot of complex selectors in there, this is some time you could save. On the other hand, having multiple stylesheets means more memory usage to support what is loaded and the acceleration structures we use internally. But then, the fact that it is an editor window, and that it is using a lot of Unity default styles, means your stylesheets could have very litter impact in the end depending on their size.

    Hope this answers your question! Let us know if you need more info :)
     
    EZaca likes this.
  3. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    I definitely appreciate the information. The reason this came up in the first place was I was playing around with Sass and made an AssetPostprocessor that calls the Sass compiler to convert a Sass file to USS. I had never used Sass before, so as I was reading up on it more, one thing lead to another when I learned I could create a single "Main" file and multiple partial files that get linked back to the main file via an _index.sass file.



    So then I updated my AssetPostProcessor to be able to handle figuring out the logistics of it and combine all the partials and then started taking some of the many USS stylesheets I was using and just making them Sass partials and having just a single stylesheet on the main editor window.

    That was when I started wondering if that was a good idea to begin with, though. Based on what you mentioned, though, I suppose I would really just have to test it out and see. I am sure there must be some way to profile it, I just have never really dug to deep into profiling something using UIElements.

    I suppose, though, the fact that I technically have things split up already, if I discover that there is some negative impact once I start combining more of them, it would really not be that difficult to have it instead produce multiple individual ones, as I just need to remove the _index.sass file and the @use statement from the main file.

    I will experiment some more with it and just see how things go.

    Thanks again. I do appreciate it.
    -MH
     
    uMathieu and griendeau_unity like this.