TrackResourceCache.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. namespace UnityEditor.Timeline
  7. {
  8. static class TrackResourceCache
  9. {
  10. private static Dictionary<System.Type, GUIContent> s_TrackIconCache = new Dictionary<Type, GUIContent>(10);
  11. private static Dictionary<System.Type, Color> s_TrackColorCache = new Dictionary<Type, Color>(10);
  12. public static GUIContent s_DefaultIcon = EditorGUIUtility.IconContent("UnityEngine/ScriptableObject Icon");
  13. public static GUIContent GetTrackIcon(TrackAsset track)
  14. {
  15. if (track == null)
  16. return s_DefaultIcon;
  17. GUIContent content = null;
  18. if (!s_TrackIconCache.TryGetValue(track.GetType(), out content))
  19. {
  20. content = FindTrackIcon(track);
  21. s_TrackIconCache[track.GetType()] = content;
  22. }
  23. return content;
  24. }
  25. public static Color GetTrackColor(TrackAsset track)
  26. {
  27. if (track == null)
  28. return Color.white;
  29. // Try to ensure DirectorStyles is initialized first
  30. // Note: GUISkin.current must exist to be able do so
  31. if (!DirectorStyles.IsInitialized && GUISkin.current != null)
  32. DirectorStyles.ReloadStylesIfNeeded();
  33. Color color;
  34. if (!s_TrackColorCache.TryGetValue(track.GetType(), out color))
  35. {
  36. var attr = track.GetType().GetCustomAttributes(typeof(TrackColorAttribute), true);
  37. if (attr.Length > 0)
  38. {
  39. color = ((TrackColorAttribute)attr[0]).color;
  40. }
  41. else
  42. {
  43. // case 1141958
  44. // There was an error initializing DirectorStyles
  45. if (!DirectorStyles.IsInitialized)
  46. return Color.white;
  47. color = DirectorStyles.Instance.customSkin.colorDefaultTrackDrawer;
  48. }
  49. s_TrackColorCache[track.GetType()] = color;
  50. }
  51. return color;
  52. }
  53. public static void ClearTrackIconCache()
  54. {
  55. s_TrackIconCache.Clear();
  56. }
  57. public static void SetTrackIcon<T>(GUIContent content) where T : TrackAsset
  58. {
  59. s_TrackIconCache[typeof(T)] = content;
  60. }
  61. public static void ClearTrackColorCache()
  62. {
  63. s_TrackColorCache.Clear();
  64. }
  65. public static void SetTrackColor<T>(Color c) where T : TrackAsset
  66. {
  67. s_TrackColorCache[typeof(T)] = c;
  68. }
  69. private static GUIContent FindTrackIcon(TrackAsset track)
  70. {
  71. // backwards compatible -- try to load from Gizmos folder
  72. Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Gizmos/" + track.GetType().Name + ".png");
  73. if (texture != null)
  74. return new GUIContent(texture);
  75. // try to load based on the binding type
  76. var binding = track.outputs.FirstOrDefault();
  77. if (binding.outputTargetType != null)
  78. {
  79. // Type calls don't properly handle monobehaviours, because an instance is required to
  80. // get the monoscript icons
  81. if (typeof(MonoBehaviour).IsAssignableFrom(binding.outputTargetType))
  82. {
  83. texture = null;
  84. var scripts = UnityEngine.Resources.FindObjectsOfTypeAll<MonoScript>();
  85. foreach (var script in scripts)
  86. {
  87. if (script.GetClass() == binding.outputTargetType)
  88. {
  89. texture = AssetPreview.GetMiniThumbnail(script);
  90. break;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. texture = EditorGUIUtility.FindTexture(binding.outputTargetType);
  97. }
  98. if (texture != null)
  99. return new GUIContent(texture);
  100. }
  101. // default to the scriptable object icon
  102. return s_DefaultIcon;
  103. }
  104. }
  105. }