加载中…
个人资料
潇湘雨
潇湘雨
  • 博客等级:
  • 博客积分:0
  • 博客访问:24,070
  • 关注人气:2
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

Unity功能扩展--双击Project栏文件夹展开

(2013-04-28 11:52:02)
标签:

editor

it

分类: Unity

新建脚本DoubleClickExpand.cs并放入Editor文件夹下

using System;

using System.Reflection;

using UnityEngine;

using UnityEditor;

 

[InitializeOnLoad]

public static class DoubleClickExpand
{

    static Assembly unityEditorAssembly;

 

    static Type objectBrowserType;

    static Type treeViewType;

    static Type treeViewDataType;

 

    static MethodInfo isFolderMethod;

    static MethodInfo findNodeByIDMethod;

    static MethodInfo isExpandableMethod;

    static MethodInfo isExpandedMethod;

    static MethodInfo setExpandedWithChildrenMethod;

    static MethodInfo setExpandedMethod;

    static MethodInfo getInstanceIDFromGUIDMethod;

 

    static DoubleClickExpand()
    {

        bool result = (unityEditorAssembly = Assembly.GetAssembly(typeof(Editor))) != null;

        if (result)
        {

            result &= (objectBrowserType = unityEditorAssembly.GetType              ("UnityEditor.ObjectBrowser")) != null;

            result &= (treeViewType = unityEditorAssembly.GetType("UnityEditor.TreeView")) !=      null;

            result &= (treeViewDataType = unityEditorAssembly.GetType("UnityEditor.ITreeViewDataSource")) != null;

            if (result)
            {

                result &= (isFolderMethod = objectBrowserType.GetMethod("IsFolder", BindingFlags.Static | BindingFlags.Public)) != null;

                result &= (findNodeByIDMethod = treeViewDataType.GetMethod("FindNodeByID", BindingFlags.Instance | BindingFlags.Public)) != null;

                result &= (isExpandableMethod = treeViewDataType.GetMethod("IsExpandable", BindingFlags.Instance | BindingFlags.Public)) != null;

                result &= (isExpandedMethod = treeViewDataType.GetMethod("IsExpanded", BindingFlags.Instance | BindingFlags.Public)) != null;

                result &= (setExpandedWithChildrenMethod = treeViewDataType.GetMethod("SetExpandedWithChildren", BindingFlags.Instance | BindingFlags.Public)) != null;

                result &= (setExpandedMethod = treeViewDataType.GetMethod("SetExpanded", BindingFlags.Instance | BindingFlags.Public)) != null;

                result &= (getInstanceIDFromGUIDMethod = typeof(AssetDatabase).GetMethod("GetInstanceIDFromGUID", BindingFlags.Static | BindingFlags.NonPublic)) != null;

                if (result)
                {

                    EditorApplication.projectWindowItemOnGUI += ProjectWindowItem_OnGUI;

                }

            }

        }

    }

 

    public static object GetFieldValue(object obj, Type type, string fieldName, BindingFlags flags)
    {

        FieldInfo field = type.GetField(fieldName, flags);

        return field != null ? field.GetValue(obj) : null;

    }

 

    static void ProjectWindowItem_OnGUI(string pGUID, Rect pDrawingRect)
    {

        if (Event.current.type == EventType.MouseDown

            && Event.current.clickCount == 2

            && pDrawingRect.Contains(Event.current.mousePosition))
        {

            object objectBrowser = GetFieldValue(null, objectBrowserType, "s_LastInteractedObjectBrowser", BindingFlags.Static | BindingFlags.Public);

            if (objectBrowser != null)
            {

                object assetTree = GetFieldValue(objectBrowser, objectBrowserType, "m_AssetTree", BindingFlags.Instance | BindingFlags.NonPublic);

                object folderTree = GetFieldValue(objectBrowser, objectBrowserType, "m_FolderTree", BindingFlags.Instance | BindingFlags.NonPublic);

                object treeData = null;

 

                if (assetTree != null)

                    treeData = GetFieldValue(assetTree, treeViewType, "m_Data", BindingFlags.Instance | BindingFlags.Public);

                else if (folderTree != null)

                    treeData = GetFieldValue(folderTree, treeViewType, "m_Data", BindingFlags.Instance | BindingFlags.Public);

 

                if (treeData != null)
                {

                    int instanceID = (int)getInstanceIDFromGUIDMethod.Invoke(null, new object[] { pGUID });

                    bool isFolder = (bool)isFolderMethod.Invoke(null, new object[] { instanceID });

                    if (isFolder)
                    {

                        object node = findNodeByIDMethod.Invoke(treeData, new object[] { instanceID });

                        if (node != null)
                        {

                            bool isExpandable = (bool)isExpandableMethod.Invoke(treeData, new object[] { node });

                            bool isExpanded = (bool)isExpandedMethod.Invoke(treeData, new object[] { node });

                            if (isExpandable)
                            {

                                if (Event.current.alt)
                                {

                                    if (isExpanded)

                                        setExpandedWithChildrenMethod.Invoke(treeData, new object[] { node, false });

                                    else

                                        setExpandedWithChildrenMethod.Invoke(treeData, new object[] { node, true });

                                }

                                else
                                {

                                    if (isExpanded)

                                        setExpandedMethod.Invoke(treeData, new object[] { node, false });

                                    else

                                        setExpandedMethod.Invoke(treeData, new object[] { node, true });

                                }

                            }

 

                            //Event.current.Use();

                        }

                    }

                }

            }

        }

    }

}

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有