Video:
Description:
What is the Platformer Editor Tool?
The Platformer Editor Tool is a tool for quickly creating and editing platformer levels. It’s based on drawing pixel maps of your level and then mapping each colour to a GameObject. The tool will detect each colour in your texture and shows this in a list, from where you can select prefabs. After clicking on “Generate Level” the prefabs will be placed on the exact same spot as their matching colours. The tool is mainly for 2D editing.
The tool is made for Unity.
How it works
For editing levels, I created an editor window. In this window you can create a “Level Data” list. This is a ScriptableObject, which contains: the name of your level, the texture of your level, the ColourMappings and it can also store a prefab of your generated tiles.
Once you created a new list, or opened an already existing one, you can edit the name and the texture of your curren level. Once you select a texture, you can create your ColourMappings. The ColourMappings consist of a colour and a GameObject. These colours are detected with nested for-oops (x and y). In this for loop, the tool iterates over every pixel in the texture and saves eery unique colour to a list. For every colour in the list, a ColourMapping is created with this colour. If you need to, you can remove ColourMappings by clicking on “Delete Last Mapping”. Only the last ColourMapping will be removed.
After mapping the colours to prefabs, you can generate your level. The tool will instantiate each prefab in editor mode in your scene. It will place these gameobjects as children of (levelName + ” Generated Tiles”) in the Hierarchy.
You can choose to export your level, which will create a prefab from the Generated Tiles object and places it in your LevelData ScriptableObject.
Started: November 2017
Finished: January 2018
LINKS:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
public class PlatformerLevelWindow : EditorWindow { // Reference to ScriptableObject public LevelData levelData; // Texture that will be read public Texture2D map; // Data private string levelName; private GameObject parentObject; private Vector2 scrollPos; [MenuItem("My Tools/Platformer Level Editor")] private static void Init() { // Get existing open window, or if none make a new one PlatformerLevelWindow window = (PlatformerLevelWindow)GetWindow(typeof(PlatformerLevelWindow), true, "Platformer Level Editor"); window.Show(); } private void OnEnable() { // Load ScriptableObject with level data if it exists if (EditorPrefs.HasKey("ObjectPath")) { string objectPath = EditorPrefs.GetString("ObjectPath"); levelData = (LevelData)AssetDatabase.LoadAssetAtPath(objectPath, typeof(LevelData)); } } #region Editor GUI private void OnGUI() { DisplayLevelDataMenu(); if (levelData != null) { DisplayLevelDataEditor(); } // Save changes to ScriptableObject if (GUI.changed) { EditorUtility.SetDirty(levelData); AssetDatabase.SaveAssets(); } } private void DisplayLevelDataMenu() { GUILayout.BeginHorizontal(); GUILayout.Space(10); // Open list from files if (GUILayout.Button("Open List")) { OpenList(); } // Select existing list in project if (levelData) { if (GUILayout.Button("Select Existing List")) { Selection.activeObject = levelData; } } // Create a new list if (GUILayout.Button("New List")) { CreateNewList(); } GUILayout.EndHorizontal(); EditorGUILayout.Space(); } private void DisplayLevelDataEditor() { EditorGUILayout.Space(); // LEVEL NAME levelName = levelData.levelName; levelName = EditorGUILayout.TextField("Level name", levelName); levelData.levelName = levelName; EditorGUILayout.Space(); GUILayout.Label("Color Mapping", EditorStyles.boldLabel); EditorGUILayout.Space(); // TEXTURE levelData.texture = (Texture2D)EditorGUILayout.ObjectField("Texture", levelData.texture, typeof(Texture2D), true); map = levelData.texture; DisplayColorMappings(); DisplayColorMappingsMenu(); DisplayGenerateExportMenu(); } // Show as many colors/prefabs as there are in levelData private void DisplayColorMappings() { EditorGUILayout.Space(); scrollPos = EditorGUILayout.BeginScrollView(scrollPos); if (levelData.mappings.Count > 0) { foreach (ColorMapping mapping in levelData.mappings) { EditorGUILayout.BeginHorizontal(); // Color GUILayout.Label("Color"); mapping.color = EditorGUILayout.ColorField(mapping.color); // Prefab GUILayout.Label("Prefab"); mapping.prefab = (GameObject)EditorGUILayout.ObjectField(mapping.prefab, typeof(GameObject), true); EditorGUILayout.EndHorizontal(); } } } private void DisplayColorMappingsMenu() { EditorGUILayout.BeginHorizontal(); // Create color mappings if (GUILayout.Button("Create Color Mappings")) { GetColors(); } // Delete last mapping in list // TODO let user select which colormapping to delete if (GUILayout.Button("Delete Last Mapping")) { levelData.mappings.RemoveAt(levelData.mappings.Count - 1); } EditorGUILayout.EndHorizontal(); GUILayout.EndScrollView(); EditorGUILayout.Space(); } private void DisplayGenerateExportMenu() { EditorGUILayout.BeginHorizontal(); // Generate level from level data if (GUILayout.Button("Generate Level")) { // Create parent object for the tiles parentObject = new GameObject(levelName + " Generated Tiles"); GenerateLevel(); } // Create prefab from Generated Tiles and assign to level data if (GUILayout.Button("Export Level")) { if (parentObject) { ExportLevel(); } else { Debug.LogError("Can't export level if it's not generated! Click on 'Generate Level'."); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); } #endregion private void CreateNewList() { levelData = CreateLevelData.Create(); if (levelData) { levelData.mappings = new List<ColorMapping>(); string relPath = AssetDatabase.GetAssetPath(levelData); EditorPrefs.SetString("ObjectPath", relPath); Selection.activeObject = levelData; } } private void OpenList() { string absPath = EditorUtility.OpenFilePanel("Select Color Mapping List", "", ""); if (absPath.StartsWith(Application.dataPath)) { string relPath = absPath.Substring(Application.dataPath.Length - "Assets".Length); levelData = (LevelData)AssetDatabase.LoadAssetAtPath(relPath, typeof(LevelData)); if (levelData) { EditorPrefs.SetString("ObjectPath", relPath); } if (levelData.mappings == null) { levelData.mappings = new List<ColorMapping>(); } Selection.activeObject = levelData; } } private void GetColors() { List<Color> colors = new List<Color>(); for (int x = 0; x < map.width; x++) { for (int y = 0; y < map.height; y++) { Color pixelColor = map.GetPixel(x, y); if (!colors.Contains(pixelColor) && pixelColor.a >= 0.9) { colors.Add(pixelColor); NewColorMapping(pixelColor); } } } } private void NewColorMapping(Color color) { ColorMapping newMapping = new ColorMapping(); newMapping.color = color; newMapping.prefab = null; levelData.mappings.Add(newMapping); } [ExecuteInEditMode] private void GenerateLevel() { // Iterate over pixels in map. for (int x = 0; x < map.width; x++) { for (int y = 0; y < map.height; y++) { GenerateTile(x, y); } } } private void GenerateTile(int x, int y) { // Read color from pixel in map Color pixelColor = map.GetPixel(x, y); if (pixelColor.a == 0) { // The pixel is transparent. return; } foreach (ColorMapping mapping in levelData.mappings) { if (mapping.color.Equals(pixelColor)) { Vector2 position = new Vector2(x, y); Instantiate(mapping.prefab, position, Quaternion.identity, parentObject.transform); } } } private void ExportLevel() { Selection.activeGameObject = parentObject; GameObject[] gObjects = Selection.gameObjects; foreach(GameObject g in gObjects) { Object obj = PrefabUtility.CreateEmptyPrefab("Assets/" + g.gameObject.name + ".prefab"); Object prefab = PrefabUtility.ReplacePrefab(g.gameObject, obj, ReplacePrefabOptions.ConnectToPrefab); levelData.generatedTiles = prefab; } } } |