ClipEditor.cs
8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
namespace UnityEditor.Timeline
{
/// <summary>
/// Description of the on-screen area where a clip is drawn
/// </summary>
public struct ClipBackgroundRegion
{
/// <summary>
/// The rectangle where the background of the clip is drawn.
/// </summary>
/// <remarks>
/// The rectangle is clipped to the screen. The rectangle does not include clip borders.
/// </remarks>
public Rect position { get; private set; }
/// <summary>
/// The start time of the region, relative to the clip.
/// </summary>
public double startTime { get; private set; }
/// <summary>
/// The end time of the region, relative to the clip.
/// </summary>
public double endTime { get; private set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="_position"></param>
/// <param name="_startTime"></param>
/// <param name="_endTime"></param>
public ClipBackgroundRegion(Rect _position, double _startTime, double _endTime)
{
position = _position;
startTime = _startTime;
endTime = _endTime;
}
public override bool Equals(object obj)
{
if (!(obj is ClipBackgroundRegion))
return false;
return Equals((ClipBackgroundRegion)obj);
}
public bool Equals(ClipBackgroundRegion other)
{
return position.Equals(other.position) &&
startTime == other.startTime &&
endTime == other.endTime;
}
public override int GetHashCode()
{
return HashUtility.CombineHash(
position.GetHashCode(),
startTime.GetHashCode(),
endTime.GetHashCode()
);
}
public static bool operator==(ClipBackgroundRegion region1, ClipBackgroundRegion region2)
{
return region1.Equals(region2);
}
public static bool operator!=(ClipBackgroundRegion region1, ClipBackgroundRegion region2)
{
return !region1.Equals(region2);
}
}
/// <summary>
/// The user-defined options for drawing a clip.
/// </summary>
public struct ClipDrawOptions
{
private IEnumerable<Texture2D> m_Icons;
/// <summary>
/// Text that indicates if the clip should display an error.
/// </summary>
/// <remarks>
/// If the error text is not empty or null, then the clip displays a warning. The error text is used as the tooltip.
/// </remarks>
public string errorText { get; set; }
/// <summary>
/// The tooltip to show for the clip.
/// </summary>
public string tooltip { get; set; }
/// <summary>
/// The color drawn under the clip. By default, the color is the same as the track color.
/// </summary>
public Color highlightColor { get; set; }
/// <summary>
/// Icons to display on the clip.
/// </summary>
public IEnumerable<Texture2D> icons
{
get { return m_Icons ?? System.Linq.Enumerable.Empty<Texture2D>(); }
set { m_Icons = value;}
}
public override bool Equals(object obj)
{
if (!(obj is ClipDrawOptions))
return false;
return Equals((ClipDrawOptions)obj);
}
public bool Equals(ClipDrawOptions other)
{
return errorText == other.errorText &&
tooltip == other.tooltip &&
highlightColor == other.highlightColor &&
System.Linq.Enumerable.SequenceEqual(icons, other.icons);
}
public override int GetHashCode()
{
return HashUtility.CombineHash(
errorText != null ? errorText.GetHashCode() : 0,
tooltip != null ? tooltip.GetHashCode() : 0,
highlightColor.GetHashCode(),
icons != null ? icons.GetHashCode() : 0
);
}
public static bool operator==(ClipDrawOptions options1, ClipDrawOptions options2)
{
return options1.Equals(options2);
}
public static bool operator!=(ClipDrawOptions options1, ClipDrawOptions options2)
{
return !options1.Equals(options2);
}
}
/// <summary>
/// Use this class to customize clip types in the TimelineEditor.
/// </summary>
public class ClipEditor
{
static readonly string k_NoPlayableAssetError = LocalizationDatabase.GetLocalizedString("This clip does not contain a valid playable asset");
static readonly string k_ScriptLoadError = LocalizationDatabase.GetLocalizedString("The associated script can not be loaded");
internal readonly bool supportsSubTimelines;
/// <summary>
/// Default constructor
/// </summary>
public ClipEditor()
{
supportsSubTimelines = TypeUtility.HasOverrideMethod(GetType(), nameof(GetSubTimelines));
}
/// <summary>
/// Implement this method to override the default options for drawing a clip.
/// </summary>
/// <param name="clip">The clip being drawn.</param>
/// <returns>The options for drawing a clip.</returns>
public virtual ClipDrawOptions GetClipOptions(TimelineClip clip)
{
return new ClipDrawOptions()
{
errorText = GetErrorText(clip),
tooltip = string.Empty,
highlightColor = GetDefaultHighlightColor(clip),
icons = System.Linq.Enumerable.Empty<Texture2D>()
};
}
/// <summary>
/// Override this method to draw a background for a clip .
/// </summary>
/// <param name="clip">The clip being drawn.</param>
/// <param name="region">The on-screen area where the clip is drawn.</param>
public virtual void DrawBackground(TimelineClip clip, ClipBackgroundRegion region)
{
}
/// <summary>
/// Called when a clip is created.
/// </summary>
/// <param name="clip">The newly created clip.</param>
/// <param name="track">The track that the clip is assigned to.</param>
/// <param name="clonedFrom">The source that the clip was copied from. This can be set to null if the clip is not a copy.</param>
/// <remarks>
/// The callback occurs before the clip is assigned to the track.
/// </remarks>
public virtual void OnCreate(TimelineClip clip, TrackAsset track, TimelineClip clonedFrom)
{
}
/// <summary>
/// Gets the error text for the specified clip.
/// </summary>
/// <param name="clip">The clip being drawn.</param>
/// <returns>Returns the error text to be displayed as the tool tip for the clip. If there is no error to be displayed, this method returns string.Empty.</returns>
public string GetErrorText(TimelineClip clip)
{
if (clip == null || clip.asset == null)
return k_NoPlayableAssetError;
var playableAsset = clip.asset as ScriptableObject;
if (playableAsset == null || MonoScript.FromScriptableObject(playableAsset) == null)
return k_ScriptLoadError;
return string.Empty;
}
/// <summary>
/// The color drawn under the clip. By default, the color is the same as the track color.
/// </summary>
/// <param name="clip">The clip being drawn.</param>
/// <returns>Returns the highlight color of the clip being drawn.</returns>
public Color GetDefaultHighlightColor(TimelineClip clip)
{
if (clip == null)
return Color.white;
return TrackResourceCache.GetTrackColor(clip.parentTrack);
}
/// <summary>
/// Called when a clip is changed by the Editor.
/// </summary>
/// <param name="clip">The clip that changed.</param>
public virtual void OnClipChanged(TimelineClip clip)
{
}
/// <summary>
/// Gets the sub-timelines for a specific clip. Implement this method if your clip supports playing nested timelines.
/// </summary>
/// <param name="clip">The clip with the ControlPlayableAsset.</param>
/// <param name="director">The playable director driving the Timeline Clip. This may not be the same as TimelineEditor.inspectedDirector.</param>
/// <param name="subTimelines">Specify the sub-timelines to control.</param>
public virtual void GetSubTimelines(TimelineClip clip, PlayableDirector director, List<PlayableDirector> subTimelines)
{
}
}
}