The question is answered, right answer was accepted
can't seem to open binary file after game is built (inside asset folder)
Hi
My game has a campaign system in which every map is a separate file I made in my custom map editor. So I save the map (which is a binary custom file) and so everything works great in the editor, I can open the file and load the map, but once I build the game and try the campaign in the built version of the game, it can't seem to locate the file and thus, open it. The file is located in the Asset folder of my Unity project. I have tried to move it to the resources folder but same problem.
Is there something I ignore about unity destroying custom files once the game is built ? Then destroying my campaign files ? I don't really understand the problem as it seems to find everything when I am in the editor. Hope my question is clear enough !
Answer by Bunny83 · Jan 11, 2020 at 01:31 AM
No, Unity does not "destroy" files. It's actually the other way round. No source files will be included in a build. Your Assets folder is a project staging folder. Unity only includes used / referenced assets in a build. However not in the source format. Assets are packed / converted into Unity's asset format.
What you probably are looking for is the StreamingAssets folder. Please read the documentation carefully. If you build for Android or WebGL the streaming assets can not be accessed through the normal file system. In this case you have to use a UnityWebRequest or the WWW class.
Ok so this is what I came up with :
//file = File.Open("Assets/Campaign/" + $$anonymous$$ain$$anonymous$$enu.mapName, File$$anonymous$$ode.Open);
file = File.Open(Application.strea$$anonymous$$gAssetsPath + "Campaign/" + $$anonymous$$ain$$anonymous$$enu.mapName, File$$anonymous$$ode.Open);
gameData = (GameData)bf.Deserialize(file);
But it is not working :( I am not sure if this is even what I need ? I don't really know what I'm looking for or how to have custom files into a build that I can access from within the game. Also, I am developing it for windows/$$anonymous$$ac, no mobile version is planned Do I have to create a Strea$$anonymous$$gAsset folder and place my files in there in order to access them ?
So you haven't read the documentation carefully ^^:
From the documentation:
Unity copies any files placed in the folder called Strea$$anonymous$$gAssets (case-sensitive) in a Unity Project verbatim to a particular folder on the target machine
This is a special folder like an "Editor" or "Resources" folder. The folder has to be located directly inside your Assets folder (like the plugins folder). $$anonymous$$aybe also have a look at the special folder names page
Yeah I got it working after a couple of tries and re reading the documentation. Thank you very much ! Saved my life for this one haha
Answer by valentin56610 · Jan 11, 2020 at 03:15 AM
To whomever who has the same problem as me, here is the solution. Go to your Asset folder in your unity project, there create a "StreamingAssets" folder, and this folder will be accessible once your game is built. the code to use is the following :
file = File.Open(Application.streamingAssetsPath + "/Campaign/" + MainMenu.mapName, FileMode.Open);
Follow this Question
Related Questions
Not writing to file correctly 1 Answer
Unity rewrite sln file causing Assembly-CSharp error 1 Answer
Access to the path is denied 0 Answers
[SOLVED]File don't save in persistentDataPath 1 Answer
Unity Errors when building 0 Answers