• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by NMD83 · Nov 24, 2019 at 10:06 PM · editoreditor-scriptingmacmacosxfolders

Is it possible to group folders together at the top on Unity for MacOS? (Like on Windows)

Pretty straight forward...

On Windows folders are sorted together at the top of the file list before their sibling files in the same folder.

I have found the Finder setting to accomplish this, which works for Finder, but it has no effect on how Unity operates
: https://lifehacker.com/sort-folders-and-files-on-mac-just-like-windows-with-a-1788709887

I also found a forum post from 2013 with an extension that is designed to fix this. However despite fixing the missing "&" symbols in the script, it still does not seem to work: https://forum.unity.com/threads/project-window-extension-script-folders-sorting-double-click-expand-collapse.180186/

I ALSO found a Unity Answers question on this from 2015, to which the first response came from me, today, 4.5 years later: https://answers.unity.com/questions/991484/project-view-folder-grouping-in-osx.html

REALLY hoping someone can help me out here because it's driving me insane.

Comment
Alejandro-Martinez-Chacin
JohnnyFactor
murat_unity348
wethings

People who like this

4 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by WeslomPo · Feb 23, 2020 at 08:42 PM

Hi @NMD83 ! How are you?

So, problem lay here

```

 // not actually code
 //        foreach browser in UnityEditor.ProjectBrowser.s_ProjectBrowsers
 //            browser.m_AssetTree.data.foldersFirst = true
 //            browser.m_ListArea.foldersFirst = true

 [InitializeOnLoad]
 public static class ProjectBrowserExtension {
     static ProjectBrowserExtension()
     {
         EditorApplication.projectChanged += OnChanged;
         EditorApplication.playModeStateChanged += OnPlayMode;
         EditorApplication.projectWindowItemOnGUI += OnFirstTime;
     }

     private static void OnFirstTime(string guid, Rect selectionrect)
     {
         EditorApplication.projectWindowItemOnGUI -= OnFirstTime;
         Refresh();
     }

     private static void OnChanged() => Refresh();

     private static void OnPlayMode(PlayModeStateChange obj) => Refresh();
     /// <summary>
     /// По сути, присваиваем значение определенной переменной:
     /// foreach browser in UnityEditor.ProjectBrowser.s_ProjectBrowsers
     ///     browser.m_AssetTree.data.foldersFirst = true
     ///     browser.m_ListArea.foldersFirst = true
     /// </summary>
     private static void Refresh()
     {
         var assembly = Assembly.GetAssembly(typeof(Editor));
         var projectBrowser = assembly.GetType("UnityEditor.ProjectBrowser");
         var field = projectBrowser.GetField("s_ProjectBrowsers", BindingFlags.Static | BindingFlags.NonPublic);

         if (field == null)
             return;

         var list = (IEnumerable) field.GetValue(projectBrowser);
         foreach (var pb in list)
             SetFolderFirstForProjectWindow(pb);
     }

     private static void SetFolderFirstForProjectWindow(object pb)
     {
         var members = pb.GetType().GetRuntimeFields();
         foreach (var member in members)
             switch (member.Name)
             {
                 // One column
                 case "m_AssetTree":
                     SetOneColumnFolderFirst(pb, member);
                     break;
                 // Two column
                 case "m_ListArea":
                     SetTwoColumnFolderFirst(pb, member);
                     break;
             }
     }

     private static void SetTwoColumnFolderFirst(object pb, FieldInfo listAreaField)
     {
         if (listAreaField == null)
             return;
         var listArea = listAreaField.GetValue(pb);
         var folderFirst = listArea.GetType().GetProperties().Single(x => x.Name == "foldersFirst");
         folderFirst.SetValue(listArea, true);
     }

     private static void SetOneColumnFolderFirst(object pb, FieldInfo assetTreeField)
     {
         if (assetTreeField == null)
             return;

         var assetTree = assetTreeField.GetValue(pb);
         var data = assetTree.GetType().GetRuntimeProperties().Single(x => x.Name == "data");
         // AssetsTreeViewDataSource
         var dataSource = data.GetValue(assetTree);
         var folderFirst = dataSource.GetType().GetProperties().Single(x => x.Name == "foldersFirst");
         folderFirst.SetValue(dataSource, true);
     }
 }
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by waltran · Apr 17, 2020 at 11:03 PM

i'm also looking for the same thing. this folder-file sorting is killing my production speed.

Comment
Alejandro-Martinez-Chacin

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by jnoel_bricklink · Jun 03, 2021 at 09:56 AM

Sorry to raise this post, but had to tweak some of the code to get it to work, thought I would share.

thank you @WeslomPo for the original!

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEditor;
 using System.Reflection;
 
 [InitializeOnLoad]
 public static class ProjectBrowserExtension
 {
     private const string k_UnityEditorProjectBrowserAssemblyName = "UnityEditor.ProjectBrowser";
     private const string k_ProjectBrowsersFieldName = "s_ProjectBrowsers";
     private const string k_AssetTreeFieldName = "m_AssetTree";
     private const string k_ListAreaFieldName = "m_ListArea";
     private const string k_DataFieldName = "data";
     private const string k_FoldersFirstFieldName = "foldersFirst";
 
     private static readonly object s_BoolTrue = true;
     
     static ProjectBrowserExtension()
      {
          EditorApplication.projectChanged += OnChanged;
          EditorApplication.playModeStateChanged += OnPlayMode;
          EditorApplication.projectWindowItemOnGUI += OnFirstTime;
      }
      private static void OnFirstTime(string guid, Rect _)
      {
          EditorApplication.projectWindowItemOnGUI -= OnFirstTime;
          Refresh();
      }
      private static void OnChanged() => Refresh();
      private static void OnPlayMode(PlayModeStateChange obj) => Refresh();
      
      /// <summary>
      /// foreach browser in UnityEditor.ProjectBrowser.s_ProjectBrowsers
      ///     browser.m_AssetTree.data.foldersFirst = true
      ///     browser.m_ListArea.foldersFirst = true
      /// </summary>
      private static void Refresh()
      {
          Assembly assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
          Type projectBrowser = assembly.GetType(k_UnityEditorProjectBrowserAssemblyName);
          FieldInfo field = projectBrowser.GetField(k_ProjectBrowsersFieldName, BindingFlags.Static | BindingFlags.NonPublic);
          if (field == null)
              return;
          IEnumerable list = (IEnumerable) field.GetValue(projectBrowser);
          foreach (object pb in list)
              SetFolderFirstForProjectWindow(pb);
      }
      private static void SetFolderFirstForProjectWindow(object pb)
      {
          IEnumerable<FieldInfo> members = pb.GetType().GetRuntimeFields();
          int maxMembersSought = 2;
          foreach (FieldInfo member in members)
          {
              switch (member.Name)
              {
                  // One column
                  case k_AssetTreeFieldName:
                      SetOneColumnFolderFirst(pb, member);
                      maxMembersSought--;
                      break;
                  // Two column
                  case k_ListAreaFieldName:
                      SetTwoColumnFolderFirst(pb, member);
                      maxMembersSought--;
                      break;
              }
 
              if (maxMembersSought == 0)
                  break;
          }
      }
      private static void SetTwoColumnFolderFirst(object pb, FieldInfo listAreaField)
      {
          if (listAreaField == null)
              return;
          object listArea = listAreaField.GetValue(pb);
          // safety check
          if (listArea == null)
              return;
          PropertyInfo folderFirst = listArea.GetType().GetProperties().Single(x => x.Name == k_FoldersFirstFieldName);
          folderFirst.SetValue(listArea, s_BoolTrue);
      }
      private static void SetOneColumnFolderFirst(object pb, FieldInfo assetTreeField)
      {
          if (assetTreeField == null)
              return;
          
          object assetTree = assetTreeField.GetValue(pb);
          // Fix: as we are looping all members, it's possible to end up in a case where one member is seen first, 
          // this will be null.
          if (assetTree == null)
              return;
          
          PropertyInfo data = assetTree.GetType().GetRuntimeProperties().Single(x => x.Name == k_DataFieldName);
          // AssetsTreeViewDataSource
          object dataSource = data.GetValue(assetTree);
 
          // safety check
          if (dataSource == null)
              return;
          PropertyInfo folderFirst = dataSource.GetType().GetProperties().Single(x => x.Name == k_FoldersFirstFieldName);
          folderFirst.SetValue(dataSource, s_BoolTrue);
      }
 }

,Sorry to raise this post, but had to tweak some of the code to get it to work, thought I would share.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEditor;
 using System.Reflection;
 
 [InitializeOnLoad]
 public static class ProjectBrowserExtension
 {
     private const string k_UnityEditorProjectBrowserAssemblyName = "UnityEditor.ProjectBrowser";
     private const string k_ProjectBrowsersFieldName = "s_ProjectBrowsers";
     private const string k_AssetTreeFieldName = "m_AssetTree";
     private const string k_ListAreaFieldName = "m_ListArea";
     private const string k_DataFieldName = "data";
     private const string k_FoldersFirstFieldName = "foldersFirst";
 
     private static readonly object s_BoolTrue = true;
     
     static ProjectBrowserExtension()
      {
          EditorApplication.projectChanged += OnChanged;
          EditorApplication.playModeStateChanged += OnPlayMode;
          EditorApplication.projectWindowItemOnGUI += OnFirstTime;
      }
      private static void OnFirstTime(string guid, Rect _)
      {
          EditorApplication.projectWindowItemOnGUI -= OnFirstTime;
          Refresh();
      }
      private static void OnChanged() => Refresh();
      private static void OnPlayMode(PlayModeStateChange obj) => Refresh();
      
      /// <summary>
      /// foreach browser in UnityEditor.ProjectBrowser.s_ProjectBrowsers
      ///     browser.m_AssetTree.data.foldersFirst = true
      ///     browser.m_ListArea.foldersFirst = true
      /// </summary>
      private static void Refresh()
      {
          Assembly assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
          Type projectBrowser = assembly.GetType(k_UnityEditorProjectBrowserAssemblyName);
          FieldInfo field = projectBrowser.GetField(k_ProjectBrowsersFieldName, BindingFlags.Static | BindingFlags.NonPublic);
          if (field == null)
              return;
          IEnumerable list = (IEnumerable) field.GetValue(projectBrowser);
          foreach (object pb in list)
              SetFolderFirstForProjectWindow(pb);
      }
      private static void SetFolderFirstForProjectWindow(object pb)
      {
          IEnumerable<FieldInfo> members = pb.GetType().GetRuntimeFields();
          int maxMembersSought = 2;
          foreach (FieldInfo member in members)
          {
              switch (member.Name)
              {
                  // One column
                  case k_AssetTreeFieldName:
                      SetOneColumnFolderFirst(pb, member);
                      maxMembersSought--;
                      break;
                  // Two column
                  case k_ListAreaFieldName:
                      SetTwoColumnFolderFirst(pb, member);
                      maxMembersSought--;
                      break;
              }
 
              if (maxMembersSought == 0)
                  break;
          }
      }
      private static void SetTwoColumnFolderFirst(object pb, FieldInfo listAreaField)
      {
          if (listAreaField == null)
              return;
          object listArea = listAreaField.GetValue(pb);
          // safety check
          if (listArea == null)
              return;
          PropertyInfo folderFirst = listArea.GetType().GetProperties().Single(x => x.Name == k_FoldersFirstFieldName);
          folderFirst.SetValue(listArea, s_BoolTrue);
      }
      private static void SetOneColumnFolderFirst(object pb, FieldInfo assetTreeField)
      {
          if (assetTreeField == null)
              return;
          
          object assetTree = assetTreeField.GetValue(pb);
          // Fix: as we are looping all members, it's possible to end up in a case where one member is seen first, 
          // this will be null.
          if (assetTree == null)
              return;
          
          PropertyInfo data = assetTree.GetType().GetRuntimeProperties().Single(x => x.Name == k_DataFieldName);
          // AssetsTreeViewDataSource
          object dataSource = data.GetValue(assetTree);
 
          // safety check
          if (dataSource == null)
              return;
          PropertyInfo folderFirst = dataSource.GetType().GetProperties().Single(x => x.Name == k_FoldersFirstFieldName);
          folderFirst.SetValue(dataSource, s_BoolTrue);
      }
 }

Comment
wedgiebee

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wedgiebee · May 18, 2022 at 03:17 PM 0
Share

This works great, thanks for posting! EDIT: Never mind, it stopped working randomly. Not sure what happened :( sigh

avatar image

Answer by JollyTheory · Sep 13, 2022 at 06:16 PM

Check my answer here: https://forum.unity.com/threads/project-window-extension-script-folders-sorting-double-click-expand-collapse.180186/#post-8437088 alt text ..

"The script above was breaking every time anything reloaded and you'd need to click something inside the project folder for it to sort. So instead i just directly modified UnityEngine.CoreModule.dll using dnSpy (running on windows 11 in parallels)

UnityEditor -> ProjectBrowser -> GetShouldShowFoldersFirst() -> right click -> Edit IL instructions -> replace instructions with "ldc.i4.1", "ret" (meaning 'return true;'). Save, replace the dll. Have windows style sorting forever without any scripts. Works with Unity 2021.3.9f1 for me."


screenshot-2022-09-13-205650-1.png (155.3 kB)
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

237 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OSX: Assertion failed: Shader Compiler Socket Exception: Terminating shader compiler process, 0 retries left 0 Answers

Can't code on Mac: "The reference assemblies for .NETFramework,Version=v4.7.1 were not found " 1 Answer

Sending OS X notifications from the Editor 0 Answers

Unity Editor 2018.4.20f1 - Editor Window on Macintosh is translucent and empty 0 Answers

assigning scripts to a rig via editor script tool 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges