- Home /
I want to create individual prefabs based from Hierarchy selection.
Hi, I want to create multiple individual prefabs based from my Hierarchy game object selection. I have tried to drag and drop but I can only do it one at a time and not all at once. Since I have thousands of objects to do this I would prefer just to be able to do them all at once. I tried to find a "make prefab" button on the menu but didn't saw that option.
My task: I need to make prefabs from 1500 different FBX assets each with 12 materials. So around 18.000 prefabs would be needed in total. So what I want is to put the FBX assets on the hierarchy, put the needed material, rename it to the material color and then make a prefab batch.
Here's a screenshot of what I want to do to save time.
Basically what I need is to batch prefabs from a selection of FBX meshes with applied materials.
Thanks
why would you want thousands of prefabs.they must have something in common (white, blue) that a few should suffice
I need to make prefabs from 1500 different FBX assets each with 12 materials. So around 18.000 prefabs would be needed in total.
So what I want is to put the FBX assets on the hierarchy, put the needed material, rename it to the material color and then make a prefab batch. $$anonymous$$aybe I'm tackling this from a wrong angle?
then you need to look into editor scripting. there are ways to write prefabs programmatically, know what's selected in hierarchy, all the good stuff
I think I was tackling the problem.
Put the FBX asset meshes on the Hierarchy.
Assign a material for all the meshes (select all and drag and drop the material under the component in inspector window).
Use a batch rename all FBX game objects to apply a suffix for with the material name. For example: Game_Object_01_Red
Drag and drop each Game Object to a Prefab folder.
Copy those prefabs to another prefab folder. For example from Red to Yellow.
Use batch rename tool again to rename all the new copies to Yellow: Game_Object_01_Yellow
Select all the new Yellow prefab copies and apply a new colored material in the Inspector under the $$anonymous$$esh Renderer part.
Thanks to @hexagonius for making me look at the problem with a different angle.
While this solves the problem of making all 11 prefabs copies with different materials and drag and drop all over again. The original material still has to be done manually from the hierarchy using drag and drop but that's not so time-consu$$anonymous$$g. So if anyone knows a solution for that part would be great.
Answer by itos · May 14, 2017 at 12:34 AM
Doing a custom script was the better choice. This will create a new menu item with a button called Create Prefab from Selection. Just make a selection from the hierarchy, use the button and you will get prefabs made from your game object selections. Make sure to have a folder called Prefabs under the Assets folder:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CreatePrefab : MonoBehaviour
{
[MenuItem("Extras/Create Prefab From Selection")]
static void DoCreatePrefab()
{
Transform[] transforms = Selection.transforms;
foreach (Transform t in transforms)
{
GameObject prefab = PrefabUtility.CreatePrefab("Assets/Prefabs/" + t.gameObject.name + ".prefab", t.gameObject, ReplacePrefabOptions.ReplaceNameBased);
}
}
}
Thanks, works great!
p.s. CreatePrefab() has been deprecated, just replace with this method instead:
GameObject prefab = PrefabUtility.SaveAsPrefabAsset(t.gameObject, "Assets/Prefabs/" + t.gameObject.name + ".prefab");
Your answer
