• 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 /
avatar image
2
Question by arisonu123 · Mar 19, 2017 at 08:36 AM · scripting problemeditoreditor-scriptingeditorwindowobject reference

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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · 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
5

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;
 }

}

Comment
Add comment · 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Confused about custom GameObjects,Custom GameObject confusion 0 Answers

Too much space between EditorGUILayout.SelectableLabel 1 Answer

OnGui elements as objects? 2 Answers

how to stop reorderable list from passing Event value to the new element 0 Answers

How to have two inspectors of different types thats objects they are inspecting change 0 Answers


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