public static void TransferRulePropertiesToSelector(this StyleSheet toStyleSheet, StyleComplexSelector toSelector, StyleSheet fromStyleSheet, StyleRule fromRule) { foreach (var property in fromRule.properties) { var newProperty = toStyleSheet.AddProperty(toSelector, property.name); foreach (var value in property.values) { switch (value.valueType) { case StyleValueType.Float: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetFloat(value)); break; #if UNITY_2019_3_OR_NEWER case StyleValueType.Dimension: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetDimension(value)); break; #endif case StyleValueType.Enum: toStyleSheet.AddValueAsEnum(newProperty, fromStyleSheet.GetEnum(value)); break; case StyleValueType.String: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetString(value)); break; case StyleValueType.Color: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetColor(value)); break; case StyleValueType.AssetReference: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetAsset(value)); break; case StyleValueType.ResourcePath: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetAsset(value)); break; #if UNITY_2019_3_OR_NEWER // UNITY_BUILDER_VARIABLE_SUPPORT case StyleValueType.Variable: toStyleSheet.AddVariable(newProperty, fromStyleSheet.GetString(value)); break; #endif } } } foreach (var property in fromRule.properties) { fromStyleSheet.RemoveProperty(fromRule, property); } }
static StyleProperty GetOrCreateStylePropertyByStyleName(StyleSheet styleSheet, StyleRule styleRule, string styleName) { var styleProperty = styleSheet.FindProperty(styleRule, styleName); if (styleProperty == null) { styleProperty = styleSheet.AddProperty(styleRule, styleName); } return(styleProperty); }
public static void AddStyleComplexSelectorToSelection(StyleSheet styleSheet, StyleComplexSelector scs) { var selectionProp = styleSheet.AddProperty( scs, BuilderConstants.SelectedStyleRulePropertyName, BuilderConstants.ChangeSelectionUndoMessage); // Need to add at least one dummy value because lots of code will die // if it encounters a style property with no values. styleSheet.AddValue( selectionProp, 42.0f, BuilderConstants.ChangeSelectionUndoMessage); }
public static void SwallowStyleRule( StyleSheet toStyleSheet, StyleRule toRule, StyleSheet fromStyleSheet, StyleRule fromRule) { // Add property values to sheet. foreach (var fromProperty in fromRule.properties) { var toProperty = toStyleSheet.AddProperty(toRule, fromProperty.name); for (int i = 0; i < fromProperty.values.Length; ++i) { var fromValueHandle = fromProperty.values[i]; var toValueIndex = toStyleSheet.SwallowStyleValue(fromStyleSheet, fromValueHandle); toStyleSheet.AddValueHandle(toProperty, toValueIndex, fromValueHandle.valueType); } } }
void CloneTree() { m_Container.Clear(); m_StyleSheetContents.Clear(); m_VisualTreeAssetContents.Clear(); m_Container.styleSheets.Clear(); if (m_VisualTreeAsset != null) { m_VisualTreeAsset.LinkedCloneTree(m_Container); var canvas = m_Container.Q("sample-canvas"); var canvasAsset = canvas.GetVisualElementAsset(); var newButton = m_VisualTreeAsset.AddElement(canvasAsset, "UnityEngine.UIElements.Button"); newButton.AddProperty("name", "new-guy"); newButton.AddProperty("text", "Canvas Button 2!"); newButton.AddStyleClass("new-guy-type"); newButton.AddStyleClass("some-button"); newButton.RemoveStyleClass("some-button"); newButton.AddStyleClass("some-fancy-button"); { // Add max-width to newButton. var rule = m_VisualTreeAsset.GetOrCreateInlineStyleRule(newButton); var prop = m_VisualTreeAsset.inlineSheet.AddProperty(rule, "max-width"); var val = m_VisualTreeAsset.inlineSheet.AddValue(prop, 200); } { // Add max-width to canvas. var rule = m_VisualTreeAsset.GetOrCreateInlineStyleRule(canvasAsset); var prop = m_VisualTreeAsset.inlineSheet.AddProperty(rule, "max-width"); var val = m_VisualTreeAsset.inlineSheet.AddValue(prop, 500); } { // Change border of canvas. var rule = m_VisualTreeAsset.GetOrCreateInlineStyleRule(canvasAsset); var prop = m_VisualTreeAsset.inlineSheet.FindLastProperty(rule, "border-width"); m_VisualTreeAsset.inlineSheet.SetValue(prop.values[0], 10); } { // Remove max-width var rule = m_VisualTreeAsset.GetOrCreateInlineStyleRule(canvasAsset); var prop = m_VisualTreeAsset.inlineSheet.FindLastProperty(rule, "max-width"); m_VisualTreeAsset.inlineSheet.RemoveProperty(rule, prop); } var newButton2 = m_VisualTreeAsset.AddElement(canvasAsset, "UnityEngine.UIElements.Button"); m_VisualTreeAsset.RemoveElement(newButton2); var newInstance = m_VisualTreeAsset.AddTemplateInstance(canvasAsset, "SampleSection"); newInstance.SetAttributeOverride("section-text-field", "label", "label programmatically written!"); newInstance.SetAttributeOverride("section-text-field", "text", "text programmatically written!"); newInstance.RemoveAttributeOverride("section-text-field", "text"); newInstance.AddStyleSheetPath(s_CanvasInstanceUSSPath); var overriddenSection = m_VisualTreeAsset.FindElementByName("overridden-section"); if (overriddenSection != null) { overriddenSection.RemoveStyleSheetPath(s_CanvasInstanceUSSPath); } // Add UXML string. var uxmlString = m_VisualTreeAsset.GenerateUXML(null); m_VisualTreeAssetContents.Add(new Label(uxmlString)); // Add inline stylesheet. var inlineBuilder = new StringBuilder(); foreach (var rule in m_VisualTreeAsset.inlineSheet.rules) { inlineBuilder.Append("{\n"); var exportOptions = new UssExportOptions(); StyleSheetToUss.ToUssString(m_VisualTreeAsset.inlineSheet, exportOptions, rule, inlineBuilder); inlineBuilder.Append("}\n"); } var inlineStyleSheetString = inlineBuilder.ToString(); m_VisualTreeAssetContents.Add(new Label(inlineStyleSheetString)); m_Container.Clear(); m_VisualTreeAsset.LinkedCloneTree(m_Container); } if (m_StyleSheet != null) { // Add width //var firstSelector = m_StyleSheet.complexSelectors.First(); //var firstSelector = m_StyleSheet.FindSelector(".blue#red > .green .pink"); var firstSelector = m_StyleSheet.FindSelector(".unity-button"); if (firstSelector != null) { var widthProperty = m_StyleSheet.AddProperty(firstSelector, "width"); var widthValue = m_StyleSheet.AddValue(widthProperty, 62); m_StyleSheet.SetValue(widthValue, 82); m_StyleSheet.RemoveProperty(firstSelector, widthProperty); var borderWidthProperty = m_StyleSheet.AddProperty(firstSelector, "border-width"); m_StyleSheet.AddValue(borderWidthProperty, 1); m_StyleSheet.AddValue(borderWidthProperty, 2); m_StyleSheet.AddValue(borderWidthProperty, 5); var leftBorderWidthValue = m_StyleSheet.AddValue(borderWidthProperty, 8); m_StyleSheet.RemoveValue(borderWidthProperty, leftBorderWidthValue); var borderColorProperty = m_StyleSheet.AddProperty(firstSelector, "border-color"); var borderColorValue = m_StyleSheet.AddValue(borderColorProperty, Color.red); m_StyleSheet.SetValue(borderColorValue, Color.green); } var newSelector = m_StyleSheet.AddSelector(".unity-button Label"); { var widthProperty = m_StyleSheet.AddProperty(newSelector, "width"); var widthValue = m_StyleSheet.AddValue(widthProperty, 62); m_StyleSheet.SetValue(widthValue, 82); } // // Add USS contents. //var selectorStrings = m_StyleSheet.GetSelectorStrings(); //foreach (var selectorString in selectorStrings) //m_StyleSheetContents.Add(new Label(selectorString)); // Add USS string. var ussString = m_StyleSheet.GenerateUSS(); m_StyleSheetContents.Add(new Label(ussString)); //m_Container.styleSheets.Add(m_StyleSheet); } }
void EnsureStylePropertyExists() { m_StyleProperty ??= GetStyleProperty(styleRule, propertyName) ?? styleSheet.AddProperty(styleRule, propertyName); }
public static StyleProperty AddProperty( this StyleSheet styleSheet, StyleComplexSelector selector, string name, string undoMessage = null) { return(styleSheet.AddProperty(selector.rule, name, undoMessage)); }