Version: Unity 6.7 Alpha (6000.7)
LanguageEnglish
  • C#

IStyle.Clear

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public void Clear(bool clearSourceAssetStyles);

Parameters

Parameter Description
clearSourceAssetStyles Indicates if the inline style properties coming from the source asset must also be cleared for the element.

Description

Clears inline style properties of the element.



After clearing, the style properties of the element revert to the values defined in stylesheets or default values.

By default, this method clears all inline style properties, including those coming from the source asset from which the element was created. To preserve these properties, set the to false.

The following example compares this method and resetting style properties individually.

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class VisualElementClearInlineStylesWindow : EditorWindow { [MenuItem("Window/UI Toolkit/Clear Inline Styles Example")] public static void ShowWindow() { var window = GetWindow<VisualElementClearInlineStylesWindow>(); window.titleContent = new GUIContent("Clear Inline Styles Example"); }

private void SetStyles(VisualElement element) { element.style.fontSize = 24; element.style.color = Color.red; }

public void CreateGUI() { var container = new VisualElement(); container.style.paddingLeft = 10; container.style.paddingRight = 10; container.style.paddingTop = 10; container.style.paddingBottom = 10; rootVisualElement.Add(container);

// Create an element with multiple inline styles var styledElement = new Label() { text = "Example" }; SetStyles(styledElement);

// Button to clear one property at a time container.Add(new Button(() => { styledElement.style.fontSize = StyleKeyword.Null; }) { text = "Clear Font Size Only" });

container.Add(new Button(() => { styledElement.style.color = StyleKeyword.Null; }) { text = "Clear Color Only" });

// Button to clear all properties at once container.Add(new Button(() => { if (styledElement.style.fontSize == StyleKeyword.Null && styledElement.style.color == StyleKeyword.Null ) Debug.Log("All styles already unset!");

styledElement.style.Clear(); }) { text = "Clear All Styles" });

// Button to reset the styles container.Add(new Button(() => { SetStyles(styledElement); }) { text = "Set Styles" });

container.Add(styledElement); }

}
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961