Skip to main content
Version: 2.0.2

Keep prefabs in editor

After a level is generated, each room template prefab that was used in the level goes through the Object.Instantiate() method. This method removes the connection to the original prefab, which basically means that the whole prefab is unpacked (described here in Unity docs.) However, it might be sometimes useful to keep the references to the prefabs. For example, if the level generator is used in the Editor and then manual changes are made to that level.

Solution​

There is currently no simple solution that would require just ticking some checkbox in the Editor. However, it is possible to change the default behaviour by changing a line or two directly in the source code of the asset.

Locate the GeneratorUtilsGrid2D class and inside the class there is the InstantiateRoomTemplate (as shown in the source code below). In that method, there are two constants which you can change to keep prefab references when generating levels directly in the Editor.

private static GameObject InstantiateRoomTemplate(GameObject roomTemplatePrefab)
{
// Set to true if you want to keep prefabs when generating levels in the editor
const bool keepPrefabsInEditor = false;

// Set to true if you want to unpack the root game object of the prefab
// (keepPrefabsInEditor must be true for this constant to change anything)
const bool unpackRootObject = false;

// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (!Application.isPlaying && keepPrefabsInEditor)
{
#if UNITY_EDITOR
var roomTemplateInstance = (GameObject) PrefabUtility.InstantiatePrefab(roomTemplatePrefab);

if (unpackRootObject)
{
#pragma warning disable CS0162 // Unreachable code detected
PrefabUtility.UnpackPrefabInstance(
roomTemplateInstance,
PrefabUnpackMode.OutermostRoot,
InteractionMode.AutomatedAction
);
#pragma warning restore CS0162 // Unreachable code detected
}

return roomTemplateInstance;
#endif
}

return Object.Instantiate(roomTemplatePrefab);
}