We’re working on a 2D platformer game and we’re loading our tile maps in from XML files. Originally we were doing this at runtime so the map would be created when the game runs but this resulted in a large loading time when loading in a level(naturally). We’ve recently decided it makes a whole bunch more sense to load the level in and create the map in Edit Mode, leaving us to just load the scene when running the game.
So one of the things I’m trying to work out at present is how to handle the tile map. It was the case that I was keeping track of it in a 2D Array of GameObjects which was populated as the map was created. This allowed me to know what tiles were surrounding other tiles in the map so that when an “explosion” occurs we can destroy a tile and it’s surrounding tiles very easily.
The issue we’re having now is that we only want to create the tile map in Edit Mode and not run the same script at all at runtime(since we want to avoid loading/creating the level then) which means the 2D Array we were using is now null.
I’m trying to find a solution to this issue. Does anyone know of a nice easy way to populate a 2D Array(in a script) in Edit Mode and then be able to access said 2D Array in play mode even though the script in question isn’t being run at all.
There could be a nice easy solution to this issue that I’m missing so I thought I’d see if you guys had any insight! =)
Well you can do that too with an attribute (ie) @script ExecuteInEditMode() at the top of your scripts. Remember that functions like start and awake will not work for obvious reasons.
Just to clarify: We have a script using [ExecuteInEditMode] to load in the Level. This “Level” is a GameObject in our hierarchy which parents each tile object in the level. After saving the Scene, we are left with the level already loaded in so we never need to(or want to) run said script again, as it has fulfilled its purpose.
The issue we’re trying to solve is we want to populate a 2D array with these tile GameObjects so we can reference them later on.
I can’t see why it would take a long time to load your tile map, even as non-binary. Did you do some tests to see how long just loading the raw data took? You can go down the route of creating your own unity editor scripts to be able to make the map inside unity but then there’s no hope of anyone ever modding/creating own levels
So why not create a script with an array that can be filled in the inspector manually or by script, then access it during runtime or edit mode. If the array is loaded in edit mode by script, from say references folder then use the attribute to save you some time, but if you just drag gameobjects to the array it will be there whenever you need it.