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