1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【エディタ拡張】ProjectWindowのフォルダ開閉状態をリセットするエディタ拡張

Posted at

概要

開きまくっていたプロジェクトウィンドウ上のフォルダ開閉状態をいったんリセットしたい場合に有効なエディタ拡張です。

使い方

ProjectCollapse.gif

  • Window/ProjectBrowserHelperで開きます。
  • Collapse All

※TwoColumnレイアウトだと動作が完全ではなく、全部閉じるではなく、
現在のフォルダ以下のものだけが閉じられる挙動になっております。

環境

  • Unity2019.4.13f1~

コード

Gistにもアップしました。

ProjectBrowserHelperWindow.cs
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class ProjectBrowserHelperWindow : EditorWindow
{
    [MenuItem("Window/ProjectBrowserHelper")]
    static void Init()
    {
        GetWindow<ProjectBrowserHelperWindow>();
    }

    void OnEnable()
    {
        var collapseAllButton = new Button(() => OnClickCollapseAll());
        collapseAllButton.text = "Collapse All";
        rootVisualElement.Add(collapseAllButton);
    }

    void OnClickCollapseAll()
    {
        Selection.activeObject = null;
        var window =
        Resources.FindObjectsOfTypeAll<EditorWindow>().FirstOrDefault(x => x.GetType().Name == "ProjectBrowser");
        if (window == null) { return; }

        new InternalProjectBrowser(window).ClearExpandedIDs();
        window.Repaint();
    }

    /// <summary>
    /// UnityEditor.ProjectBrowserのリフレクション
    /// </summary>
    class InternalProjectBrowser
    {
        SerializedObject so;

        SerializedProperty AssetTreeExpandedIDsProperty => so.FindProperty("m_AssetTreeState.m_ExpandedIDs");
        SerializedProperty FolderTreeExpandedIDsProperty => so.FindProperty("m_FolderTreeState.m_ExpandedIDs");

        public InternalProjectBrowser(EditorWindow ProjectWindow)
        {
            so = new SerializedObject(ProjectWindow);
        }

        public void ClearExpandedIDs()
        {
            so.Update();
            AssetTreeExpandedIDsProperty.ClearArray();
            FolderTreeExpandedIDsProperty.ClearArray();
            AssetTreeExpandedIDsProperty.arraySize = 0;
            FolderTreeExpandedIDsProperty.arraySize = 0;
            so.ApplyModifiedProperties();
            Reload();
        }

        void Reload()
        {
            var methodInfo = so.targetObject.GetType().GetMethod("ResetViews", BindingFlags.Instance | BindingFlags.NonPublic);
            methodInfo.Invoke(so.targetObject, new object[0]);
        }
    }
}

まとめ

フォルダの開閉状態を一気に閉じたい場合、便利かと思います。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?