{"author_link":"\/users\/pndaa","author_name":"pndaa","author_uid":"pndaa","comments":[],"epoch":1587804944,"event":"LD46","format":"md","ldjam_node_id":208038,"likes":9,"metadata":{"p_key":"144961","p_author":"pndaa","p_authorkey":"1001569","p_urlkey":"361374","p_title":"Lost Goblins - Level generation","p_cat":"LDJam ","p_event":"LD46","p_time":"1587804944","p_likes":"9","p_comments":"0","p_status":"WAYBACK","us_key":"1001569","us_name":"pndaa","us_username":"pndaa","event_start":"1587081600","event_key":"77","event_name":"Ludum Dare 46"},"node":{"_collation":{"body_sanitizer":"TextUtils::SanitizeHTML via existing importer","event":"LD46","removed_author":false},"_superparent":176557,"_trust":11,"author":1569,"body":"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.\n\n## Texture\n\nFor that first create an image with the level you want. Like this one:\n\n![pixelimage.png](\/\/\/raw\/126\/z\/33285.png)\n\nWith the following color mapping:\n- Black : Empty\n- White : Ground\n- Red : Closed Door\n- Dark Red: Opened Door.\n\nYou 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:\n\n![pixelImageConfig.png](\/\/\/raw\/126\/z\/33289.png)\n\nThen 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.\n\nIn the script you can start by looping through texture's pixels like that:\n\n```\nTexture2D mapTexture = Resources.Load<Texture2D>(fileLevel);\nfor (int y = 0; y < mapTexture.height; y++)\n{\n    for (int x = 0; x < mapTexture.width; x++)\n    {\n        Color color = mapTexture.GetPixel(x, y);\n        ... \/\/ Instantiation code\n    }\n}\n```\n\n## Instantiation\n\nThe instantiation code:\n\n```\nGameObject gameObjectCorrespondingToColor = GetGameObjectCorrespondingToColor(color);\nstring prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(gameObjectCorrespondingToColor);\nobject prefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(System.Object));\nUnityEngine.Object unityObj = PrefabUtility.InstantiatePrefab(prefab, transform);\nGameObject instance = (GameObject) unityObj;\n```\n\nDon'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.\n\nThen apply transformation for your instance to be at the right place:\n\n```\ninstance.transform.Translate(new Vector3(x, 0, y));\n```\n\nYou also can set the current GameObject as parent of the instance:\n```\ninstance.transform.parent = GetComponent<Transform>();\n```\n\n## Builder\nYou can have buttons in your editor to build and delete the level:\n\n![buildbutton.png](\/\/\/raw\/126\/z\/33280.png)\n\nThe code to have those buttons looks like that:\n```\n[CustomEditor(typeof(MapController))]\npublic class MapControllerBuilder : Editor\n{\n    public override void OnInspectorGUI()\n    {\n        DrawDefaultInspector();\n        MapController mapController = (MapController) target;\n        if(GUILayout.Button(\"Build Map\"))\n        {\n            mapController.BuildMap();\n        }\n    }\n}\n```\n\nThen when you click on \"Build Map\" you will have your level built:\n\n![generated.png](\/\/\/raw\/126\/z\/3327b.png)\n\nIf you have a better way of doing that, left us your idea in the comments.\n\n\nThanks for reading. [Check out our game to see that in action.](https:\/\/ldjam.com\/events\/ludum-dare\/46\/lost-goblins) And thank to the folks who already checked out our game :heart:.\n\n![action.gif](\/\/\/raw\/126\/z\/33282.gif)\n\n","comments":0,"created":"2020-04-25T08:06:35Z","files":[],"files-timestamp":0,"id":208038,"love":9,"love-timestamp":"2020-04-25T11:52:01Z","meta":[],"modified":"2020-04-25T11:52:01Z","name":"Lost Goblins - Level generation","node-timestamp":"2020-04-25T10:06:55Z","parent":188384,"parents":[1,5,9,176557,188384],"path":"\/events\/ludum-dare\/46\/lost-goblins\/lost-goblins-level-generation","published":"2020-04-25T08:55:44Z","scope":"public","slug":"lost-goblins-level-generation","subsubtype":"","subtype":"","type":"post","version":630949},"node_metadata":{"n_key":"208038","n_urlkey":"361374","n_parent":"188384","n_path":"\/events\/ludum-dare\/46\/lost-goblins\/lost-goblins-level-generation","n_slug":"lost-goblins-level-generation","n_type":"post","n_subtype":"","n_subsubtype":"","n_author":"1569","n_created":"1587801995","n_modified":"1587815521","n_version":"630949","n_status":"WAYBACK"},"source_url":"https:\/\/ldjam.com\/events\/ludum-dare\/46\/lost-goblins\/lost-goblins-level-generation","text":"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.\n\n## Texture\n\nFor that first create an image with the level you want. Like this one:\n\n![pixelimage.png](\/\/\/raw\/126\/z\/33285.png)\n\nWith the following color mapping:\n- Black : Empty\n- White : Ground\n- Red : Closed Door\n- Dark Red: Opened Door.\n\nYou 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:\n\n![pixelImageConfig.png](\/\/\/raw\/126\/z\/33289.png)\n\nThen 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.\n\nIn the script you can start by looping through texture's pixels like that:\n\n```\nTexture2D mapTexture = Resources.Load<Texture2D>(fileLevel);\nfor (int y = 0; y < mapTexture.height; y++)\n{\n    for (int x = 0; x < mapTexture.width; x++)\n    {\n        Color color = mapTexture.GetPixel(x, y);\n        ... \/\/ Instantiation code\n    }\n}\n```\n\n## Instantiation\n\nThe instantiation code:\n\n```\nGameObject gameObjectCorrespondingToColor = GetGameObjectCorrespondingToColor(color);\nstring prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(gameObjectCorrespondingToColor);\nobject prefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(System.Object));\nUnityEngine.Object unityObj = PrefabUtility.InstantiatePrefab(prefab, transform);\nGameObject instance = (GameObject) unityObj;\n```\n\nDon'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.\n\nThen apply transformation for your instance to be at the right place:\n\n```\ninstance.transform.Translate(new Vector3(x, 0, y));\n```\n\nYou also can set the current GameObject as parent of the instance:\n```\ninstance.transform.parent = GetComponent<Transform>();\n```\n\n## Builder\nYou can have buttons in your editor to build and delete the level:\n\n![buildbutton.png](\/\/\/raw\/126\/z\/33280.png)\n\nThe code to have those buttons looks like that:\n```\n[CustomEditor(typeof(MapController))]\npublic class MapControllerBuilder : Editor\n{\n    public override void OnInspectorGUI()\n    {\n        DrawDefaultInspector();\n        MapController mapController = (MapController) target;\n        if(GUILayout.Button(\"Build Map\"))\n        {\n            mapController.BuildMap();\n        }\n    }\n}\n```\n\nThen when you click on \"Build Map\" you will have your level built:\n\n![generated.png](\/\/\/raw\/126\/z\/3327b.png)\n\nIf you have a better way of doing that, left us your idea in the comments.\n\n\nThanks for reading. [Check out our game to see that in action.](https:\/\/ldjam.com\/events\/ludum-dare\/46\/lost-goblins) And thank to the folks who already checked out our game :heart:.\n\n![action.gif](\/\/\/raw\/126\/z\/33282.gif)\n\n","title":"Lost Goblins - Level generation","wayback_source":[]}