using System.IO; using UnityEditor; using UnityEngine; using uc; [CustomEditor(typeof(DShowClip))] public class DShowClipEditor : PlayerPreviewEditorBase { private DShowPlayer _player; protected override DShowPlayer player { get { if (_player == null) _player = new DShowPlayer(); return _player; } } protected override DShowClip clip { get { return target as DShowClip; } } public void OnDestroy() { player.release(); } public override GUIContent GetPreviewTitle() { return new GUIContent("DShowClip"); } [MenuItem("Assets/Create/UltraCombos/DShowClip")] public static void CreateAsset() { CreateAsset(); } private static void CreateAsset() where T : ScriptableObject { T asset = ScriptableObject.CreateInstance(); string path = AssetDatabase.GetAssetPath(Selection.activeObject); if (path == "") { path = "Assets"; } else if (Path.GetExtension(path) != "") { path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), ""); } string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New " + typeof(T).ToString() + ".asset"); AssetDatabase.CreateAsset(asset, assetPathAndName); AssetDatabase.SaveAssets(); EditorUtility.FocusProjectWindow(); Selection.activeObject = asset; } private SerializedProperty _propLocation; private SerializedProperty _propPath; private static GUIStyle _mediaNameStyle = null; #if UNITY_EDITOR_OSX private const string MediaExtensions = "mp4,m4v,mov,avi,mp3,m4a,aac,ac3,au,aiff,wav"; #else //private const string MediaExtensions = "Media Files;*.mp4;*.mov;*.m4v;*.avi;*.mkv;*.ts;*.webm;*.flv;*.vob;*.ogg;*.ogv;*.mpg;*.wmv;*.3gp;Audio Files;*wav;*.mp3;*.mp2;*.m4a;*.wma;*.aac;*.au;*.flac"; private const string MediaExtensions = "Media Files;*.avi"; #endif void OnEnable() { _propLocation = serializedObject.FindProperty("m_VideoLocation"); _propPath = serializedObject.FindProperty("m_VideoPath"); } private static bool IsPathWithin(string fullPath, string targetPath) { return fullPath.StartsWith(targetPath); } private static string GetPathRelativeTo(string root, string fullPath) { #if true return fullPath.GetRelativePath(root); #else string result = fullPath.Remove(0, root.Length); if (result.StartsWith(System.IO.Path.DirectorySeparatorChar.ToString()) || result.StartsWith(System.IO.Path.AltDirectorySeparatorChar.ToString())) { result = result.Remove(0, 1); } return result; #endif } public override void OnInspectorGUI() { DShowClip media = (this.target) as DShowClip; serializedObject.Update(); if (media == null || _propLocation == null) { return; } GUILayout.Space(6f); /////////////////// FILE PATH // Display the file name and buttons to load new files OnInspectorGUI_CopyableFilename(media.m_VideoPath); EditorGUILayout.LabelField("Source Path", EditorStyles.boldLabel); EditorGUILayout.PropertyField(_propLocation, GUIContent.none); { string oldPath = _propPath.stringValue; string newPath = EditorGUILayout.TextField(string.Empty, _propPath.stringValue); if (newPath != oldPath) { // Check for invalid characters if (0 > newPath.IndexOfAny(System.IO.Path.GetInvalidPathChars())) { _propPath.stringValue = newPath.Replace("\\", "/"); EditorUtility.SetDirty(target); } } } //if (!Application.isPlaying) { GUILayout.BeginHorizontal(); GUI.color = Color.green; if (GUILayout.Button("BROWSE")) { string startFolder = GetStartFolder(_propPath.stringValue, (DShowClip.FileLocation)_propLocation.enumValueIndex); string videoPath = media.m_VideoPath; string fullPath = string.Empty; DShowClip.FileLocation fileLocation = media.m_VideoLocation; if (Browse(startFolder, ref videoPath, ref fileLocation, ref fullPath, MediaExtensions)) { _propPath.stringValue = videoPath.Replace("\\", "/"); _propLocation.enumValueIndex = (int)fileLocation; EditorUtility.SetDirty(target); } } GUI.color = Color.white; GUILayout.EndHorizontal(); //ShowFileWarningMessages(_propPath.stringValue, (FileLocation)_propLocation.enumValueIndex, media.m_AutoOpen, Platform.Unknown); GUI.color = Color.white; } GUILayout.BeginHorizontal(); if (GUILayout.Button("SpawnAnimation")) { MakeAnimationCaller((DShowClip)target); } GUILayout.EndHorizontal(); //////////////////// PREVIEW //OnInspectorGUI_Preview(); if (serializedObject.ApplyModifiedProperties()) { EditorUtility.SetDirty(target); } } private void OnInspectorGUI_CopyableFilename(string path) { // Display the file name so it's easy to read and copy to the clipboard if (!string.IsNullOrEmpty(path) && 0 > path.IndexOfAny(System.IO.Path.GetInvalidPathChars())) { // Some GUI hacks here because SelectableLabel wants to be double height and it doesn't want to be centered because it's an EditorGUILayout function... GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f, 0.1f); if (EditorGUIUtility.isProSkin) { GUI.backgroundColor = Color.black; GUI.color = Color.cyan; } string text = System.IO.Path.GetFileName(path); if (_mediaNameStyle == null) { _mediaNameStyle = new GUIStyle(EditorStyles.wordWrappedLabel); _mediaNameStyle.fontStyle = FontStyle.Bold; _mediaNameStyle.stretchWidth = true; _mediaNameStyle.stretchHeight = true; _mediaNameStyle.alignment = TextAnchor.MiddleCenter; _mediaNameStyle.margin.top = 8; _mediaNameStyle.margin.bottom = 16; } float height = _mediaNameStyle.CalcHeight(new GUIContent(text), Screen.width) * 1.5f; EditorGUILayout.SelectableLabel(text, _mediaNameStyle, GUILayout.Height(height), GUILayout.ExpandHeight(false), GUILayout.ExpandWidth(true)); GUI.color = Color.white; GUI.backgroundColor = Color.white; } } private static string GetStartFolder(string path, DShowClip.FileLocation fileLocation) { // Try to resolve based on file path + file location string result = DShowClip.GetFilePath(path, fileLocation); if (!string.IsNullOrEmpty(result)) { if (System.IO.File.Exists(result)) { result = System.IO.Path.GetDirectoryName(result); } } if (!System.IO.Directory.Exists(result)) { // Just resolve on file location result = DShowClip.GetPath(fileLocation); } if (string.IsNullOrEmpty(result)) { // Fallback result = Application.streamingAssetsPath; } return result; } private static bool Browse(string startPath, ref string filePath, ref DShowClip.FileLocation fileLocation, ref string fullPath, string extensions) { bool result = false; string path = UnityEditor.EditorUtility.OpenFilePanel("Browse Video File", startPath, extensions); if (!string.IsNullOrEmpty(path) && !path.EndsWith(".meta")) { fullPath = path; GetRelativeLocationFromPath(path, ref filePath, ref fileLocation); result = true; } return result; } private static void GetRelativeLocationFromPath(string path, ref string filePath, ref DShowClip.FileLocation fileLocation) { string projectRoot = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, "..")); projectRoot = projectRoot.Replace('\\', '/'); #if true switch(fileLocation) { case DShowClip.FileLocation.RelativeToStreamingAssetsFolder: // Must be StreamingAssets relative path filePath = GetPathRelativeTo(Application.streamingAssetsPath, path); fileLocation = DShowClip.FileLocation.RelativeToStreamingAssetsFolder; break; case DShowClip.FileLocation.RelativeToDataFolder: // Must be Assets relative path filePath = GetPathRelativeTo(Application.dataPath, path); fileLocation = DShowClip.FileLocation.RelativeToDataFolder; break; case DShowClip.FileLocation.RelativeToProjectFolder: // Must be project relative path filePath = GetPathRelativeTo(projectRoot, path); fileLocation = DShowClip.FileLocation.RelativeToProjectFolder; break; case DShowClip.FileLocation.RelativeToPeristentDataFolder: filePath = GetPathRelativeTo(Application.persistentDataPath, path); fileLocation = DShowClip.FileLocation.RelativeToPeristentDataFolder; break; case DShowClip.FileLocation.AbsolutePathOrURL: default: // Must be absolute path filePath = path; fileLocation = DShowClip.FileLocation.AbsolutePathOrURL; break; } #else if (path.StartsWith(projectRoot)) { if (path.StartsWith(Application.streamingAssetsPath)) { // Must be StreamingAssets relative path filePath = GetPathRelativeTo(Application.streamingAssetsPath, path); fileLocation = DShowClip.FileLocation.RelativeToStreamingAssetsFolder; } else if (path.StartsWith(Application.dataPath)) { // Must be Assets relative path filePath = GetPathRelativeTo(Application.dataPath, path); fileLocation = DShowClip.FileLocation.RelativeToDataFolder; } else { // Must be project relative path filePath = GetPathRelativeTo(projectRoot, path); fileLocation = DShowClip.FileLocation.RelativeToProjectFolder; } } else { // Must be persistant data if (path.StartsWith(Application.persistentDataPath)) { filePath = GetPathRelativeTo(Application.persistentDataPath, path); fileLocation = DShowClip.FileLocation.RelativeToPeristentDataFolder; } // Must be absolute path filePath = path; fileLocation = DShowClip.FileLocation.AbsolutePathOrURL; } #endif } public void MakeAnimationCaller(DShowClip DSclip) { makeAnime(DSclip); } void makeAnime(DShowClip DSclips) { AnimationClip NewClip = new AnimationClip(); NewClip.wrapMode = WrapMode.Loop; SerializedObject serializedClip = new SerializedObject(NewClip); SerializedProperty settings = serializedClip.FindProperty("m_AnimationClipSettings"); while (settings.Next(true)) { if (settings.name == "m_LoopTime") { break; } } settings.boolValue = true; serializedClip.ApplyModifiedProperties(); { ObjectReferenceKeyframe[] Keyframes = new ObjectReferenceKeyframe[2]; Keyframes[0] = new ObjectReferenceKeyframe(); Keyframes[0].time = 0.1f; //Keyframes[0].value = DSclips; Keyframes[1] = new ObjectReferenceKeyframe(); Keyframes[1].time = player.GetDuration; EditorCurveBinding curveBinding = new EditorCurveBinding(); curveBinding.type = typeof(DShowListMoviePlayer); curveBinding.path = string.Empty; curveBinding.propertyName = "videoAsset"; AnimationUtility.SetObjectReferenceCurve(NewClip, curveBinding, Keyframes); } { AnimationEvent[] m_event = new AnimationEvent[1]; m_event[0] = new AnimationEvent(); //m_event[0].stringParameter = DSclips.name; m_event[0].objectReferenceParameter = DSclips; m_event[0].functionName = "Play"; m_event[0].time = 0; AnimationUtility.SetAnimationEvents(NewClip, m_event); } { string filePath = AssetDatabase.GetAssetPath(DSclips); string[] PathSp = filePath.Split(new char[] { '.' }); if (PathSp.Length > 1) { filePath = ""; for (int i = 0; i < PathSp.Length - 1; i++) { filePath += PathSp[i]; } } AssetDatabase.CreateAsset(NewClip, filePath + ".anim"); } } }