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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Feature Request Adding stylesheet by order by insert timing.

Discussion in 'UI Toolkit' started by Creta_Park, May 5, 2021.

  1. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    I was trying implement custom style to ShaderGraph with this code snippet.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using System.Reflection;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using UnityEngine.UIElements;
    7.  
    8. public class ShaderGraphExtension : Editor {
    9.  
    10.     [MenuItem("Tools/Make it bright")]
    11.     public static void Run() {
    12.      
    13.         var asm = Assembly.Load("Unity.ShaderGraph.Editor");
    14.         var shaderGraphEditorType = asm.GetType("UnityEditor.ShaderGraph.Drawing.MaterialGraphEditWindow");
    15.      
    16.         var windows = Resources.FindObjectsOfTypeAll(shaderGraphEditorType)
    17.             .Select(obj => (EditorWindow)obj);
    18.      
    19.         if (EditorGUIUtility.Load("Assets/Editor/CustomShaderGraphStyle.uss") is StyleSheet uss)
    20.             foreach (var window in windows) {
    21.              
    22.                 IterateAllElements(window.rootVisualElement, uss);
    23.                 window.rootVisualElement.styleSheets.Add(uss);
    24.             }
    25.      
    26.         Debug.Log("Ok, injection complete! have fun!");
    27.     }
    28.  
    29.     private static void IterateAllElements(VisualElement el, StyleSheet uss) {
    30.      
    31.         el.styleSheets.Add(uss);
    32.      
    33.         foreach (var child in el.Children())
    34.             IterateAllElements(child, uss);
    35.     }
    36. }
    It'll work any ShaderGraphEditorWindow is available on editor when clicking
    Tools/Make it bright
    .
    I was sure my custom style sheet will added after initialized ShaderGraph's styles.

    upload_2021-5-5_15-28-3.png
    upload_2021-5-5_15-33-15.png

    But UI Toolkit added my stylesheet in between initialized ShaderGraph's style sheets somehow.

    I'm curious is there way to solve my stylesheet set into last of them?
     
    Last edited: May 5, 2021
    Seromu likes this.
  2. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    Just bumped this thread, is any news about relate this?