Game Development Reference
In-Depth Information
Statement
Description
This code targets all the RT-based platforms, including Windows Phone 8.1, Win-
dows 8.1, and the Universal apps
UNITY_WINRT_8_1
Note
It's worth noting that you are not limited to just the Unity preprocessor directives. You can
use Visual Studio's directives or even create your own by adding the following class to the
top of your
#define MyDirective
class (no semicolon). Then, you can block out
sections of your code by enabling or disabling this line. If a directive does not exist, it will
always be skipped.
To use these directives, we will simply declare them with an
#if
statement to surround
the code we want to target. For example, if you recall in
Chapter 11
,
Onward Wary Trav-
eler
, we learned that
File
classes don't work for Windows 8 because the OS does not
have the
File
class or more specifically, the
System.IO
class (if you build the project
for Windows 8 currently, you will get lots of such errors). So, to be able to load the files
for Windows 8, you need to use different code.
Updating the save system for another platform
To walk through the use of preprocessor directives, let's handle one such platform that
needs some attention, that is, Windows 8. In Unity's implementation on Windows 8 (due
to its asynchronous way of working), they have added some specialized classes because
the default implementations of these classes are not available on Windows 8 (in this case,
the
File
class). On Windows 8, you need to use the
UnityEngine.Windows.File
class instead of the normal
UnityEngine.File
class.
So, open up the
GameState
script and update the following code by adding the high-
lighted snippet:
public static void SaveState()
{
try
{
PlayerPrefs.SetString("CurrentLocation",
Application.loadedLevelName);