I’ve a level editor that saves files externally. My campaign levels are created with the same level editor. I want to then copy them from assets to the levels folder at runtime (located in MyDocuments/GameName/levels). This seams to only work during the editor though as it’s trying to use Application.dataPath. So is there any idea on how to actually do this? I want the campaign levels secure in the assets as I don’t want the originals altered, but the copies are safe to alter (hence why they’re being moved to the levels folder).
Application.dataPath works in builds.
You can just work with System.IO namespace to copy files over.
I’m already using System.IO. The files I want to copy are in my Unity project (assets) and I want to copy them to My Documents. I have all of this working, but it only works in the Editor. It won’t work at runtime as the assets are compiled. I need to know how to access the assets at runtime when it’s compiled.
Anyone have any idea? I’m pretty much stuck on this as I can’t copy over my campaign levels when the game runs. It does this to allow players to edit the campaign levels in the level editor and to reset the campaign level they just delete it. It will only copy if it doesn’t exist. Like I said though this only works in editor. It won’t do anything when compiled.
Edit: Maybe I should somehow open the levels and write them where I want them? If so then how do I open them as again the path won’t be valid to open them.
What step are you getting stuck at? Getting the file that should be copied, or the write-to-disk part?
I’m not sure how to get a .asset or .prefab file from the Assets directory. However, you can get files from the Streaming Assets folder, so if you put your levels there you should be able to access them. However, I’m not sure if you can write to that folder.
Now that I’m thinking about it, I don’t actually know how to create a .prefab or .asset file at runtime, then save it. All the methods I can quickly think of require the UnityEditor namespace which isn’t available at runtime.
Can you serialize the modified levels data, or store it in a JSON file? This is the method I have used most often. Then you can load the custom data and rebuild the level inside your game.
My files are just text files containing JSON. I want to copy them to My Documents from Application.dataPath, but that doesn’t work when the game is built. I’ll try Streaming Assets as well, but am concerned how they’ll be put in the the games built folder. I don’t want the plain text files there, but maybe a single compressed .pak that it extracts from.
Maybe Resources could work as well by loading them as TextAsset. Will also try it.