Example #1
0
        internal static VisualElement Create(UIElementsSamples.SampleTreeItem item)
        {
            var panelContainer = new ScrollView();

            panelContainer.name = s_StylesExplorerName;
            panelContainer.AddToClassList(s_StylesExplorerClassName);

            var innerContainer = new VisualElement();

            panelContainer.Add(innerContainer);
            innerContainer.AddToClassList(s_ScrollViewContainerClassName);
            innerContainer.RegisterCallback <GeometryChangedEvent>(ContainerGeometryChange);
            ContainerSetColumnCount(innerContainer);

            var leftPanel  = CreatePanel();
            var rightPanel = CreatePanel();

            leftPanel.AddToClassList(s_StylePanelDarkClassName);
            rightPanel.AddToClassList(s_StylePanelLightClassName);

            leftPanel.styleSheets.Add(UIElementsEditorUtility.GetCommonDarkStyleSheet());
            rightPanel.styleSheets.Add(UIElementsEditorUtility.GetCommonLightStyleSheet());

            innerContainer.Add(leftPanel);
            innerContainer.Add(rightPanel);

            return(panelContainer);
        }
        // VLadN: Use our own ForceDarkStyleSheet as UIToolkit version also affects parent elements
        // (UIElementsEditorUtility.ForceDarkStyleSheet)
        // This makes it that panels outside of graphview like node inspector and toolbar are also forced in dark incorrectly
        private static void ForceDarkStyleSheet(VisualElement ele)
        {
            if (EditorGUIUtility.isProSkin)
            {
                return;
            }
#if UNITY_2020_3_OR_NEWER
            StyleSheet commonLightStyleSheet = UIElementsEditorUtility.GetCommonLightStyleSheet();
            StyleSheet commonDarkStyleSheet  = UIElementsEditorUtility.GetCommonDarkStyleSheet();
#else
            StyleSheet commonLightStyleSheet = UIElementsEditorUtility.s_DefaultCommonLightStyleSheet;
            StyleSheet commonDarkStyleSheet  = UIElementsEditorUtility.s_DefaultCommonDarkStyleSheet;
#endif
            VisualElement visualElement = ele;
            {
                VisualElementStyleSheetSet styleSheets = visualElement.styleSheets;
                if (styleSheets.Contains(commonLightStyleSheet))
                {
                    styleSheets = visualElement.styleSheets;
                    styleSheets.Swap(commonLightStyleSheet, commonDarkStyleSheet);
                }
            }
        }
 public static bool IsUnityEditorStyleSheet(this StyleSheet styleSheet)
 {
     if ((UIElementsEditorUtility.IsCommonDarkStyleSheetLoaded() && styleSheet == UIElementsEditorUtility.GetCommonDarkStyleSheet()) ||
         (UIElementsEditorUtility.IsCommonLightStyleSheetLoaded() && styleSheet == UIElementsEditorUtility.GetCommonLightStyleSheet()))
     {
         return(true);
     }
     return(false);
 }
        void ApplyCanvasTheme(VisualElement element, BuilderDocument.CanvasTheme theme, ThemeStyleSheet customThemeSheet)
        {
            if (element == null)
            {
                return;
            }

            // Find the runtime stylesheet.
            var runtimeStyleSheet = BuilderPackageUtilities.LoadAssetAtPath <StyleSheet>(BuilderConstants.RuntimeThemeUSSPath);

            if (runtimeStyleSheet == null)
#if !UI_BUILDER_PACKAGE || UNITY_2021_1_OR_NEWER
            { runtimeStyleSheet = UIElementsEditorUtility.GetCommonLightStyleSheet(); }
#else
            { runtimeStyleSheet = UIElementsEditorUtility.s_DefaultCommonLightStyleSheet; }
#endif

            // Remove any null stylesheet. This may occur if an used theme has been deleted.
            // This should be handle by ui toolkit
            var i = 0;

            if (element.styleSheetList != null)
            {
                while (i < element.styleSheetList.Count)
                {
                    var sheet = element.styleSheetList[i];
                    if (sheet == null)
                    {
                        element.styleSheetList?.Remove(sheet);
                        if (element.styleSheetList.Count == 0)
                        {
                            element.styleSheetList = null;
                            break;
                        }
                    }
                    else
                    {
                        i++;
                    }
                }
            }

            // Should remove the previous custom theme stylesheet
            if (m_LastCustomTheme)
            {
                element.styleSheets.Remove(m_LastCustomTheme);
            }
#if !UI_BUILDER_PACKAGE || UNITY_2021_1_OR_NEWER
            element.styleSheets.Remove(UIElementsEditorUtility.GetCommonDarkStyleSheet());
            element.styleSheets.Remove(UIElementsEditorUtility.GetCommonLightStyleSheet());
#else
            element.styleSheets.Remove(UIElementsEditorUtility.s_DefaultCommonDarkStyleSheet);
            element.styleSheets.Remove(UIElementsEditorUtility.s_DefaultCommonLightStyleSheet);
#endif
            element.styleSheets.Remove(runtimeStyleSheet);
            m_Viewport.canvas.defaultBackgroundElement.style.display = DisplayStyle.Flex;

            StyleSheet themeStyleSheet = null;
            m_Viewport.canvas.checkerboardBackgroundElement.style.display = DisplayStyle.None;

            switch (theme)
            {
            case BuilderDocument.CanvasTheme.Dark:
#if !UI_BUILDER_PACKAGE || UNITY_2021_1_OR_NEWER
                themeStyleSheet = UIElementsEditorUtility.GetCommonDarkStyleSheet();
#else
                themeStyleSheet = UIElementsEditorUtility.s_DefaultCommonDarkStyleSheet;
#endif
                break;

            case BuilderDocument.CanvasTheme.Light:
#if !UI_BUILDER_PACKAGE || UNITY_2021_1_OR_NEWER
                themeStyleSheet = UIElementsEditorUtility.GetCommonLightStyleSheet();
#else
                themeStyleSheet = UIElementsEditorUtility.s_DefaultCommonLightStyleSheet;
#endif
                break;

            case BuilderDocument.CanvasTheme.Runtime:
                themeStyleSheet = runtimeStyleSheet;
                m_Viewport.canvas.defaultBackgroundElement.style.display      = DisplayStyle.None;
                m_Viewport.canvas.checkerboardBackgroundElement.style.display = DisplayStyle.Flex;
                break;

            case BuilderDocument.CanvasTheme.Default:
                themeStyleSheet = null;
                break;

            case BuilderDocument.CanvasTheme.Custom:
                m_LastCustomTheme = customThemeSheet;
                themeStyleSheet   = customThemeSheet;
                break;
            }

            if (themeStyleSheet != null)
            {
                element.styleSheets.Add(themeStyleSheet);
            }
        }
        void ApplyCanvasTheme(VisualElement element, BuilderDocument.CanvasTheme theme, ThemeStyleSheet customThemeSheet)
        {
            if (element == null)
            {
                return;
            }

            // Remove any null stylesheet. This may occur if an used theme has been deleted.
            // This should be handle by ui toolkit
            var i = 0;

            if (element.styleSheetList != null)
            {
                while (i < element.styleSheetList.Count)
                {
                    var sheet = element.styleSheetList[i];
                    if (sheet == null)
                    {
                        element.styleSheetList?.Remove(sheet);
                        if (element.styleSheetList.Count == 0)
                        {
                            element.styleSheetList = null;
                            break;
                        }
                    }
                    else
                    {
                        i++;
                    }
                }
            }

            // Should remove the previous custom theme stylesheet
            if (m_LastCustomTheme)
            {
                element.styleSheets.Remove(m_LastCustomTheme);
                m_LastCustomTheme = null;
            }
            // We verify whether the styles are loaded beforehand because calling GetCommonXXXStyleSheet() will load them unecessarily in this case
            if (UIElementsEditorUtility.IsCommonDarkStyleSheetLoaded())
            {
                element.styleSheets.Remove(UIElementsEditorUtility.GetCommonDarkStyleSheet());
            }
            if (UIElementsEditorUtility.IsCommonLightStyleSheetLoaded())
            {
                element.styleSheets.Remove(UIElementsEditorUtility.GetCommonLightStyleSheet());
            }
            m_Viewport.canvas.defaultBackgroundElement.style.display = DisplayStyle.Flex;

            StyleSheet themeStyleSheet = null;

            m_Viewport.canvas.checkerboardBackgroundElement.style.display = DisplayStyle.None;

            switch (theme)
            {
            case BuilderDocument.CanvasTheme.Dark:
                themeStyleSheet = UIElementsEditorUtility.GetCommonDarkStyleSheet();
                break;

            case BuilderDocument.CanvasTheme.Light:
                themeStyleSheet = UIElementsEditorUtility.GetCommonLightStyleSheet();
                break;

            case BuilderDocument.CanvasTheme.Default:
                themeStyleSheet = EditorGUIUtility.isProSkin ? UIElementsEditorUtility.GetCommonDarkStyleSheet() : UIElementsEditorUtility.GetCommonLightStyleSheet();
                break;

            case BuilderDocument.CanvasTheme.Custom:
                m_Viewport.canvas.defaultBackgroundElement.style.display      = DisplayStyle.None;
                m_Viewport.canvas.checkerboardBackgroundElement.style.display = DisplayStyle.Flex;
                m_LastCustomTheme = customThemeSheet;
                themeStyleSheet   = customThemeSheet;
                break;

            default:
                break;
            }

            if (themeStyleSheet != null)
            {
                element.styleSheets.Add(themeStyleSheet);
            }

            element.SetProperty(BuilderConstants.ElementLinkedActiveThemeStyleSheetVEPropertyName, themeStyleSheet);
        }
 public StyleSheet GetUIToolkitDefaultCommonLightStyleSheet() => UIElementsEditorUtility.GetCommonLightStyleSheet();