0
votes

I have a large room with tends of thousands of nested objects. I tried to select a group of objects that represents a single object out of hundreds. This was originally a sketchup file that got turned into a prefab variant and added to the scene.

 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);
     }
 }
 }

So I am trying to iterate through the selection and turn them each into prefabs. Its pretty ridiculous because there is close to 100,000 objects in total and everything, like nuts, bolts, rods that make up a chair are individual gameobjects.

I am trying to use GPU instancing to reduce framerate but this imported model needs to be converted into prefab somehow. I am trying to extract meaningful objects that repeat (ex. chairs) as individual chair prefabs but the problem is:

  1. I don't know how to first combine/fuse the objects that make up a chair

  2. If I am able to select a chair I need to be able to convert it into a chair prefab.

  3. I have to basically repeat this process for each object that repeats excessively (ex. chairs, desks)

I get this error message from unity: can't save part of a prefab instance as a prefab,

How can I group the individual parts that make up a chair and turn it into a prefab?

2
Unity can neither handle that many objects nor a hierarchy this deep! (a nice article on Unity's own website about this. Thus you will never end up with good performance this way. You'll have to reduce and combine the objects in a dedicated software (usually 3D modeling software) and import those objects to unity. - Thomas
@Thomas I would accept that as an answer - jamus

2 Answers

0
votes

Unity can neither handle that many objects nor a hierarchy this deep! (a nice article on Unity's own website about this). Thus you will never end up with good performance this way. You'll have to reduce and combine the objects in a dedicated software (usually 3D modeling software) and import those objects to unity.

0
votes

The same requirement as your. Unity hasn't provided such a method to save a nested prefab modification, but can be achieved by a trick.

The solutions is:

  1. Instantiate another nested prefab by PrefabUtility.InstantiatePrefab
  2. Modify that new GameObject of the prefab
  3. Save the modification by PrefabUtility.SaveAsPrefabAssetAndConnect

And because the gameobject hierarchy is a reference, the saved modification will also affect the nested prefab.

Here is the code:

var prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(goInScene);
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
var anotherGo = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

// Do the same modification of the nestedGo to anotherGo

PrefabUtility.SaveAsPrefabAssetAndConnect(anotherGo, prefabPath, InteractionMode.AutomatedAction); // This save will affect all prefab in Scene!
DestroyImmediate(anotherGo);