A game about forced loneliness, made by TACStudios
at master 60 lines 2.0 kB view raw
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using UnityEditor.Experimental; 5using UnityEditor.StyleSheets; 6using UnityEngine; 7using UnityEngine.Timeline; 8 9namespace UnityEditor.Timeline 10{ 11 static class StyleManager 12 { 13 static readonly StyleState[] k_StyleStates = { StyleState.any }; 14 static readonly string k_ErrorCannotFindStyle = L10n.Tr("Cannot find style {0} for {1}"); 15 16 static Dictionary<Type, GUIStyle> s_CustomStyles = new Dictionary<Type, GUIStyle>(); 17 static GUISkin s_CurrentSkin; 18 19 public static GUIStyle UssStyleForType(Type type) 20 { 21 ClearCacheIfInvalid(); 22 23 GUIStyle cachedStyle; 24 if (s_CustomStyles.TryGetValue(type, out cachedStyle)) 25 return cachedStyle; 26 27 var style = DirectorStyles.GetGUIStyle(DirectorStyles.markerDefaultStyle); 28 29 var customStyleForType = CustomStyleForType(type); 30 if (customStyleForType != null) 31 { 32 if (IsStyleValid(customStyleForType)) 33 style = DirectorStyles.GetGUIStyle(customStyleForType); 34 else 35 Debug.LogWarningFormat(k_ErrorCannotFindStyle, customStyleForType, type.Name); 36 } 37 38 s_CustomStyles.Add(type, style); 39 return style; 40 } 41 42 static string CustomStyleForType(Type type) 43 { 44 var attr = (CustomStyleAttribute)type.GetCustomAttributes(typeof(CustomStyleAttribute), true).FirstOrDefault(); 45 return attr != null ? attr.ussStyle : null; 46 } 47 48 static bool IsStyleValid(string ussStyle) 49 { 50 return GUISkin.current.FindStyle(ussStyle) != null || EditorResources.styleCatalog.GetStyle(ussStyle, k_StyleStates).IsValid(); 51 } 52 53 static void ClearCacheIfInvalid() 54 { 55 if (s_CurrentSkin != GUISkin.current) 56 s_CustomStyles.Clear(); 57 s_CurrentSkin = GUISkin.current; 58 } 59 } 60}