Any way of importing .tmx files in runtime? (From external folder)
I'm making a game that uses tiled maps as game arenas, and I would like to enable the player to create their own custom maps in Tiled, and import into the built game. Something like putting the .tmx, .tsx and .png files inside a game folder, that looks into that and loads the .tmx files and instantiates the gameobject created into the scene.
Is there any way I can achieve this? How does the importer works? It needs more than the file .xml contents to import the map? What kind of Editor Specific code is used to import the files? (I can help in some sense to create a custom importer that converts Editor Specific code into runtime only code)
I've seen a closed issue where is recommended using the AssetBundles and Adressables, but either of these needs the user to be able to access the Unity project to be able to pack the maps into bundles. But the functionality I need should be able to just read the .xml contents from a file in a folder (and find specific dependencies within the .xml itself, since .tmx saves file references in its file contents)
Thank you!
Hi there, @RotcivOcnarb. For something like this you'll need the set up your users with some way to import their own content in Unity and have it built into an Asset Bundle or perhaps an Addressable Asset (newer Unity tech that will eventually replace Asset Bundles).
Then your game would have to provide a mechanism to import/install the user-created content.
The reason why we can't just import Tiled assets at runtime is because of the heavy dependency on Unity tech that is only available in editor.
What kind of dependencies on Unity Editor is used when importing the tiles?
I took a look at the code, (is too much stuff but I kind of understood the main concept) and most of the work is XML parsing.
I saw some AssetDatabase asset manipulations, some importer stuff (like to add objects into the asset file), and some Scriptable Object creations (like reading the Tileset as a Scriptable Object)
But I think all of those things do have its runtime counterparts.
AssetDatabase loading can be switched for simple file IO reading
Texture2D also can load files in runtime without editor specific code
The scriptable objects can be made normal classes for data that can be loaded / unloaded in the memory to access the tileset data
Since the load is gonna be made in runtime, there is no need to manipulate the asset itself (with functions like AddObjectToAsset or SetMainObject
Of course, this loading is very intense to the processor, but since is a loading asset, it is done only once, and can be made async, the dev should be also aware to put some loading bar or something
I had tried to make some extensions into the plugin to be able to make some stuff like that, but oh boy, there is a lot of code there hehe....
I searched for other Tiled map importers for Unity that can load from external files without editor code, but neither of them are so complex than yours. I would be losing a LOT of tiled funcionality if I used them, so I have hope to make this work somehow with your plugin haha
I'll update to you if I manage to make something work!
OK, SO I AM REALLY EXCITED
I MADE IT WORK
Put some hours (maybe some days) by creating versions of the importers that run without editor specific code
I've kind of hacked the classes, extracted some of them to be compiled with the built, only the REAL editor specific classes are not compiled, it is definetely not the most optimized approach, but for my needs, it works like a charm
If you'd like to test, i've uploaded the built file that loads a .tmx from persistentDataPath. You can change the tileset before opening the game to see that the changes do in fact get applied into the game without the need to rebuilt
https://drive.google.com/file/d/1eYqbH6H8NOC6kQzZM8rZewGj7nFO7HBc/view?usp=sharing
You can put the packed Map folder into the persistentDataPath, or your own map if you'd like
The instructions are in the uppercase named text file
Currently .world files are not supported (but they could totaly be, I was just lazy)
I'll pack the modified version of the asset to link to you later, if you wanna see the code
EDIT: https://drive.google.com/file/d/1eA_wsOxKHAoL7UubYsBZdKxXp_V6VrKH/view?usp=sharing here you go
Hi @RotcivOcnarb ! Can you give me the code to import a map from persistentDataPath. Thanks!