# Make Unity game using files directly from game directory

For stats of enemies, and several other kinds of info, I've decided to use not inspector fields or hardcoded values, but to store values to some XML and other types of files, and read them from scripts, obtaining required values. And I want this files to stay in game folder, not packed to *.assets file, because I'd like to have possibility to quickly modify this values without need to rebuild whole game.

So, how can I mark asset in editor, that it should not be included to *.assets, but stay in unchanged form in game folder?

UPDATE: I will slightly rephrase what I want to achieve. I need possibility to have file in game directory, so I can read it from game, and I'd like to have this possibility in both editor and built game. So, as for answer, I will also receive information about what folder is considered as game folder in editor.

• File.ReadAllText? – Ave Aug 25 '16 at 0:42
• are you making the game for PC or mobile platforms ? Because if its for PC its trivial for mobile platforms it's a bit more tricky. – Uri Popov Aug 25 '16 at 7:58
• @UriPopov as for now, I aim for PC. I suppose, I will port my game to mobile platforms, when balance questions will be more or less settled, so this will be not problem then. – lentinant Aug 25 '16 at 8:30
• Take a look to ScriptableObject they can be used with the Unity buildin JSON Serialization – Sata Aug 25 '16 at 17:31
• @Sata thanks for input, but how to store data in files, and how to read this data, is another question. – lentinant Aug 26 '16 at 12:35

You will need to save your things in Streaming Assets folder:

http://docs.unity3d.com/Manual/StreamingAssets.html

And using some serializer, if you are using Unity below version 4, you should use whydoidoit serializer:

https://www.assetstore.unity3d.com/en/#!/content/3675

If you are using higher versions you can try an adapted version of this from TheSniperFan called unityserializer-ng:

https://gitgud.io/TheSniperFan/unityserializer-ng/tree/master-pre-5.4

Or even my API that is a forked and simplified version of it with a few things more, but it's still very undocumented and messy:

https://github.com/Ikillnukes/Lerp2API (it doesn't have scene saver, but I will implement it)

The way you can achieve this is by doing this, imagine you have an gameobject called "myobject", you can do myobject.SaveTreeObject(); to convert it to a byte[] variable (you can save it using System.IO.File.WriteAllBytes) and to load it by using LevelLoader.LoadObhectTree(Action) being careful, because the action represents the function that load the object.

My API currently does that I said.

Good luck.

• Streaming assets look exactly like what I'm looking for, thanks! – lentinant Aug 25 '16 at 13:20
• Yes, of course you can do modding, instead of using asset bundles (this more a server solution) – z3nth10n Aug 25 '16 at 14:12

Put this in a editor folder inside the Assets folder.

using System.Diagnostics;
using UnityEditor;

public class CustomBuild {

public static void BuildGame()
{
// Get filename.
string path = EditorUtility.SaveFolderPanel("Choose Location of Built Game", "", "");
string[] levels = new string[] { "Assets/Scene1.unity"};

// Build player.
BuildPipeline.BuildPlayer(levels, path + "/BuiltGame.exe", BuildTarget.StandaloneWindows, BuildOptions.None);

// Copy a file from the project folder to the build folder, alongside the built game.
//This does not check if the file already exist or not you need to do this by yourself.
FileUtil.CopyFileOrDirectory("Assets/test.txt", path + "/test.txt");

// Run the game (Process class from System.Diagnostics).
Process proc = new Process();
proc.StartInfo.FileName = path + "/BuiltGame.exe";
proc.Start();
}
}


I'm not sure how you can do this on mobile. On Android for example you might want to create the file when your game first launches to some extern directory on the device and modify it from there. Also not sure how it will work on Mac since I know almost nothing about Macs. Have fun ;)

• My experience is that a script with the using UnityEditor; import works while you test in the editor but will fail on proper building due to a missing dependency (yes, also on a Windows build). – Philipp Aug 25 '16 at 11:36
• @Philipp but the whole idea of this script is to put it in a Editor folder so that it does not get build into the final build. This simply creates a custom menu that calls the methods that get called when you click the build button. And yes if it is not in a Editor folder Unity will not build it since it does not include the UnityEditor assembly. – Uri Popov Aug 26 '16 at 8:23