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
5
Question by frarees · Jul 04, 2013 at 09:01 AM · editorassetprojectwindowsearch

Getting all assets of the specified type

I'm searching for something like:

 Object[] objs = AssetDatabase.Find (typeof (ScriptableObject));

Does this exists? If not, how does ProjectWindow (or a SearchableEditorWindow) get those? How does the search manage to filter by type? (`t:Material`)

Comment
Add comment · Show 4
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 frarees · Jul 04, 2013 at 11:14 AM 0
Share

Anyone knows how to use HierarchyPropertys? They seem to be very unstable, but ProjectWindow relies on them it seems.

avatar image Benproductions1 · Jul 05, 2013 at 08:37 AM 0
Share

Why not just get all and then search yourself?

avatar image frarees · Jul 05, 2013 at 08:56 AM 0
Share

@BiG couldn't remember that method. Thank you!

avatar image BiG · Jul 05, 2013 at 08:58 AM 0
Share

I have converted my comment into an answer. Glad that you solved!

5 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by BiG · Jul 05, 2013 at 08:48 AM

Does Resources.FindObjectsOfTypeAll accomplish what you wanna do?

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 yoyo · Jan 14, 2014 at 11:30 PM 5
Share

This will only find assets that are loaded (into memory in the Editor), not all assets in your Project hierarchy.

avatar image
28

Answer by glitchers · Jul 15, 2016 at 09:56 AM

Unity has made Object. FindObjectsOfTypeIncludingAssets obsolete and recommends you use Resources. FindObjectsOfTypeAll but this will not find all assets.

I have written a small piece of code that you can use in Editor only that takes a generic type and will return all objects in your project regardless of if they have been loaded yet.

 public static List<T> FindAssetsByType<T>() where T : UnityEngine.Object
 {
     List<T> assets = new List<T>();

     string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(T)));

     for( int i = 0; i < guids.Length; i++ )
     {
         string assetPath = AssetDatabase.GUIDToAssetPath( guids[i] );
         T asset = AssetDatabase.LoadAssetAtPath<T>( assetPath );

         if( asset != null )
         {
             assets.Add(asset);
         }
     }
     return assets;
 }

Comment
Add comment · Show 2 · 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 ilya_ca · Sep 22, 2016 at 07:56 PM 3
Share

Thanks! The only thing is that at line 5 typeof(T) should be typeof (T).ToString().Replace("UnityEngine.", ""); For example if we're looking for a material we should say FindAssets("t:$$anonymous$$aterial"); however typeof(T) returns UnityEngine.$$anonymous$$aterial, therefore we have to remove the UnityEngine part first.

avatar image Ziplock9000 · Apr 25, 2017 at 10:56 PM 0
Share

This wont work with scenes due to a boxing conversion error :/ 5.6.0f3

avatar image
2

Answer by icepick912 · Nov 27, 2013 at 06:37 PM

