暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrackResourceCache.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Texture2D GetTrackIconForType(System.Type trackType)
  26. {
  27. if (trackType == null || !typeof(TrackAsset).IsAssignableFrom(trackType))
  28. return null;
  29. GUIContent content;
  30. if (!s_TrackIconCache.TryGetValue(trackType, out content) || content.image == null)
  31. return s_DefaultIcon.image as Texture2D;
  32. return content.image as Texture2D;
  33. }
  34. public static Color GetTrackColor(TrackAsset track)
  35. {
  36. if (track == null)
  37. return Color.white;
  38. // Try to ensure DirectorStyles is initialized first
  39. // Note: GUISkin.current must exist to be able do so
  40. if (!DirectorStyles.IsInitialized && GUISkin.current != null)
  41. DirectorStyles.ReloadStylesIfNeeded();
  42. Color color;
  43. if (!s_TrackColorCache.TryGetValue(track.GetType(), out color))
  44. {
  45. var attr = track.GetType().GetCustomAttributes(typeof(TrackColorAttribute), true);
  46. if (attr.Length > 0)
  47. {
  48. color = ((TrackColorAttribute)attr[0]).color;
  49. }
  50. else
  51. {
  52. // case 1141958
  53. // There was an error initializing DirectorStyles
  54. if (!DirectorStyles.IsInitialized)
  55. return Color.white;
  56. color = DirectorStyles.Instance.customSkin.colorDefaultTrackDrawer;
  57. }
  58. s_TrackColorCache[track.GetType()] = color;
  59. }
  60. return color;
  61. }
  62. public static void ClearTrackIconCache()
  63. {
  64. s_TrackIconCache.Clear();
  65. }
  66. public static void SetTrackIcon<T>(GUIContent content) where T : TrackAsset
  67. {
  68. s_TrackIconCache[typeof(T)] = content;
  69. }
  70. public static void ClearTrackColorCache()
  71. {
  72. s_TrackColorCache.Clear();
  73. }
  74. public static void SetTrackColor<T>(Color c) where T : TrackAsset
  75. {
  76. s_TrackColorCache[typeof(T)] = c;
  77. }
  78. private static GUIContent FindTrackIcon(TrackAsset track)
  79. {
  80. // backwards compatible -- try to load from Gizmos folder
  81. Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Gizmos/" + track.GetType().Name + ".png");
  82. if (texture != null)
  83. return new GUIContent(texture);
  84. // try to load based on the binding type
  85. var binding = track.outputs.FirstOrDefault();
  86. if (binding.outputTargetType != null)
  87. {
  88. // Type calls don't properly handle monobehaviours, because an instance is required to
  89. // get the monoscript icons
  90. if (typeof(MonoBehaviour).IsAssignableFrom(binding.outputTargetType))
  91. {
  92. texture = null;
  93. var scripts = UnityEngine.Resources.FindObjectsOfTypeAll<MonoScript>();
  94. foreach (var script in scripts)
  95. {
  96. if (script.GetClass() == binding.outputTargetType)
  97. {
  98. texture = AssetPreview.GetMiniThumbnail(script);
  99. break;
  100. }
  101. }
  102. }
  103. else
  104. {
  105. texture = EditorGUIUtility.FindTexture(binding.outputTargetType);
  106. }
  107. if (texture != null)
  108. return new GUIContent(texture);
  109. }
  110. // default to the scriptable object icon
  111. return s_DefaultIcon;
  112. }
  113. }
  114. }