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 Molix · Nov 26, 2010 at 04:43 AM · editorprefabsearch

How can I select all prefabs that contain a certain component?

I had to change the data format of one of the components we use frequently. I thought I could just quickly write a script to search through the entire project, find any prefabs containing the component, and then run some code to convert the data. But I'm not actually sure where to start -- how do I iterate through prefabs in editor code? I'm familiar with finding active game objects (i.e. in the scene/hierarchy) but not those in the project. I must be tired, but I just can't find out where to start (I looked in EditorApplication, EditorUtility, searched here, etc).

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
0
Best Answer

Answer by Jean-Fabre · Nov 26, 2010 at 08:47 AM

Hi,

AssetDatabase.LoadAllAssetsAtPath(<string>)

I think this is what you need given your explanation. You will end up with a list of all objects in that path. Look at the rest of AssetDatabase functions, there is a lot related to that.

Then you could use

EditorUtility.GetPrefabType(<object>) 

As you iterate through the return of AssetDatabase.LoadAllAssetsAtPath or whatever in the scene, if GetPrefabType() returns PrefabType.Prefab as a result, then you have a prefab.

Hope it helps,

Bye,

Jean

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 Molix · Nov 26, 2010 at 03:13 PM 0
Share

Thanks; would I be correct in assu$$anonymous$$g that LoadAllAssetsAtPath does not check child folders? So I'll need to do that too. Ok, thanks a lot!

avatar image
2

Answer by vikingfabian-com · Jun 24, 2014 at 11:29 AM

Could not find a satisfying solution, so I made my own method:

 /// <summary>
 /// Find all prefabs containing a specific component (T)
 /// </summary>
 /// <typeparam name="T">The type of component</typeparam>
 public static List<GameObject> LoadPrefabsContaining<T>(string path) where T : UnityEngine.Component
 {
     List<GameObject> result = new List<GameObject>();
 
     var allFiles = Resources.LoadAll<UnityEngine.Object>(path);
     foreach (var obj in allFiles)
     {
         if (obj is GameObject)
         {
             GameObject go = obj as GameObject;
             if (go.GetComponent<T>() != null)
             {
                 result.Add(go);
             }
         }
     }
     return result;
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

Extending "GameObject/Create Other" with own prefab 1 Answer

How does Unity retain UnityEngine.Object references? 2 Answers

The Script Equivilent of dragging a hierarchy of meshes over a preexisting prefab? 0 Answers

Why do prefabbed meshes go missing whenever I pull an update from Unity Collab? 0 Answers

How Mark Prefab Dirty? 1 Answer


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