Is there a simple #if UNITY?
The Unity Manual has a page on platform dependent compilation, which explains a set of #if
directives we can use to differentiate between the editor, Windows, Android, and so on. However, is there a similar directive for when Unity is used at all?
For instance:
string text;
#if UNITY
text = Resource.Load<string>("/path/to/textfile");
#else
text = System.IO.File.ReadAllText("/path/to/textfile");
#end
// read the text...
Essentially, the idea is that if the script is being run inside any Unity context whatsoever, the code uses Unity types to load the file. Otherwise, it uses System.IO
to load the file.
Does this facility exist in Unity? If it's not defined as UNITY
, is it something else?
What are you trying to do in the end? Are you making a plugin (dll) you could use outside Unity?
It would be a source file that I could use outside Unity, but the idea is similar.
Answer by hoy_smallfry · Jan 14, 2018 at 05:37 AM
I do not think that there is a pre-processor definition that Unity defines that does what you want. However, it's possible to define your own in Player Settings, so you could define UNITY that exists for your project, and simply avoid defining UNITY for your non-Unity code projects:
The only downside to this is that you would need to define this for every new project.
Although, your usage raises a question about why you would want to do it this way. If your code relies on reading a text file, it's possible to keep this functionality intact in Unity. Not only can you read files with System.IO.File in a Unity standalone, its also possible to have Unity to keep the text file as it's own separate file when you build a standalone if you use the Streaming Assets folder.