- Home /
Is it possible to get a prefab object from its asset path?
For instance, I want to be able to instantiate a gameObject from a prefab stored in "Assets/Prefab/Items/Key_yellow.prefab". Thank you.
Answer by Kryptos · Sep 05, 2012 at 09:32 AM
Loading Resources at Runtime is what you are looking for.
Answer by schlenger · Jan 03, 2020 at 08:02 AM
For everybody new: The answers above weren't working for me with the Unity versions from 2019 and on.
Yes, you might want to check the Docs at https://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html
Here it says
you simply create a new folder inside the Project View , and name the folder “Resources”. You can have multiple Resource Folders organized differently in your Project. Whenever you want to load an asset from one of these folders, you call Resources.Load()
- So, you have to move your files e.g. to Assets/Resources/
- let us assume your prefab is called Cube.prefab
it is now at Assets/Resources/Cube.prefab
. - To load it programmatically, you now can call Resources.Load("Cube");
(Note: You neither need the Resources
prefix, nor the .prefab
ending) - Instantiation is the same as above: GameObject.Instantiate((UnityEngine.Object) Resources.Load("Cube"), Vector3.zero, Quaternion.identity);
Long story short:
private UnityEngine.Object LoadPrefabFromFile(string filename)
{
Debug.Log("Trying to load LevelPrefab from file ("+filename+ ")...");
var loadedObject = Resources.Load("Levels/" + filename);
if (loadedObject == null)
{
throw new FileNotFoundException("...no file found - please check the configuration");
}
return loadedObject;
}
// Called via:
var loadedPrefabResource = LoadPrefabFromFile("Cube");
Instantiate(loadedPrefabResource, Vector3.zero, Quaternion.identity);
@tmdchi you may want to have a look
thank you sir. I was making the mistake of still including the full path when I was trying to do this.
Answer by MadDave · Sep 05, 2012 at 09:09 AM
UnityEngine.Object pPrefab = Resources.Load("Assets/Prefab/Items/Key_yellow"); // note: not .prefab!
GameObject pNewObject = (GameObject)GameObject.Instantiate(pPrefab, Vector3.zero, Quaternion.identity);
Won't work. Because the prefab is not inside a Resources folder.
Answer by Nit_Ram · Feb 08, 2020 at 05:13 PM
You don't have to use the ressource folder as long as you want to use this only in the editor, not at runtime.
Here is a little script that finds you all prefabs in a specific folder:
private void GetAllPrefabs()
{
string[] foldersToSearch = {"Assets"};
List<GameObject> allPrefabs = GetAssets<GameObject>(foldersToSearch, "t:prefab");
}
public static List<T> GetAssets<T>(string[] _foldersToSearch, string _filter) where T : UnityEngine.Object
{
string[] guids = AssetDatabase.FindAssets(_filter, _foldersToSearch);
List<T> a = new List<T>();
for (int i = 0; i < guids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
a.Add(AssetDatabase.LoadAssetAtPath<T>(path));
}
return a;
}
Answer by FlightOfOne · May 11, 2018 at 07:22 PM
This is what worked for me (unity 2017.3)
UnityEngine.Object prefab = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/PlayerController.prefab", typeof(PlayerController));
PlayerController player = Instantiate(prefab) as PlayerController;
No longer works. Use resource path ins$$anonymous$$d. ex.
UnityEngine.Object pPrefab = Resources.Load("Prefabs/PrefabName");
make sure to place the Resource folder inside your Assets folder, then place any needed files in resource folder.
Strange.. I was just using this the other day. Still working for me on 2019.1
This works perfectly but we aware: works only in the unity editor, so you cannot use it in the build. This will generate an error at build time.
Still working in 2020.3 and perfectly made the job, thank you for smooth solution