码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-07-19 21:10:46      阅读:467      评论:0      收藏:0      [点我收藏+]

标签:getdir   bsp   source   zed   免费   text   分享图片   cut   database   

技术分享图片

原本作为资源放在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快速定位类和编辑器类

标签:getdir   bsp   source   zed   免费   text   分享图片   cut   database   

原文地址:https://www.cnblogs.com/VicX/p/9337587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!