- Home /
How to get the currently selected object in the project window only
Hi!How do I just get the selected object in project tab? I want to have an editor window open when I select an asset in the project view only. I have an instance of another class attached to a gameobject that stores an instance of this class and currently whenever I click to select one and then select one in that little menu that pops up it opens my editor window. It also opens my editor window if my class attached to a gameobject already has an instance of the class that opens the editor window stored in it and I click on the reference in the inspector. I only want the editor window to open if I select one in the project tab. Is this possible? Currently I am trying
private void OnSelectionChange(){
if(Selection.GetFiltered<MyObject>(Selection.DeepAssets)!=null){
Test=Selection.GetFiltered<MyObject>(SelectionMode.DeepAssets)[0] as MyObject;
}
}
but it doesn't work.
Answer by arisonu123 · Mar 26, 2017 at 05:52 PM
Well I figured out something that works for me. I can check if the the a active object is of my type and then I can check if the assetGUIDS length is greater than 0. I learned that only assets and folders in the project window seem to have GUIDs. For anyone finding this question I got it working like this:
private MyObject Test;
private void OnSelectionChange(){
if(Selection.activeObjest is MyObject)
{
if(Selection.assetGUIDS.length>0){
Test=Selection.activeObject as MyObject;
}
}
}
This results in the first item being selected that is of my type and is in the project window being stored in my variable.
Answer by sanketprabhu · Jun 12, 2019 at 03:16 PM
public static class UnityUtil { public static string GetSelectedPathOrFallback() { string path = "Assets";
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
path = AssetDatabase.GetAssetPath(obj);
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
path = Path.GetFileName(path);
break;
}
}
return path;
}
}
Your answer
Follow this Question
Related Questions
Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer
Set MinWidth for EditorWindow 1 Answer
mouseposition and clicks in editor sceneview 0 Answers
How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer
Viewpport for Custom Editor 1 Answer