During the LD we've built a level generator in unity that permit us to convert pixels into prefabs instances. This is executed when editing levels and not at runtime. Here is a post to explain how it works.
Texture
For that first create an image with the level you want. Like this one:

With the following color mapping:
- Black : Empty
- White : Ground
- Red : Closed Door
- Dark Red: Opened Door.
You need to import that texture with the correct settings for the generator to work. It need to get the correct pixel color and not an interpolated one:

Then create one prefab for each color. Create also a script that have the "pixel texture" and that contains the Color to GameObject mapping. This script will handle prefab instantiation.
In the script you can start by looping through texture's pixels like that:
Texture2D mapTexture = Resources.Load<Texture2D>(fileLevel);
for (int y = 0; y < mapTexture.height; y++)
{
for (int x = 0; x < mapTexture.width; x++)
{
Color color = mapTexture.GetPixel(x, y);
... // Instantiation code
}
}
Instantiation
The instantiation code:
GameObject gameObjectCorrespondingToColor = GetGameObjectCorrespondingToColor(color);
string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(gameObjectCorrespondingToColor);
object prefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(System.Object));
UnityEngine.Object unityObj = PrefabUtility.InstantiatePrefab(prefab, transform);
GameObject instance = (GameObject) unityObj;
Don't use Instantiate but use InstanciatePrefab to have your objects linked to the prefab. That way all levels will be updated if you change a prefab.
Then apply transformation for your instance to be at the right place:
instance.transform.Translate(new Vector3(x, 0, y));
You also can set the current GameObject as parent of the instance:
instance.transform.parent = GetComponent<Transform>();
Builder
You can have buttons in your editor to build and delete the level:

The code to have those buttons looks like that:
[CustomEditor(typeof(MapController))]
public class MapControllerBuilder : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
MapController mapController = (MapController) target;
if(GUILayout.Button("Build Map"))
{
mapController.BuildMap();
}
}
}
Then when you click on "Build Map" you will have your level built:

If you have a better way of doing that, left us your idea in the comments.
Thanks for reading. Check out our game to see that in action. And thank to the folks who already checked out our game :heart:.
