Unity 在project view快速定位类和编辑器类

Posted vicx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 在project view快速定位类和编辑器类相关的知识,希望对你有一定的参考价值。

技术分享图片

原本作为资源放在asset store 上卖,现在我把它免费了, 因为没啥技术含量的哈哈,不过很方便开发

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
public class vicUtility  {

    [MenuItem("Edit/RunOrPause _F3")] // shortcut key F3 to Play (and exit playmode also)
    static void PlayGame() {
        if (!EditorApplication.isPlaying)
            EditorApplication.ExecuteMenuItem("Edit/Play");
        else
            EditorApplication.ExecuteMenuItem("Edit/Pause");
    }
    
    [MenuItem("CONTEXT/MonoBehaviour/Pin Script", false, 620)]
    private static void PinScript(MenuCommand mc)
    {
        EditorApplication.ExecuteMenuItem("Window/Project");
        var script= MonoScript.FromMonoBehaviour(mc.context as MonoBehaviour);
        EditorGUIUtility.PingObject(script);

    }
           
    [MenuItem("CONTEXT/MonoBehaviour/Pin Editor", false, 619)]
    private static void PinEditor(MenuCommand mc)
    {
        Object editorClass;
        
        EditorApplication.ExecuteMenuItem("Window/Project");
        var script= MonoScript.FromMonoBehaviour(mc.context as MonoBehaviour);
        var guidStrs= AssetDatabase.FindAssets(script.name+"Editor");
        if(guidStrs.Length==0) 
             guidStrs= AssetDatabase.FindAssets(script.name+"Inspector");
        
        if(guidStrs.Length>0){
            var path =    AssetDatabase.GUIDToAssetPath(guidStrs[0]);
            editorClass = AssetDatabase.LoadAssetAtPath<Object>(path);
            EditorGUIUtility.PingObject(editorClass);
        }else
        {
            Debug.LogWarning("No Editor Class for this script!!!");            
        }
        

    }

    private static string undoText = "Group Selection";

    private static bool groupCreated;

    [MenuItem("GameObject/Group %g", true, 0)]
    private static bool ValidateGroupSelection()
    {
        groupCreated = false;
        return Selection.transforms.Length > 0;
    }

    [MenuItem("GameObject/Group %g", false, 0)]
    private static void GroupSelection()
    {
        if (!groupCreated)
        {
            GameObject gameObject = new GameObject("Group");
            Undo.RegisterCreatedObjectUndo(gameObject, undoText);
            if ((Object)Selection.activeTransform != (Object)null)
            {
                Undo.SetTransformParent(gameObject.GetComponent<Transform>(), Selection.activeTransform.parent, undoText);
            }
            Transform[] transforms = Selection.transforms;
            Vector3 a = Vector3.zero;
            for (int i = 0; i < transforms.Length; i++)
            {
                a += transforms[i].position;
            }
            a /= (float)transforms.Length;
            gameObject.GetComponent<Transform>().position = a;
            for (int j = 0; j < transforms.Length; j++)
            {
                Undo.SetTransformParent(transforms[j], gameObject.GetComponent<Transform>(), undoText);
            }
            groupCreated = true;
            Selection.activeGameObject = gameObject;
        }
    }
    
    [MenuItem("Assets/Copy Asset Path", true, 102)]
    private static bool ValidateCopyAssetPath()
    {
        return Selection.activeObject != (Object)null;
    }

