Search Unity

Custom editor, how to select an empty project folder in code

Discussion in 'Scripting' started by Munchy2007, Oct 20, 2020.

  1. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Hi all,

    as the title says, I'm struggling to work out a way to select a project folder from code if it's empty, and extensive Googling hasn't provided a solution as yet.

    I'm okay if the folder has something in it, because I can use Selection.activeGameObject to select the object in the folder and in so doing select the folder, but for the life of me I can't find out a way to select an empty folder.

    I'm starting to think it's actually not possible as things stand, but I'm hoping someone can tell me otherwise :)
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    I'm not sure this is what you want to do but if you're just looking to select an empty folder in the editor's Project window you can do this:

    Code (CSharp):
    1. EditorUtility.FocusProjectWindow();
    2. UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>("Assets/MyNewEmptyFolder");
    3. Selection.activeObject = obj;
     
    Munchy2007 likes this.
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Yep, that does the job, thanks very much!

    I'd already tried something very similar AssetDatabase.LoadAssetAtPath<DefaultAsset>("Folder Path"), which returned null. So near and yet so far! :)
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Great, maybe because it's a folder the 'DefaultAsset' type was too specific? Glad it's working anyway!
     
    Munchy2007 likes this.