AVProWindowsMediaMovieEditor.cs
14.1 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Support for Editor.RequiresConstantRepaint()
#if UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_5
#define AVPROWINDOWSMEDIA_UNITYFEATURE_EDITORAUTOREFRESH
#endif
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
//-----------------------------------------------------------------------------
// Copyright 2012-2016 RenderHeads Ltd. All rights reserved.
//-----------------------------------------------------------------------------
[CustomEditor(typeof(AVProWindowsMediaMovie))]
public class AVProWindowsMediaMovieEditor : Editor
{
private AVProWindowsMediaMovie _movie;
private bool _showAlpha;
#if AVPROWINDOWSMEDIA_UNITYFEATURE_EDITORAUTOREFRESH
public override bool RequiresConstantRepaint()
{
return (_movie != null && _movie._editorPreview && _movie.MovieInstance != null);
}
#endif
#if UNITY_EDITOR_WIN
private static void ShowInExplorer(string itemPath)
{
itemPath = System.IO.Path.GetFullPath(itemPath.Replace(@"/", @"\")); // explorer doesn't like front slashes
if (System.IO.File.Exists(itemPath))
{
System.Diagnostics.Process.Start("explorer.exe", "/select," + itemPath);
}
}
#endif
private static bool Browse(string startPath, ref string filename, ref string folder, ref bool isStreamingAsset)
{
bool result = false;
string path = UnityEditor.EditorUtility.OpenFilePanel("Browse video file", startPath, "*");
if (!string.IsNullOrEmpty(path) && !path.EndsWith(".meta"))
{
string projectRoot = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, ".."));
projectRoot = projectRoot.Replace('\\', '/');
if (path.StartsWith(projectRoot))
{
if (path.StartsWith(Application.streamingAssetsPath))
{
// Must be StreamingAssets relative path
path = path.Remove(0, Application.streamingAssetsPath.Length);
filename = System.IO.Path.GetFileName(path);
path = System.IO.Path.GetDirectoryName(path);
if (path.StartsWith(System.IO.Path.DirectorySeparatorChar.ToString()) || path.StartsWith(System.IO.Path.AltDirectorySeparatorChar.ToString()))
{
path = path.Remove(0, 1);
}
folder = path;
isStreamingAsset = true;
}
else
{
// Must be project relative path
path = path.Remove(0, projectRoot.Length);
filename = System.IO.Path.GetFileName(path);
path = System.IO.Path.GetDirectoryName(path);
if (path.StartsWith(System.IO.Path.DirectorySeparatorChar.ToString()) || path.StartsWith(System.IO.Path.AltDirectorySeparatorChar.ToString()))
{
path = path.Remove(0, 1);
}
folder = path;
isStreamingAsset = false;
}
}
else
{
// Must be absolute path
filename = System.IO.Path.GetFileName(path);
folder = System.IO.Path.GetDirectoryName(path);
isStreamingAsset = false;
}
result = true;
}
return result;
}
public override void OnInspectorGUI()
{
_movie = (this.target) as AVProWindowsMediaMovie;
EditorGUILayout.Separator();
GUILayout.Label("File Location", EditorStyles.boldLabel);
//DrawDefaultInspector();
_movie._useStreamingAssetsPath = EditorGUILayout.Toggle("Use StreamingAssets", _movie._useStreamingAssetsPath);
_movie._folder = EditorGUILayout.TextField("Folder", _movie._folder);
_movie._filename = EditorGUILayout.TextField("Filename", _movie._filename);
GUILayout.BeginHorizontal();
GUI.enabled = System.IO.File.Exists(_movie.GetFilePath());
#if UNITY_EDITOR_WIN
if (GUILayout.Button("Show"))
{
ShowInExplorer(_movie.GetFilePath());
}
#endif
GUI.enabled &= _movie._useStreamingAssetsPath;
if (GUILayout.Button("Select"))
{
string projectRoot = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, ".."));
projectRoot = projectRoot.Replace('\\', '/');
string path = _movie.GetFilePath();
path = path.Remove(0, projectRoot.Length + 1);
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(path);
}
GUILayout.EndHorizontal();
GUI.enabled = true;
GUI.color = Color.green;
if (GUILayout.Button("BROWSE"))
{
Browse(Application.streamingAssetsPath, ref _movie._filename, ref _movie._folder, ref _movie._useStreamingAssetsPath);
}
GUI.color = Color.white;
if (string.IsNullOrEmpty(_movie.GetFilePath()))
{
if (_movie._loadOnStart)
{
GUI.color = Color.red;
GUILayout.TextArea("Error: No file specfied");
GUI.color = Color.white;
}
else
{
GUI.color = Color.yellow;
GUILayout.TextArea("Warning: No file specfied");
GUI.color = Color.white;
}
}
else if (!System.IO.File.Exists(_movie.GetFilePath()))
{
GUI.color = Color.red;
GUILayout.TextArea("Error: File not found");
GUI.color = Color.white;
}
else
{
if (!_movie._useStreamingAssetsPath)
{
GUI.color = Color.yellow;
GUILayout.TextArea("Warning: Files not in StreamingAssets will require manual copying for builds");
GUI.color = Color.white;
}
}
if (System.IO.Path.IsPathRooted(_movie._folder))
{
GUI.color = Color.yellow;
GUILayout.TextArea("Warning: Absolute path is not ideal. Better to use files relative to the project root");
GUI.color = Color.white;
}
GUILayout.Space(10f);
EditorGUILayout.Separator();
GUILayout.Label("Load Options", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Colour Format");
_movie._colourFormat = (AVProWindowsMediaMovie.ColourFormat)EditorGUILayout.EnumPopup(_movie._colourFormat);
EditorGUILayout.EndHorizontal();
_movie._useDisplaySync = EditorGUILayout.Toggle("Use Display Sync", _movie._useDisplaySync);
if (_movie._useDisplaySync)
{
if (
#if UNITY_5
PlayerSettings.d3d11FullscreenMode != D3D11FullscreenMode.ExclusiveMode ||
#endif
PlayerSettings.d3d9FullscreenMode != D3D9FullscreenMode.ExclusiveMode)
{
GUI.color = Color.cyan;
GUILayout.TextArea("Perf: For display sync fullscreen mode should be set to EXCLUSIVE in Player Settings");
GUI.color = Color.white;
}
if (QualitySettings.vSyncCount != 1 && QualitySettings.vSyncCount != 2)
{
GUI.color = Color.cyan;
GUILayout.TextArea("Perf: For display sync vsync must be set to 1 or 2 in Quality Settings");
GUI.color = Color.white;
}
}
_movie._allowAudio = EditorGUILayout.Toggle("Allow Audio", _movie._allowAudio);
GUI.enabled = _movie._allowAudio;
{
_movie._useAudioDelay = EditorGUILayout.Toggle("Use Audio Delay", _movie._useAudioDelay);
_movie._useAudioMixer = EditorGUILayout.Toggle("Use Audio Mixer", _movie._useAudioMixer);
}
GUI.enabled = true;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Texture Filter");
_movie._textureFilterMode = (FilterMode)EditorGUILayout.EnumPopup(_movie._textureFilterMode);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Texture Wrap");
_movie._textureWrapMode = (TextureWrapMode)EditorGUILayout.EnumPopup(_movie._textureWrapMode);
EditorGUILayout.EndHorizontal();
EditorGUILayout.Separator();
EditorGUILayout.Separator();
EditorGUILayout.LabelField("Start Options", EditorStyles.boldLabel);
_movie._loadOnStart = EditorGUILayout.Toggle("Load On Start", _movie._loadOnStart);
GUI.enabled = _movie._loadOnStart;
if (!_movie._loadOnStart)
_movie._playOnStart = false;
_movie._playOnStart = EditorGUILayout.Toggle("Play On Start", _movie._playOnStart);
GUI.enabled = true;
EditorGUILayout.Separator();
EditorGUILayout.Separator();
EditorGUILayout.Separator();
EditorGUILayout.Separator();
EditorGUILayout.LabelField("Performance", EditorStyles.boldLabel);
_movie._ignoreFlips = EditorGUILayout.Toggle("Ignore Flips", _movie._ignoreFlips);
EditorGUILayout.Separator();
EditorGUILayout.Separator();
EditorGUILayout.Separator();
GUILayout.Label("Playback", EditorStyles.boldLabel);
_movie._loop = EditorGUILayout.Toggle("Loop", _movie._loop);
//_movie._editorPreview = EditorGUILayout.Toggle("Editor Preview", _movie._editorPreview);
GUI.enabled = _movie._allowAudio;
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Audio Volume");
_movie._volume = EditorGUILayout.Slider(_movie._volume, 0.0f, 1.0f);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Audio Balance");
_movie._audioBalance = EditorGUILayout.Slider(_movie._audioBalance, -1.0f, 1.0f);
EditorGUILayout.EndHorizontal();
}
GUI.enabled = true;
GUILayout.Space(8.0f);
SerializedProperty tps = serializedObject.FindProperty("_clips");
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(tps, new GUIContent("Clips"), true);
if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
}
GUILayout.Space(8.0f);
if (!Application.isPlaying)
{
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Load"))
{
if (AVProWindowsMediaManager.Instance != null)
{
_movie.LoadMovie(_movie._playOnStart);
}
}
#if UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 && !UNITY_5_3
using (var scope = new EditorGUI.DisabledScope(_movie.MovieInstance == null))
#else
EditorGUI.BeginDisabledGroup(_movie.MovieInstance == null);
#endif
{
if (GUILayout.Button("Unload"))
{
_movie.UnloadMovie();
}
}
#if UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 && !UNITY_5_3
#else
EditorGUI.EndDisabledGroup();
#endif
EditorGUILayout.EndHorizontal();
}
AVProWindowsMedia media = _movie.MovieInstance;
if (media != null)
{
GUI.enabled = (_movie != null && _movie.MovieInstance != null);
_movie._editorPreview = EditorGUILayout.Foldout(_movie._editorPreview, "Video Preview");
GUI.enabled = true;
if (_movie._editorPreview && _movie.MovieInstance != null)
{
{
Texture texture = _movie.OutputTexture;
if (texture == null)
texture = EditorGUIUtility.whiteTexture;
float ratio = (float)texture.width / (float)texture.height;
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
Rect textureRect = GUILayoutUtility.GetRect(Screen.width/2, Screen.width/2, (Screen.width / 2) / ratio, (Screen.width / 2) / ratio);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
_showAlpha = GUILayout.Toggle(_showAlpha, "Show Alpha Channel");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
Matrix4x4 prevMatrix = GUI.matrix;
if (_movie.MovieInstance.RequiresFlipY)
{
GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), new Vector2(0, textureRect.y + (textureRect.height / 2)));
}
if (!_showAlpha)
GUI.DrawTexture(textureRect, texture, ScaleMode.ScaleToFit);
else
EditorGUI.DrawTextureAlpha(textureRect, texture, ScaleMode.ScaleToFit);
GUI.matrix = prevMatrix;
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
GUILayout.FlexibleSpace();
if (GUILayout.Button("Select Texture", GUILayout.ExpandWidth(false)))
{
Selection.activeObject = texture;
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label(string.Format("{0}x{1} @ {2}fps {3} secs", media.Width, media.Height, media.FrameRate.ToString("F2"), media.DurationSeconds.ToString("F2")));
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (media.FramesTotal > 30)
{
GUILayout.Label("Displaying at " + media.DisplayFPS.ToString("F1") + " fps");
}
else
{
GUILayout.Label("Displaying at ... fps");
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
if (_movie.enabled)
{
GUILayout.Space(8.0f);
EditorGUILayout.LabelField("Frame:");
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.ExpandWidth(false)))
{
media.PositionFrames--;
}
uint currentFrame = media.PositionFrames;
if (currentFrame != uint.MaxValue)
{
int newFrame = EditorGUILayout.IntSlider((int)currentFrame, 0, (int)media.LastFrame);
if (newFrame != currentFrame)
{
media.PositionFrames = (uint)newFrame;
}
}
if (GUILayout.Button(">", GUILayout.ExpandWidth(false)))
{
media.PositionFrames++;
}
EditorGUILayout.EndHorizontal();
if (_movie.NumClips > 0)
{
EditorGUILayout.Separator();
EditorGUILayout.LabelField("Clips", EditorStyles.boldLabel);
for (int i = 0; i < _movie.NumClips; i++)
{
GUILayout.BeginHorizontal();
string clipName = _movie.GetClipName(i);
GUILayout.Label(clipName);
if (GUILayout.Button("Loop"))
{
_movie.PlayClip(clipName, true, false);
}
GUILayout.EndHorizontal();
}
if (GUILayout.Button("Reset Clip"))
_movie.ResetClip();
EditorGUILayout.Separator();
}
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Rewind"))
{
_movie.MovieInstance.Rewind();
}
if (!media.IsPlaying)
{
if (GUILayout.Button("Play"))
{
_movie.Play();
}
}
else
{
if (GUILayout.Button("Pause"))
{
_movie.Pause();
}
}
EditorGUILayout.EndHorizontal();
#if !AVPROWINDOWSMEDIA_UNITYFEATURE_EDITORAUTOREFRESH
this.Repaint();
#endif
}
}
}
if (GUI.changed)
{
EditorUtility.SetDirty(_movie);
}
// If the app isn't running but the media is we may need to manually update it
if (!Application.isPlaying && _movie.MovieInstance != null)
{
_movie.Update();
AVProWindowsMediaManager.Instance.Update();
EditorUtility.SetDirty(_movie);
}
}
}