In a editor, I’m creating multiple gameobjects and I need to apply them directly to the prefab. So basically what I did is :
GameObject prefab = GetPrefab();
string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(prefab);
for (int i = 0; i < 10; i++)
{
GameObject instance = new GameObject();
....
PrefabUtility.ApplyAddedGameObject(instance, prefabPath, InteractionMode.AutomatedAction);
}
Problem is, each ApplyAddedGameObject() is very slow : the full process is taking minutes of loading at this point.
So I’m looking for a function to apply gameobjects in bulk, but it doesn’t seem to exist publicly in the API (please correct me if I’m wrong !).
In the Unity CS reference github, I found the following function, called internally by ApplyAddedGameObject() like this :
PrefabUtility.AddGameObjectsToPrefabAndConnect(
new GameObject[ ] { gameObject },
prefabSourceGameObjectParent);
While a batch API would be nice I think it would be better to edit the prefab directly in this case
GameObject prefab = GetPrefab();
string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(prefab);
// The editing scope will automatically save, reimport prefab and unload contents
using (var editingScope = new PrefabUtility.EditPrefabContentsScope(prefabPath))
{
var rootTransform = editingScope.prefabContentsRoot.GetComponent<Transform>();
for (int i = 0; i < 10; ++i)
{
var newGo = new GameObject();
newGo.GetComponent<Transform>().parent = rootTransform;
}
}
Interesting ! Unfortunately, we are stuck in Unity 2019.3 for what-it-seems the rest of the production of our game and this function is only available in Unity 2020.1 (unless it will be backported to 2019.4), but I will keep that in mind !
It works ! The process now only takes a few seconds. Thanks !