zoukankan      html  css  js  c++  java
  • unity中project视图下,Alt+左键显示文件扩展名

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEditor;
    using System.IO;
    using System.Reflection;
    
    [InitializeOnLoad]
    public static class ShowFileExtensions
    {
        // ================================================================================
        //  static constructor
        // --------------------------------------------------------------------------------
    
        static ShowFileExtensions()
        {
            EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
        }
    
        // ================================================================================
        //  checking editor events
        // --------------------------------------------------------------------------------
    
        private static void ProjectWindowItemOnGUI(string guid, Rect rect)
        {
            if (Event.current.alt)
            {
                EditorWindow window = GetProjectWindow();            
    
                string assetPath = AssetDatabase.GUIDToAssetPath(guid);
                UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
    
                if (obj != null && AssetDatabase.IsMainAsset(obj) && !IsDirectory(obj))
                {
                    if (showsBigIcons)
                    {
                        // just draw it bold in upper left
                        string extension = Path.GetExtension(assetPath);
                        GUI.Label(rect, extension, EditorStyles.boldLabel);
                    }
                    else
                    {
                        // we overpaint the filename here, does look a bit dirty and might need adjustment of the offset
                        var labelRect = rect.Translate(new Vector2(16f, 1f));
                        var fileName = Path.GetFileName(assetPath);
                        GUI.Label(labelRect, fileName);
                    }
                }
    
                EditorApplication.RepaintProjectWindow();
            }
        }
    
        // ================================================================================
        //  getting infos about the project window
        // --------------------------------------------------------------------------------
    
        private static bool showsBigIcons
        {
            get
            {
                return isTwoColumnMode && listAreaGridSize > 16f;
            }
        }
    
        private static bool isTwoColumnMode
        {
            get
            {
                var projectWindow = GetProjectWindow();
    
                var projectWindowType = projectWindow.GetType();
                var modeFieldInfo = projectWindowType.GetField("m_ViewMode", BindingFlags.Instance | BindingFlags.NonPublic);
    
                int mode = (int)modeFieldInfo.GetValue(projectWindow);
    
                // 0 == ViewMode.OneColumn
                // 1 == ViewMode.TwoColum
    
                return mode == 1;
            }
        }
    
        private static float listAreaGridSize
        {
            get
            {
                var projectWindow = GetProjectWindow();
    
                var projectWindowType = projectWindow.GetType();
                var propertyInfo = projectWindowType.GetProperty("listAreaGridSize", BindingFlags.Instance | BindingFlags.Public);
                return (float)propertyInfo.GetValue(projectWindow, null);
            }
        }
    
        /// <summary>
        /// there's a chance here we get the wrong one when two project windows are open
        /// </summary>
        private static EditorWindow GetProjectWindow()
        {
            if (EditorWindow.focusedWindow != null && EditorWindow.focusedWindow.titleContent.text == "Project")
            {
                return EditorWindow.focusedWindow;
            }
    
            return GetExistingWindowByName("Project");
        }
    
        private static EditorWindow GetExistingWindowByName(string name)
        {
            EditorWindow[] windows = Resources.FindObjectsOfTypeAll<EditorWindow>();
            foreach (var item in windows)
            {
                if (item.titleContent.text == name)
                {
                    return item;
                }
            }
    
            return default(EditorWindow);
        }
    
        // ================================================================================
        //  utilities
        // --------------------------------------------------------------------------------
    
        private static Rect Translate(this Rect rect, Vector2 delta)
        {
            rect.x += delta.x;
            rect.y += delta.y;
    
            return rect;
        }
    
        private static bool IsDirectory(UnityEngine.Object obj)
        {
            if (obj == null)
            {
                return false;
            }
    
            return obj is DefaultAsset && !AssetDatabase.IsForeignAsset(obj);
        }
    }

    转自:https://gist.github.com/talecrafter/1009efa95cbcf6e01958efb887def90b

  • 相关阅读:
    hdu4734 F(x)
    hdu2089 不要62 两解
    luogu2602 [ZJOI2010]数字计数 两解
    lemon
    UVA1218 完美的服务 Perfect Service
    luogu HNOI2003消防局的设立
    uva10891 game of sum
    uva10635 Prince and Princess
    UVA1394 And Then There Was One
    uva10003切木棍
  • 原文地址:https://www.cnblogs.com/Yellow0-0River/p/6513192.html
Copyright © 2011-2022 走看看