Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 nuverian · Mar 13, 2013 at 11:03 PM · guieditorprefabinspector

How to know if something is selected in Hierarchy or in project.

To be more specific.

I have made a prefab. Now that prefab naturaly resides in the "Project", as well as it can be in a level's "Hierarchy". How can I know if what is selected and as such displays in the "Inspector", is a prefab selected in project or something selected in the scene's hierarchy,(and then (for example) do stuff in my OnInspectorGUI() to display different GUI depending on the situation).

Thanks

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

5 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by Nerraw · Mar 20, 2015 at 05:53 AM

AssetDatabase.Contains( Object ) -> true if it's in the project, false if it's in the scene.

Comment
Add comment · 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 MikeHergaarden · Jun 29, 2017 at 01:09 PM 0
Share

Simplest & best solution (GetPrefabType will still return Prefab type for prefabs in the scene)

avatar image
5

Answer by save · Nov 20, 2013 at 03:56 PM

I recently ran in to the very same issue and found this question. I although figure you have solved it by now as the question was written a while ago - but for anyone stumbling into this question here's a possible solution. What did it for me was to use PrefabUtility.

 if (PrefabUtility.GetPrefabType(target)==PrefabType.None)
     // In Hieararchy
 else
     // In Project View


Comment
Add comment · 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 MaceB · May 15, 2020 at 04:16 PM 0
Share

This works best in my opinion because all the other solutions also count Prefab $$anonymous$$ode as in the Hierarchy.

This code is now obsolete however so for anyone new:

 private void OnEnable()
     {
         if (PrefabUtility.GetPrefabInstanceStatus(script.gameObject) == PrefabInstanceStatus.NotAPrefab)
             Debug.Log("Selected in Project or Prefab $$anonymous$$ode");
         else
             Debug.Log("Selected in Hierarchy");
     }

Throw this into your Prefab's editor script and it will debug which mode you selected your Prefab in.

avatar image
4

Answer by devpros · Mar 26, 2020 at 03:38 PM

For whoever stumbles upon this and looking specifically for gameobjects:

test for

gameObject.scene.IsValid()

if will return false when in project window

 if(!gameObject.scene.IsValid())
 {
      //true if the go is in a scene
      //false if is in project window
 }
Comment
Add comment · 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 MostHated · Jul 31, 2020 at 09:26 PM 0
Share

This was exactly what I was looking for. Thanks.

avatar image
0

Answer by GingerLoaf · Aug 30, 2014 at 01:32 AM

This is not the absolute perfect solution, but I found it to be a little more settling than the others in this post. I noticed that objects have hideflags on them that govern where they are visible in the editor. I found out that when you use Resources.FindObjectsOfTypeAll<>() to locate components (Resources.FindObjectsOfTypeAll includes disabled components as well as ones on disabled GameObjects), any objects in the project window will come with the HideInInspector hideflag set on it and the ones in the hierarchy come with No hideflags. I don't know enough about the internal operations of Unity to determine the accuracy of this fix, but it seems to be working flawlessly for me.

Here is an example:

 public T[] FindObjectsOfTypeIncludingDisabled<T>() where T : Component
 {
     T[] components = Resources.FindObjectsOfTypeAll<T>();
     if (components.Length == 0)
     {
         return new T[0];
     }
 
     List<T> allComponents = new List<T>();
     for (int i = 0; i < components.Length; i++)
     {
         T currentComponent = components[i];
         if (currentComponent.hideFlags != HideFlags.None)
         {
             continue;
         }
 
         allComponents.Add(currentComponent);
     }
 
     return allComponents.ToArray();
 }
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
0

Answer by minimink · Mar 21, 2018 at 06:54 AM

I'm not sure if this is the perfect way, but if Selection.transforms.Length == 0, it is selected in Project.

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

OnInspectorGUI - Using the default Object Selection popup. 1 Answer

Create an Editor Window like Mecanim 1 Answer

Unity Editor Inspector creating gameObjects in world 2 Answers

Display object in the inspector via script 1 Answer

How do you get the default inspector to draw for Prefabs ? 2 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