3
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ File.ReadAllText? \$\endgroup\$ – Ave Aug 25 '16 at 0:42
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Uri Popov Aug 25 '16 at 7:58
  • \$\begingroup\$ @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. \$\endgroup\$ – lentinant Aug 25 '16 at 8:30
  • \$\begingroup\$ Take a look to ScriptableObject they can be used with the Unity buildin JSON Serialization \$\endgroup\$ – Sata Aug 25 '16 at 17:31
  • \$\begingroup\$ @Sata thanks for input, but how to store data in files, and how to read this data, is another question. \$\endgroup\$ – lentinant Aug 26 '16 at 12:35
1
\$\begingroup\$

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.

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

Put this in a editor folder inside the Assets folder.

using System.Diagnostics;
using UnityEditor;

public class CustomBuild {

    [MenuItem("MyTools/Windows Build With Postprocess")]
    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 ;)

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Philipp Aug 25 '16 at 11:36
  • \$\begingroup\$ @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. \$\endgroup\$ – Uri Popov Aug 26 '16 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.