Here is a c# one for getting all assets in project of type with a certain file extension

             /// <summary>
             /// Used to get assets of a certain type and file extension from entire project
             /// </summary>
             /// <param name="type">The type to retrieve. eg typeof(GameObject).</param>
             /// <param name="fileExtension">The file extention the type uses eg ".prefab".</param>
             /// <returns>An Object array of assets.</returns>
             public static Object[] GetAssetsOfType(System.Type type, string fileExtension)
             {
                 List<Object> tempObjects = new List<Object>();
                 DirectoryInfo directory = new DirectoryInfo(Application.dataPath);
                 FileInfo[] goFileInfo = directory.GetFiles("*" + fileExtension, SearchOption.AllDirectories);
     
                 int i = 0; int goFileInfoLength = goFileInfo.Length;
                 FileInfo tempGoFileInfo; string tempFilePath;
                 Object tempGO;
                 for (; i < goFileInfoLength; i++)
                 {
                     tempGoFileInfo = goFileInfo[i];
                     if (tempGoFileInfo == null)
                         continue;
     
                     tempFilePath = tempGoFileInfo.FullName;
                     tempFilePath = tempFilePath.Replace(@"\", "/").Replace(Application.dataPath, "Assets");
     
                     Debug.Log(tempFilePath + "\n" + Application.dataPath);
     
                     tempGO = AssetDatabase.LoadAssetAtPath(tempFilePath, typeof(Object)) as Object;
                     if (tempGO == null)
                     {
                         Debug.LogWarning("Skipping Null");
                         continue;
                     }
                     else if (tempGO.GetType() != type)
                     {
                         Debug.LogWarning("Skipping " + tempGO.GetType().ToString());
                         continue;
                     }
     
                     tempObjects.Add(tempGO);
                 }
     
                 return tempObjects.ToArray();
             }
Comment
Add comment · Show 2 · 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 victorafael · Jul 11, 2014 at 12:46 PM 0
Share

I have changed your code a little to support declarations like that: $$anonymous$$yScript[] script = GetAssetsOfType(".prefab");

 /// <summary>
 /// Used to get assets of a certain type and file extension from entire project
 /// </summary>
 /// <param name="type">The type to retrieve. eg typeof(GameObject).</param>
 /// <param name="fileExtension">The file extention the type uses eg ".prefab".</param>
 /// <returns>An Object array of assets.</returns>
 public static T[] GetAssetsOfType<T>(string fileExtension) where T : UnityEngine.Object
 {
     List<T> tempObjects = new List<T>();
     DirectoryInfo directory = new DirectoryInfo(Application.dataPath);
     FileInfo[] goFileInfo = directory.GetFiles("*" + fileExtension, SearchOption.AllDirectories);
     
     int i = 0; int goFileInfoLength = goFileInfo.Length;
     FileInfo tempGoFileInfo; string tempFilePath;
     T tempGO;
     for (; i < goFileInfoLength; i++)
     {
         tempGoFileInfo = goFileInfo[i];
         if (tempGoFileInfo == null)
             continue;
         
         tempFilePath = tempGoFileInfo.FullName;
         tempFilePath = tempFilePath.Replace(@"\", "/").Replace(Application.dataPath, "Assets");
         tempGO = AssetDatabase.LoadAssetAtPath(tempFilePath, typeof(T)) as T;
         if (tempGO == null)
         {
             continue;
         }
         else if (!(tempGO is T))
         {
             continue;
         }
         
         tempObjects.Add(tempGO);
     }
     
     return tempObjects.ToArray();
 }
avatar image steffen-itterheim victorafael · Jan 17, 2018 at 09:34 AM 1
Share

I prefer clean code, so here is my version. ;) Supports checking for both extension or extension plus type:

 public static T[] FindAssetsWithExtension<T>(string fileExtension) where T : UnityEngine.Object
 {
     var paths = FindAssetPathsWithExtension(fileExtension);
     if (paths == null || paths.Length == 0)
         return null;

     List<T> assetsOfType = new List<T>();
     for (int i = 0; i < paths.Length; i++)
     {
         var asset = AssetDatabase.LoadAssetAtPath(paths[i], typeof(T)) as T;
         if (asset == null || (asset is T) == false)
             continue;

         assetsOfType.Add(asset);
     }

     return assetsOfType.ToArray();
 }


 public static string[] FindAssetPathsWithExtension(string fileExtension)
 {
     if (string.IsNullOrEmpty(fileExtension))
         return null;

     if (fileExtension[0] == '.')
         fileExtension = fileExtension.Substring(1);

     DirectoryInfo directoryInfo = new DirectoryInfo(Application.dataPath);
     FileInfo[] fileInfos        = directoryInfo.GetFiles("*." + fileExtension, SearchOption.AllDirectories);

     List<string> assetPaths     = new List<string>();
     foreach (var file in fileInfos)
     {
         var assetPath = file.FullName.Replace(@"\", "/").Replace(Application.dataPath, "Assets");
         assetPaths.Add(assetPath);
     }

     return assetPaths.ToArray();
 }
avatar image
1

Answer by FriedrichWessel_Inno · Oct 18, 2018 at 07:49 AM

 AssetDatabase.Find("t: ScriptableObject")

You can test the filters in your searchbar before Documentation: https://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html

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 zauberzaubar · Apr 09, 2021 at 02:32 PM

glitchers answer is correct but here's an iterator version of it:

 public static IEnumerable<T> FindAssetsByType<T>() where T : Object {
         var guids = AssetDatabase.FindAssets($"t:{typeof(T)}");
         foreach (var t in guids) {
             var assetPath = AssetDatabase.GUIDToAssetPath(t);
             var asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
             if (asset != null) {
                 yield return asset;
             }
         }
     }


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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to highlight or select an asset in project window from editor script? 2 Answers

Multiple object Selection in project view: Unity shows wrong type 0 Answers

Was the feature of using the Asterisk ('*') as a wildcard Removed? 1 Answer

Getting assets / sub folders inside a folder 1 Answer

Custom Project Editor Window 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