public static bool IsUnityEditorStyleSheet(this StyleSheet styleSheet)
 {
     if ((UIElementsEditorUtility.IsCommonDarkStyleSheetLoaded() && styleSheet == UIElementsEditorUtility.GetCommonDarkStyleSheet()) ||
         (UIElementsEditorUtility.IsCommonLightStyleSheetLoaded() && styleSheet == UIElementsEditorUtility.GetCommonLightStyleSheet()))
     {
         return(true);
     }
     return(false);
 }
Esempio n. 2
0
        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);
        }