- Home /
Instantiate prefab in the editor
Hello. How can I instantiate in the asset post processor prefabs?
I've built a pipeline tool in maya that gives string attributes to locators in maya, and in the asset post processor script (JS) I've managed to read that.
Now I want to instantiate it in the editor, so I can have a complete level.
Via a menu command or automatically on import? Can you post any code?
Answer by morobuse · Oct 20, 2012 at 09:06 PM
So this is what I was talking about... it's not perfect but it does what I wanted it to do... sort of...
class MyPostprocessor extends AssetPostprocessor {
function OnPostprocessGameObjectWithUserProperties (
go : GameObject,
propNames : String[],
values : System.Object[]){
for (var i : int = 0; i < propNames.Length; i++) {
var propName : String = propNames[i];
var value : Object = values[i];
modelImporter = assetImporter as ModelImporter;
// adds script
if (propName == "isPrefab")
{
if (value.GetType() == String) {
var isPrefab : String = value;
if (isPrefab != null)
{
var newInstance : GameObject = PrefabUtility.InstantiatePrefab(Resources.Load(isPrefab));
var meshToDelete = go.GetComponent(MeshFilter);
var meshRendererToDelete = go.GetComponent(MeshRenderer);
// Debug.Log(meshToDelete);
MeshFilter.DestroyImmediate(meshToDelete);
MeshRenderer.DestroyImmediate(meshRendererToDelete);
newInstance.transform.parent = go.transform;
newInstance.transform.localRotation = new Quaternion.Euler(0, 0, 0);
newInstance.transform.localPosition = new Vector3(0,0,0);
newInstance.transform.localScale = new Vector3(1,1,1);
// var newItem : GameObject = GameObject.Instantiate(Resources.Load(isPrefab), go.transform.position, go.transform.rotation);
// newItem.transform.parent = go.transform;
// newItem.transform.localScale = new Vector3(1,1,1);
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
PrefabUtility.InstantiatePrefab not working with PrefabType.ModelPrefabInstance? 0 Answers
Instantiating in editor not saving prefab connection? 2 Answers
Instantiating a prefab in the editor is orphaning the children of the prefabs 0 Answers
Prefab doesn't hold prefab of itself, Instead just copies itself on Instantiate() 1 Answer