- Home /
Any way to find original prefab name by script?
If I put a prefab into a scene, and rename it, is there an API I can call on that GameObject to tell me the 'original prefab' name?
You know, like whatever happens when you click the 'Select' button next to the word 'Prefab' in the Inspector, it finds and highlights the original piece (even if it's nested).
Answer by DaveA · Aug 06, 2010 at 07:26 PM
From andeeee on the Unity Forum:
"You can do this from an editor script using EditorUtility.GetPrefabParent"
This is what I needed. The manual reads like it would give me the name of the parent in the Hierarchy (game object in the scene), rather than the Asset itself, but it's what I needed. Thanks andeeee
Answer by spinaljack · Aug 06, 2010 at 12:38 AM
you can add a variable to it that stores the original prefab and then you'd just make a function that returns that variable. e.g.
var original : GameObject;
function GetPrefab(){ return original; }
Drag the original prefab onto the 'original' slot in the inspector.
Thanks, but this needs to be non-interactive. I'm exporting the scene graph, and doing that for my hundreds of objects would be too much trouble. Also needs to work for 'nested' prefabs.
Answer by DaveA · Aug 06, 2010 at 01:52 AM
I think I have something that should work. The idea is to set the name of the mesh when it's imported, such that you can reconstruct the object itself when you need it. Put this script in Editor folder, Reimport the prefabs in question, that will set their mesh's names accordingly. Then you can access those mesh names when you go to export them. (Please, Unity team, make this easier please!)
class MyModelPostprocessor : AssetPostprocessor { void OnPostprocessModel (GameObject g) { Apply (g.name, g.transform); }
// rename each imported mesh to indicate it's transform's name
void Apply (string parName, Transform transform)
{
MeshFilter mf = transform.GetComponent(typeof(MeshFilter)) as MeshFilter;
if (mf)
{
Mesh m = mf.sharedMesh;
if (m)
{
Debug.Log ("renaming to "+parName+"_Mesh");
m.name = parName + "_Mesh";
}
}
// Recurse
foreach (Transform child in transform)
Apply(parName+"_"+child.name, child);
}
}
Your answer
Follow this Question
Related Questions
Enemy health. Need to take damage from two different bullet prefabs 1 Answer
is there a way to make a script prefab? 1 Answer
Tagging object while instating not working. 3 Answers
Prefabs & scripts 1 Answer
How To Disable A Prefab? 2 Answers