• 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 Jaroslav-Stehlik · Mar 04, 2012 at 03:55 AM · assetscustomfindfilterobjectfield

Find All Assets by Type

Hi, I need to find all assets by their type no matter if they are active or not.

the reason is, because I made my custom Object Field Script for custom finding criteria. this AssetsFinder can filter scene or asset objects and filter them by components which they have.

Resources.FindObjectsOfTypeAll should do the trick. But it doesn't, because it works only on previously selected assets. If I restart the Editor, it would not found the assets before I click on them. Why ? This is an huge problem.

The purpose is that our team should see only relevant prefabs or game objects which have only relevant components. How this can be done ?

Jaroslav Stehlik - Team Leader At Silicon Jelly. http://www.siliconjelly.com

Comment
Add comment · Show 1
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 frogsbo · Sep 08, 2014 at 04:25 PM 0
Share

liked this question. the answers were technically abit difficult for this very moment, so here is a workaround for similar probs, get the asset path name, get the last 3 characters, check them agains reference ie

 sting s1 = "file.tga";
 string s2 = s1.Substring(s1.Length - 3);
 see if s2 = tga

2 Replies

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

Answer by Jaroslav-Stehlik · Mar 12, 2012 at 07:55 AM

I figured it out with my selft implementation of searching trhu assets. Works perfect.

protected static function getAllEditorAssets():GameObject[] { var tempObjects:Array = new Array(); var directory:DirectoryInfo = new DirectoryInfo(Application.dataPath); var goFileInfo:FileInfo[] = directory.GetFiles("*.prefab", SearchOption.AllDirectories); var i:uint = 0; var goFileInfoLength:uint = goFileInfo.length; var tempGoFileInfo:FileInfo; var tempFilePath:String; var assetIndex:int; var tempGO:GameObject; for(i = 0; i < goFileInfoLength; i++) { tempGoFileInfo = goFileInfo[i] as FileInfo; if(tempGoFileInfo == null) continue; tempFilePath = tempGoFileInfo.FullName;

     assetIndex = tempFilePath.IndexOf("Assets/");
     //assetIndex = tempFilePath.IndexOf("Assets\\");
     if (assetIndex &lt; 0) assetIndex = 0;         
     tempFilePath = tempFilePath.Substring(assetIndex, tempFilePath.length - assetIndex);
     //tempFilePath = tempFilePath.Replace('\\', '/');
     tempGO = AssetDatabase.LoadAssetAtPath(tempFilePath, GameObject) as GameObject;
     if(tempGO == null) continue;
     tempObjects.push(tempGO);
 }

 return tempObjects.ToBuiltin(GameObject) as GameObject[];

}

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
3

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

Converted to c#

      /// <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 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 steffen-itterheim · Jan 17, 2018 at 09:35 AM 0
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();
 }

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom texture importer for custom folder 1 Answer

How to Find Assets of the Same Type in Editor Script 1 Answer

Custom Assets, Inventory - best practice 0 Answers

Make Array Of All Objects In A Folder 1 Answer

custom editor EditorGUILayout.ObjectField only shows the assets folder 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges