FileNameGeneratorDrawer.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace UnityEditor.Recorder
  5. {
  6. [CustomPropertyDrawer(typeof(FileNameGenerator))]
  7. class FileNameGeneratorDrawer : TargetedPropertyDrawer<FileNameGenerator>
  8. {
  9. SerializedProperty m_FileName;
  10. SerializedProperty m_Path;
  11. static bool s_Dirty = false;
  12. static readonly GUIStyle s_PathPreviewStyle = new GUIStyle(GUI.skin.label) { wordWrap = true };
  13. static readonly GUIStyle s_OpenPathButtonStyle = new GUIStyle("minibutton") { fixedWidth = 30 };
  14. static Texture2D s_OpenPathIcon;
  15. protected override void Initialize(SerializedProperty property)
  16. {
  17. if (s_OpenPathIcon == null)
  18. {
  19. var iconName = "popout_icon";
  20. if (EditorGUIUtility.isProSkin)
  21. iconName = "d_" + iconName;
  22. s_OpenPathIcon = Resources.Load<Texture2D>(iconName);
  23. }
  24. if (target != null)
  25. return;
  26. base.Initialize(property);
  27. m_FileName = property.FindPropertyRelative("m_FileName");
  28. m_Path = property.FindPropertyRelative("m_Path");
  29. }
  30. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  31. {
  32. Initialize(property);
  33. EditorGUI.BeginProperty(position, label, property);
  34. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  35. const float tagWidth = 77;
  36. var txtWidth = position.width - tagWidth - 5;
  37. var txtRect = new Rect(position.x, position.y, txtWidth, position.height);
  38. var tagRect = new Rect(position.x + txtWidth + 5, position.y, tagWidth, position.height);
  39. GUI.SetNextControlName("FileNameField");
  40. m_FileName.stringValue = GUI.TextField(txtRect, m_FileName.stringValue);
  41. var editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
  42. if (GUI.GetNameOfFocusedControl().Equals("FileNameField") &&
  43. Event.current.type == EventType.KeyUp && (Event.current.modifiers == EventModifiers.Control || Event.current.modifiers == EventModifiers.Command))
  44. {
  45. if (Event.current.keyCode == KeyCode.C)
  46. {
  47. Event.current.Use();
  48. editor.Copy();
  49. }
  50. else if (Event.current.keyCode == KeyCode.V)
  51. {
  52. Event.current.Use();
  53. editor.Paste();
  54. m_FileName.stringValue = editor.text;
  55. }
  56. }
  57. if (EditorGUI.DropdownButton(tagRect, new GUIContent("+ Wildcards"), FocusType.Passive))
  58. {
  59. var menu = new GenericMenu();
  60. foreach (var w in target.wildcards)
  61. {
  62. var pattern = w.pattern;
  63. menu.AddItem(new GUIContent(w.label), false, () =>
  64. {
  65. m_FileName.stringValue = InsertTag(pattern, m_FileName.stringValue, editor);
  66. m_FileName.serializedObject.ApplyModifiedProperties();
  67. s_Dirty = true;
  68. });
  69. }
  70. menu.DropDown(tagRect);
  71. }
  72. if (s_Dirty)
  73. {
  74. s_Dirty = false;
  75. GUI.changed = true;
  76. }
  77. EditorGUILayout.PropertyField(m_Path);
  78. EditorGUILayout.BeginHorizontal();
  79. EditorGUILayout.PrefixLabel(" ");
  80. var path = target.BuildAbsolutePath(null);
  81. var r = GUILayoutUtility.GetRect(new GUIContent(path), s_PathPreviewStyle, null);
  82. EditorGUI.SelectableLabel(r, path, s_PathPreviewStyle);
  83. if (GUILayout.Button(s_OpenPathIcon, s_OpenPathButtonStyle))
  84. OpenInFileBrowser.Open(path);
  85. EditorGUILayout.EndHorizontal();
  86. EditorGUI.EndProperty();
  87. }
  88. static string InsertTag(string pattern, string text, TextEditor editor)
  89. {
  90. if (!string.IsNullOrEmpty(editor.text)) // HACK If editor is not focused on
  91. {
  92. try
  93. {
  94. editor.ReplaceSelection(pattern);
  95. return editor.text;
  96. }
  97. catch (Exception)
  98. {
  99. // ignored
  100. }
  101. }
  102. return text + pattern;
  103. }
  104. }
  105. // Inspired from http://wiki.unity3d.com/index.php/OpenInFileBrowser
  106. static class OpenInFileBrowser
  107. {
  108. static void OpenInOSX(string path, bool openInsideFolder)
  109. {
  110. var osxPath = path.Replace("\\", "/");
  111. if (!osxPath.StartsWith("\""))
  112. {
  113. osxPath = "\"" + osxPath;
  114. }
  115. if (!osxPath.EndsWith("\""))
  116. {
  117. osxPath = osxPath + "\"";
  118. }
  119. var arguments = (openInsideFolder ? "" : "-R ") + osxPath;
  120. try
  121. {
  122. System.Diagnostics.Process.Start("open", arguments);
  123. }
  124. catch (System.ComponentModel.Win32Exception e)
  125. {
  126. // tried to open mac finder in windows
  127. // just silently skip error
  128. // we currently have no platform define for the current OS we are in, so we resort to this
  129. e.HelpLink = ""; // do anything with this variable to silence warning about not using it
  130. }
  131. }
  132. static void OpenInWindows(string path, bool openInsideFolder)
  133. {
  134. var winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashes
  135. try
  136. {
  137. System.Diagnostics.Process.Start("explorer.exe", (openInsideFolder ? "/root," : "/select,") + winPath);
  138. }
  139. catch (System.ComponentModel.Win32Exception e)
  140. {
  141. // tried to open win explorer in mac
  142. // just silently skip error
  143. // we currently have no platform define for the current OS we are in, so we resort to this
  144. e.HelpLink = ""; // do anything with this variable to silence warning about not using it
  145. }
  146. }
  147. public static void Open(string path)
  148. {
  149. if (!File.Exists(path))
  150. path = Path.GetDirectoryName(path);
  151. var openInsideFolder = Directory.Exists(path);
  152. if (Application.platform == RuntimePlatform.WindowsEditor)
  153. {
  154. OpenInWindows(path, openInsideFolder);
  155. }
  156. else if (Application.platform == RuntimePlatform.OSXEditor)
  157. {
  158. OpenInOSX(path, openInsideFolder);
  159. }
  160. }
  161. }
  162. }