Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all assets of a type?

Tags:

c#

unity3d

In my Unity project a have several instanced of the class ArmourType (these are assets, scriptable objects). I'm trying to show these in a dropdown list in the inspector, and this works. However, I use

List<ArmourType> armourTypes = Resources.FindObjectsOfTypeAll<ArmourType>();

to find all these instances. This only finds the objects that are loaded into memory, so occasionally it only finds some of the required assets. This is documented, so isn't a bug, but very annoying at times.

So my question is, is there a different way of getting all these assets that does return those that aren't loaded into memory? Or perhaps is there a way to make Unity load the assets when they are looked for?

Note: I'm using Unity5 and c#.

like image 679
The Oddler Avatar asked Apr 08 '15 22:04

The Oddler


People also ask

How do I find assets in Unity?

Editor versions 2019.4 and below: Open the Unity Editor and launch an existing project (or create a new one). Click Window > Asset Store. Log into your Unity account to access the Asset Store, if prompted. Find the asset you want to download by using the search bar inside the Asset Store window at the top of the page.

What is resource folder in Unity?

The Resources class allows you to find and access Objects including assets. In the editor, Resources. FindObjectsOfTypeAll can be used to locate assets and Scene objects. All assets that are in a folder named "Resources" anywhere in the Assets folder can be accessed via the Resources.

What can you use to improve the searchability of your projects assets Unity?

Use the Asset Search Provider to search all Assets in the current Project. You can search using keywords or GUIDs (Instance IDs). You can also search the Asset Database or the file system from the Quick Search window. Default action: Open the Asset, either in Unity or in an external editor.


1 Answers

Is this what you are looking for?

string[] guids = AssetDatabase.FindAssets ("t:ArmourType", null);
foreach (string guid in guids) {
    Debug.Log (AssetDatabase.GUIDToAssetPath(guid));
}

http://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html

AssetDatabase is an Editor script. If you want to do a similar thing in-game place the relevant scripts in a Resources folder (this ensures they will be included in the build even if not linked in the scene) and use:

Resources.LoadAll<ArmourType>("");
like image 166
Huacanacha Avatar answered Sep 22 '22 15:09

Huacanacha