- Home /
Find prefab path of a gameObject?
Hi!
Does anyone know how I could find the prefab path of a gameobject?
Something like (go is an ordinary gameObject):
string path = findPrefabPath(go);
GameObject newGO = GameObject.Instantiate(Resources.Load(path));
I know this code sample here does not make so much sense, but I really need this function as it would be used in the sample above.
I found this: http://unity3d.com/support/documentation/ScriptReference/PrefabType.html but it is not all I need, this would only give the information if an object has some kind of prefab connected to it. I need the path it is connected to (or even disconnected from).
Is it possible?
Thanks in advance!
Answer by Keep Silence · Oct 16, 2013 at 07:05 PM
try this
Object parentObject = EditorUtility.GetPrefabParent(obj); string path = AssetDatabase.GetAssetPath(parentObject); Debug.Log("prefab path:" + path);
Yes,that's what I figured out myself.The GetPrefabParent method. But why doesn't GetPrefabObject work?
For everyone who needs this, EditorUtility.GetPrefabParent(obj) is obsolete use PrefabUtility.GetPrefabParent(obj) ins$$anonymous$$d.
This is also obsolete now. Try use PrefabUtility.GetCorrespondingObjectFromSource()
Answer by Cameron 3 · Feb 05, 2012 at 06:18 PM
Softrare,
You would use the AssetDatabase class. Check the scripting ref, there's a lot of good stuff in there. IE:
string myPath = AssetDatabase.GetAssetPath( myGameObjectOrPrefab );
Thank you! Seems logical, but strangely this function returns "" (empty string) oO http://unity3d.com/support/documentation/ScriptReference/PrefabType.html returns DisconnectedPrefabInstance. So the path should really be possible to grasp right?
Try using EditorUtility.GetPrefabParent() and then ask the AssetDatabase about the root prefab. Not sure this will work with DIsconnectedPrefabInstance though.
Answer by Sky-Foxy · Nov 02, 2016 at 03:46 PM
If you need find object in Prefabs, Material, Scene, by GUID https://gist.github.com/AndreySkyFoxSidorov/977e5b0a91b3bc3e2acf53917e32f410
Click Right-click select "Find GUID in Prefabs Material Scene" the object to find in Prefabs, Material, Scene, by GUID.
Answer by Berenger · Feb 05, 2012 at 06:02 PM
Unless you need the exact prefab setups, you can always instantiate an object from a GameObject (prefabs are just that) from the scene :
GameObject newGO = GameObject.Instantiate(go);
Yes, I know that. But indeed I need the exact path. Thanks anyway!
Answer by IgorAherne · Jun 30, 2017 at 05:33 PM
Better use this
PrefabUtility.FindPrefabRoot(_targ.gameObject);
string path = AssetDatabase.GetAssetPath(prefabParent);