    [MenuItem("Assets/Copy Asset Path", false, 102)]
    private static void CopyAssetPath()
    {
        PropertyInfo property = typeof(GUIUtility).GetProperty("systemCopyBuffer", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
        property.SetValue(null, GetAssetPath(Selection.activeObject), null);
    }

    private static string GetAssetPath(Object obj)
    {
        string text = AssetDatabase.GetAssetPath(obj);
        if (!Path.IsPathRooted(text))
        {
            string dataPath = Application.dataPath;
            dataPath = dataPath.Substring(0, dataPath.Length - "/Assets".Length);
            text = dataPath + "/" + text;
        }
        if (SystemInfo.operatingSystem.IndexOf("Windows") >= 0)
        {
            text = text.Replace("/", "\\");
        }
        Debug.Log("<color=aqua>"+text+"</color>");
        return text;
    }

    [MenuItem("Assets/Duplicate", true, 2000)]
    private static bool ValidateDuplicate()
    {
        return Selection.assetGUIDs.Length > 0;
    }

    [MenuItem("Assets/Duplicate", false, 2000)]
    private static void Duplicate()
    {
        List<Object> list = new List<Object>();
        Object[] objects = Selection.objects;
        foreach (Object @object in objects)
        {
            Object object2 = null;
            object2 = ((!((Object)(@object as MonoScript) != (Object)null)) ? DuplicateAsset(@object) : DuplicateScript(@object as MonoScript));
            if (object2 != (Object)null)
            {
                list.Add(object2);
            }
        }
        Selection.objects = list.ToArray();
    }

    private static Object DuplicateScript(MonoScript script)
    {
        if ((Object)script == (Object)null)
        {
            return null;
        }
        string assetPath = AssetDatabase.GetAssetPath(script);
        if (assetPath == string.Empty)
        {
            return null;
        }
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(assetPath);
        string copyPath = GetCopyPath(assetPath, string.Empty);
        string fileNameWithoutExtension2 = Path.GetFileNameWithoutExtension(copyPath);
        string text = File.ReadAllText(assetPath);
        text = text.Replace(fileNameWithoutExtension, fileNameWithoutExtension2);
        File.WriteAllText(copyPath, text);
        AssetDatabase.Refresh();
        return AssetDatabase.LoadAssetAtPath(copyPath, typeof(MonoScript));
    }

    private static Object DuplicateAsset(Object obj)
    {
        if (obj == (Object)null)
        {
            return null;
        }
        string assetPath = AssetDatabase.GetAssetPath(obj);
        if (assetPath == string.Empty)
        {
            return null;
        }
        string copyPath = GetCopyPath(assetPath, " ");
        AssetDatabase.CopyAsset(assetPath, copyPath);
        AssetDatabase.Refresh();
        return AssetDatabase.LoadAssetAtPath(copyPath, obj.GetType());
    }

    private static string GetCopyPath(string sourcePath, string separator = "")
    {
        string directoryName = Path.GetDirectoryName(sourcePath);
        string extension = Path.GetExtension(sourcePath);
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourcePath);
        string text = fileNameWithoutExtension;
        string text2 = sourcePath;
        int num = 0;
        while (File.Exists(text2))
        {
            num++;
            text = fileNameWithoutExtension + separator + num;
            text2 = directoryName + "/" + text + extension;
        }
        return text2;
    }
    
    [MenuItem("Edit/MaxWindow &1", false, 700)]
    private static void MaxWindow()
    {
        //var editorWindowType = typeof(EditorWindow);
        //if (editorWindowType == null)
        //    return;
        //var maximizedProperty = editorWindowType.GetProperty("maximized", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public|BindingFlags.GetProperty);
        //Debug.Log(curInspector.titleContent.text);
        //Debug.Log(maximizedProperty.Name);
        //bool isMax=(bool)( maximizedProperty.GetValue(curInspector,null) );
        //maximizedProperty.SetValue(curInspector,!isMax,null);
        var curWindow = EditorWindow.mouseOverWindow;
        if(curWindow)
        curWindow.maximized = ! curWindow.maximized;

    }
    [MenuItem("Edit/ShowASelectionInNewInspector &2", false, 700)]
    private static void ShowActiveObjectInNewInspector()
    {
        if (!Selection.activeObject)
            return;
        var inspectorWindowType = typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow");
        if (inspectorWindowType == null)
            return;
        var flipLockedMethod = inspectorWindowType.GetMethod("FlipLocked", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        var newInspector = (EditorWindow)EditorWindow.CreateInstance(inspectorWindowType);
        newInspector.Show(true);
        newInspector.Repaint();
        if (flipLockedMethod != null)
            flipLockedMethod.Invoke(newInspector, null);
    }
    

    [MenuItem("Edit/Test &3", false, 700)]
    private static void Fuck()
    {
        if (!Selection.activeObject)
            return;
        var xx= typeof(GameObject).GetProperty("name").GetValue(Selection.activeObject,null);
        Debug.Log("<color=aqua>"+(string)xx+"</color>");
        
    }

}

 这是一整个工具集,加了很多其他功能

以上是关于Unity 在project view快速定位类和编辑器类的主要内容,如果未能解决你的问题,请参考以下文章

Unity 快速定位UI

11.unity3d 摄像机快速定位到Scene视角

解决Android Studio启动项目后一直处于refreshing 'View' gradle project,快速解决亲测有效

Unity学习-优化_卡顿原因定位以及优化方案

仿微信通讯录右侧快速定位字母表控件的实现

在unity中,如何在脚本中根据路径找到文件,然后在project面板中用黄色的框框出来?