private void OnCustomStyleResolved(CustomStyleResolvedEvent e) { // We should consider not exposing image as a style at all, since it's intimately tied to uv/sourceRect Texture2D textureValue = null; Sprite spriteValue = null; VectorImage vectorImageValue = null; string scaleModeValue; Color tintValue = Color.white; ICustomStyle customStyle = e.customStyle; if (!m_ImageIsInline && customStyle.TryGetValue(s_ImageProperty, out textureValue)) { m_Image = textureValue; m_Sprite = null; m_VectorImage = null; } if (!m_ImageIsInline && customStyle.TryGetValue(s_SpriteProperty, out spriteValue)) { m_Image = null; m_Sprite = spriteValue; m_VectorImage = null; } if (!m_ImageIsInline && customStyle.TryGetValue(s_VectorImageProperty, out vectorImageValue)) { m_Image = null; m_Sprite = null; m_VectorImage = vectorImageValue; } if (!m_ScaleModeIsInline && customStyle.TryGetValue(s_ScaleModeProperty, out scaleModeValue)) { StylePropertyUtil.TryGetEnumIntValue(StyleEnumType.ScaleMode, scaleModeValue, out var intValue); SetScaleMode((ScaleMode)intValue); } if (!m_TintColorIsInline && customStyle.TryGetValue(s_TintColorProperty, out tintValue)) { m_TintColor = tintValue; } }
private void OnCustomStyleResolved(CustomStyleResolvedEvent e) { Texture2D image = null; VectorImage vectorImage = null; Color white = Color.white; ICustomStyle customStyle = e.customStyle; bool flag = !this.m_ImageIsInline && customStyle.TryGetValue(Image.s_ImageProperty, out image); if (flag) { this.m_Image = image; bool flag2 = this.m_Image != null; if (flag2) { this.m_VectorImage = null; } } bool flag3 = !this.m_ImageIsInline && customStyle.TryGetValue(Image.s_VectorImageProperty, out vectorImage); if (flag3) { this.m_VectorImage = vectorImage; bool flag4 = this.m_VectorImage != null; if (flag4) { this.m_Image = null; } } string value; bool flag5 = !this.m_ScaleModeIsInline && customStyle.TryGetValue(Image.s_ScaleModeProperty, out value); if (flag5) { this.m_ScaleMode = (ScaleMode)StylePropertyUtil.GetEnumIntValue(StyleEnumType.ScaleMode, value); } bool flag6 = !this.m_TintColorIsInline && customStyle.TryGetValue(Image.s_TintColorProperty, out white); if (flag6) { this.m_TintColor = white; } }
protected void VisitUrlFunction(PrimitiveTerm term) { string path = (string)term.Value; var response = URIHelpers.ValidateAssetURL(assetPath, path); if (response.hasWarningMessage) { m_Errors.AddValidationWarning(response.warningMessage, m_CurrentLine); } if (response.result != URIValidationResult.OK) { var(_, message) = ConvertErrorCode(response.result); m_Builder.AddValue(path, StyleValueType.MissingAssetReference); m_Errors.AddValidationWarning(string.Format(message, response.errorToken), m_CurrentLine); } else { var projectRelativePath = response.resolvedProjectRelativePath; var subAssetPath = response.resolvedSubAssetPath; var asset = response.resolvedQueryAsset; if (asset) { if (response.isLibraryAsset) { // do not add path dependencies on assets in the Library folder (e.g. built-in resources) m_Builder.AddValue(asset); return; } // explicit asset reference already loaded m_Context?.DependsOnSourceAsset(projectRelativePath); } else { asset = DeclareDependencyAndLoad(projectRelativePath, subAssetPath); } bool isTexture = asset is Texture2D; Sprite spriteAsset = asset as Sprite; if (isTexture && string.IsNullOrEmpty(subAssetPath)) { // Try to load a sprite sub-asset associated with this texture. // Sprites have extra data, such as slices and tight-meshes that // aren't stored in plain textures. spriteAsset = AssetDatabase.LoadAssetAtPath <Sprite>(projectRelativePath); } if (asset != null) { // Looking suffixed images files only if (isTexture) { string hiResImageLocation = URIHelpers.InjectFileNameSuffix(projectRelativePath, "@2x"); if (File.Exists(hiResImageLocation)) { UnityEngine.Object hiResImage = DeclareDependencyAndLoad(hiResImageLocation); if (hiResImage is Texture2D) { m_Builder.AddValue(new ScalableImage() { normalImage = asset as Texture2D, highResolutionImage = hiResImage as Texture2D }); } else { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidHighResolutionImage, string.Format(glossary.invalidHighResAssetType, asset.GetType().Name, projectRelativePath), m_CurrentLine); } return; } // If we didn't find an high res variant, tell ADB we depend on that potential file existing if (spriteAsset != null) { DeclareDependencyAndLoad(hiResImageLocation); } } Object assetToStore = spriteAsset != null ? spriteAsset : asset; m_Builder.AddValue(assetToStore); if (!disableValidation) { var propertyName = new StylePropertyName(m_Builder.currentProperty.name); // Unknown properties (not custom) should beforehand if (propertyName.id == StylePropertyId.Unknown) { return; } var allowed = StylePropertyUtil.GetAllowedAssetTypesForProperty(propertyName.id); // If no types were returned, it means this property doesn't support assets. // Normal syntax validation should cover this. if (!allowed.Any()) { return; } Type assetType = assetToStore.GetType(); // If none of the allowed types are compatible with the asset type, output a warning if (!allowed.Any(t => t.IsAssignableFrom(assetType))) { string allowedTypes = string.Join(", ", allowed.Select(t => t.Name)); m_Errors.AddValidationWarning( string.Format(glossary.invalidAssetType, assetType.Name, projectRelativePath, allowedTypes), m_CurrentLine); } } } else { // Asset is actually missing OR we couldn't load it for some reason; this should result in // response.result != URIValidationResult.OK (above) but if assets are deleted while Unity is // already open, we fall in here instead. var(_, message) = ConvertErrorCode(URIValidationResult.InvalidURIProjectAssetPath); // In case of error, we still want to call AddValue, with parameters to indicate the problem, in order // to keep the full layout from being discarded. We also add appropriate warnings to explain to the // user what is wrong. m_Builder.AddValue(path, StyleValueType.MissingAssetReference); m_Errors.AddValidationWarning(string.Format(message, path), m_CurrentLine); } } }
/// <summary> /// This will iterate over the current UI Builder hierarchy and extract the supported animatable properties. /// This will allow to display the options to users in the same order that they are in the builder. /// </summary> void GenerateTransitionPropertiesContent() { var content = new CategoryDropdownContent(); content.AppendValue(new CategoryDropdownContent.ValueItem { value = "all", displayName = "all" }); foreach (var kvp in m_StyleCategories) { var groupName = kvp.Key.text; if (string.IsNullOrWhiteSpace(groupName) || groupName == "Transition Animations") { continue; } content.AppendCategory(new CategoryDropdownContent.Category { name = groupName }); foreach (var element in kvp.Value) { var styleName = element.GetProperty(BuilderConstants.InspectorStylePropertyNameVEPropertyName) as string; if (!string.IsNullOrWhiteSpace(styleName)) { var styleId = StyleDebug.GetStylePropertyIdFromName(styleName); if (!StylePropertyUtil.IsAnimatable(styleId)) { continue; } if (!string.IsNullOrWhiteSpace(styleId.ToString())) { content.AppendValue( new CategoryDropdownContent.ValueItem { categoryName = groupName, value = styleName, displayName = ObjectNames.NicifyVariableName(styleId.ToString()) }); } } if (!(element is FoldoutField foldoutField)) { continue; } var hashSet = HashSetPool <StylePropertyId> .Get(); try { foreach (var bindingPath in foldoutField.bindingPathArray) { var shortHandId = StyleDebug.GetStylePropertyIdFromName(bindingPath).GetShorthandProperty(); if (shortHandId == StylePropertyId.Unknown || !hashSet.Add(shortHandId)) { continue; } if (!StylePropertyUtil.IsAnimatable(shortHandId)) { continue; } if (!string.IsNullOrWhiteSpace(shortHandId.ToString())) { content.AppendValue( new CategoryDropdownContent.ValueItem { categoryName = groupName, value = BuilderNameUtilities.ConvertStyleCSharpNameToUssName( shortHandId.ToString()), displayName = ObjectNames.NicifyVariableName(shortHandId.ToString()) }); } } } finally { HashSetPool <StylePropertyId> .Release(hashSet); } } } content.AppendSeparator(); content.AppendValue(new CategoryDropdownContent.ValueItem { value = "none", displayName = "none" }); content.AppendValue(new CategoryDropdownContent.ValueItem { value = "initial", displayName = "initial" }); content.AppendValue(new CategoryDropdownContent.ValueItem { value = "ignored", displayName = "ignored" }); TransitionPropertyDropdownContent.Content = content; }