Nice day everyone,
Recently I try to do some practice with unity editor script, I have not understand clearly about LoadAssetAtPath, LoadAllAssetAtPath, LoadMainAssetAtPath.
Say I have a prefabs at path ~/Assets/Resources/Prefabs/something.prefab
As I understand if I use:
- LoadAssetAtPath is load only one asset at that path. => I get the something.prefab object
- LoadAllAssetAtPath is load all the asset at that path => I Get the something.prefab and the children object inside this prefab
- LoadMainAssetAtPath is same with LoadAssetAtPath. => I get the something.prefab object (I don’t know if this right or wrong, because as I test I get the same result with the LoadAssetAtPath) .
When I use LoadAllAssetAtPath, It’s “seem like” some object has been duplicated. (I think so)
for example, this is my test prefab:
something.prefab
|_Tab1
|_child 1
|_child 2
I will got this result:

I hope someone here can teach me the different and the knowledge of these AssetDatabase feature.
Thanks in advanced.
Well, you first should understand what Unity considers an “asset”. In general every type that is derived from UnityEngine.Object could be called an asset as those are the only types Unity can serialize. Using LoadAllAssetAtPath on .prefab files doesn’t make much sense as it would read out every single object that might be inside the prefab. Keep in mind that “GameObject” as well as “Transform” are serializable classes and they both share the same “name”.
This method makes more sense for “.asset” files which contain multiple assets in a single file.An asset file could contain a serialized gameobject, a Material, maybe several Textures or any other asset like ScriptableObjects.
To understand this better i suggest you switch your “asset serialization mode” to “Force Text” (Edit–>Project Settings–>Editor–>Asset Serialization). This way you can open .prefab .scene or .asset files in a text editor. The text serialization is done in YAML.
To explain the difference between the three methods:
-
LoadAllAssetAtPath as mentioned simply loads all serialized objects it finds inside the specified file.
-
LoadMainAssetAtPath simply returns the “main” asset. This can be a bit confusing. Unity’s built-in types have a “higher priority” than for example your ScriptableObject classes. That means if you save a ScriptableObject as .asset file you will see that instance in the inspector. However when you add a Material to the same file the Material will become the main asset. If no special priority applies this method will simply return the first asset that appears in the file.
-
LoadAssetAtPath loads a single asset from the file. It basically does almost the same as “LoadMainAssetAtPath” however you can specify a “filter type” so it only returns assets which are either of that type or derived from that type. So specifying “Texture” as type it will return any kind of texture asset (Texture2D, RenderTexture, Texture3D). Specifying “Component” will return the first asset that is a Component (might be the Transform component).