- Home /
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
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
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 < 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[];
}
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();
}
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
Follow this Question
Related Questions
Implementing custom texture filtering 1 Answer
Custom levels created by users 1 Answer
How can I get a list of specific game objects in the object picker opened from an ObjectField()? 0 Answers
How to Find Assets of the Same Type in Editor Script 1 Answer
custom editor EditorGUILayout.ObjectField only shows the assets folder 1 Answer