简体   繁体   English

unity3d:如何将预制实例的一部分保存为预制?

[英]unity3d: How to save part of a prefab instance as a prefab?

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.我试图 select 一组对象,这些对象代表数百个中的单个 object。 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.这很荒谬,因为总共有近 100,000 个对象,而构成椅子的所有东西,如螺母、螺栓、杆都是单独的游戏对象。

I am trying to use GPU instancing to reduce framerate but this imported model needs to be converted into prefab somehow.我正在尝试使用 GPU 实例来降低帧速率,但是这个导入的 model 需要以某种方式转换为预制件。 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.如果我能够 select 椅子,我需要能够将其转换为椅子预制件。

  3. I have to basically repeat this process for each object that repeats excessively (ex. chairs, desks)我必须对每个过度重复的 object 基本上重复这个过程(例如椅子、桌子)

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?如何将构成椅子的各个部分分组并将其变成预制件?

Unity can neither handle that many objects nor a hierarchy this deep! Unity 既不能处理那么多对象,也不能处理这么深的层次结构! ( a nice article on Unity's own website about this ). Unity 自己的网站上关于此的一篇不错的文章)。 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.您必须在专用软件(通常是 3D 建模软件)中减少和组合对象并将这些对象导入统一。

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. Unity 没有提供这样的方法来保存嵌套的 prefab 修改,但是可以通过一个技巧来实现。

The solutions is:解决方案是:

  1. Instantiate another nested prefab by PrefabUtility.InstantiatePrefab通过PrefabUtility.InstantiatePrefab实例化另一个嵌套预制件
  2. Modify that new GameObject of the prefab修改预制件的新游戏对象
  3. Save the modification by PrefabUtility.SaveAsPrefabAssetAndConnect通过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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM