Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Selection.activeObject on items on left side of Project window

Discussion in 'Scripting' started by crydrk, May 11, 2017.

  1. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    74
    I've found a number of resources on how to get the path of the selection in the project window but it only works for selected objects on the right side of the divider. Is there any way to get the same info from the left side, assuming you've specifically selected or right clicked the item?

    http://answers.unity3d.com/questions/489981/get-current-project-window-folder.html

    http://answers.unity3d.com/questions/47029/get-folder-from-selection-array.html

    The following only works if I have an item selected in the right side of the window. It's by no means a dealbreaker, but it's a nice-to-have.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEditor;
    6.  
    7. public class BuildAssetFolderStructure : MonoBehaviour
    8. {
    9.  
    10.     [MenuItem("Assets/Build Asset Folder")]
    11.     static void CreateFolder()
    12.     {
    13.         Debug.Log(Selection.activeObject);
    14.     }
    15.  
    16. }
    17.  
     
    frank28 likes this.
  2. frank28

    frank28

    Joined:
    Dec 30, 2013
    Posts:
    13
    Second for this feature.

    It's strange that clicking(Right/Left) on left side of Project panel doesn't change Selection.activeObject.
     
  3. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    795
    Did anyone ever figure this out?
     
  4. noahternullo

    noahternullo

    Joined:
    May 26, 2017
    Posts:
    9
    I still have this question today lmao
     
  5. Thomas-Bousquet

    Thomas-Bousquet

    Joined:
    Dec 19, 2016
    Posts:
    41
    I am necroing this because I found a way to do this using Reflection. This is working in 2021.3.14f1.

    Code (CSharp):
    1. using System.Reflection;
    2. Type projectWindowUtil = typeof(ProjectWindowUtil);
    3. MethodInfo method = projectWindowUtil.GetMethod("GetActiveFolderPath", BindingFlags.NonPublic | BindingFlags.Static);
    4. if (method != null)
    5. {
    6.     string leftPaneFolderPath = (string)method.Invoke(null, null);
    7.     Debug.Log(leftPaneFolderPath);
    8. }
    I figured this out after browsing the UnityCsReference github repo.
    Too bad this function is internal and not public; this could gain from being changed.
     
  6. baerbel

    baerbel

    Joined:
    Feb 24, 2015
    Posts:
    5
    Code (CSharp):
    1. Object[] objs = Selection.GetFiltered<Object>(SelectionMode.Assets);
    This also returns objects that are selected in the left column without having to use reflection.
    Additionally, if several objects were selected you know.