Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using System.IO;
  6. using System.Reflection;
  7.  
  8. [InitializeOnLoad]
  9. public static class ShowFileExtensions
  10. {
  11. // ================================================================================
  12. // static constructor
  13. // --------------------------------------------------------------------------------
  14.  
  15. static ShowFileExtensions()
  16. {
  17. EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
  18. }
  19.  
  20. // ================================================================================
  21. // checking editor events
  22. // --------------------------------------------------------------------------------
  23.  
  24. private static void ProjectWindowItemOnGUI(string guid, Rect rect)
  25. {
  26. if (Event.current.alt)
  27. {
  28. EditorWindow window = GetProjectWindow();
  29.  
  30. string assetPath = AssetDatabase.GUIDToAssetPath(guid);
  31. UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
  32.  
  33. if (obj != null && AssetDatabase.IsMainAsset(obj) && !IsDirectory(obj))
  34. {
  35. if (showsBigIcons)
  36. {
  37. // just draw it bold in upper left
  38. string extension = Path.GetExtension(assetPath);
  39. GUI.Label(rect, extension, EditorStyles.boldLabel);
  40. }
  41. else
  42. {
  43. // we overpaint the filename here, does look a bit dirty and might need adjustment of the offset
  44. var labelRect = rect.Translate(new Vector2(19f, 1f));
  45. var fileName = Path.GetFileName(assetPath);
  46. GUI.Label(labelRect, fileName);
  47. }
  48. }
  49.  
  50. EditorApplication.RepaintProjectWindow();
  51. }
  52. }
  53.  
  54. // ================================================================================
  55. // getting infos about the project window
  56. // --------------------------------------------------------------------------------
  57.  
  58. private static bool showsBigIcons
  59. {
  60. get
  61. {
  62. return isTwoColumnMode && listAreaGridSize > 16f;
  63. }
  64. }
  65.  
  66. private static bool isTwoColumnMode
  67. {
  68. get
  69. {
  70. var projectWindow = GetProjectWindow();
  71.  
  72. var projectWindowType = projectWindow.GetType();
  73. var modeFieldInfo = projectWindowType.GetField("m_ViewMode", BindingFlags.Instance | BindingFlags.NonPublic);
  74.  
  75. int mode = (int)modeFieldInfo.GetValue(projectWindow);
  76.  
  77. // 0 == ViewMode.OneColumn
  78. // 1 == ViewMode.TwoColum
  79.  
  80. return mode == 1;
  81. }
  82. }
  83.  
  84. private static float listAreaGridSize
  85. {
  86. get
  87. {
  88. var projectWindow = GetProjectWindow();
  89.  
  90. var projectWindowType = projectWindow.GetType();
  91. var propertyInfo = projectWindowType.GetProperty("listAreaGridSize", BindingFlags.Instance | BindingFlags.Public);
  92. return (float)propertyInfo.GetValue(projectWindow, null);
  93. }
  94. }
  95.  
  96. /// <summary>
  97. /// there's a chance here we get the wrong one when two project windows are open
  98. /// </summary>
  99. private static EditorWindow GetProjectWindow()
  100. {
  101. if (EditorWindow.focusedWindow != null && EditorWindow.focusedWindow.titleContent.text == "Project")
  102. {
  103. return EditorWindow.focusedWindow;
  104. }
  105.  
  106. return GetExistingWindowByName("Project");
  107. }
  108.  
  109. private static EditorWindow GetExistingWindowByName(string name)
  110. {
  111. EditorWindow[] windows = Resources.FindObjectsOfTypeAll<EditorWindow>();
  112. foreach (var item in windows)
  113. {
  114. if (item.titleContent.text == name)
  115. {
  116. return item;
  117. }
  118. }
  119.  
  120. return default(EditorWindow);
  121. }
  122.  
  123. // ================================================================================
  124. // utilities
  125. // --------------------------------------------------------------------------------
  126.  
  127. private static Rect Translate(this Rect rect, Vector2 delta)
  128. {
  129. rect.x += delta.x;
  130. rect.y += delta.y;
  131.  
  132. return rect;
  133. }
  134.  
  135. private static bool IsDirectory(UnityEngine.Object obj)
  136. {
  137. if (obj == null)
  138. {
  139. return false;
  140. }
  141.  
  142. return obj is DefaultAsset && !AssetDatabase.IsForeignAsset(obj);
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement