Game Development Reference
In-Depth Information
Platform Dependent Compilation
The desktop and mobile controllers are now added and configured successfully in the scene. If you
run the game now, however, both controllers will be active and operational—working simultaneously:
desktop input controlling the desktop controller, and mobile input controlling the mobile controller.
That's not what we want. The aim now is to code a C# script to deactivate the redundant controller
at scene start-up, leaving us with the relevant controller, based on the target platform. This can be
achieved using a Unity C# feature known as Platform Dependent Compilation . To see this in action,
create a new C# script file, ControlSwitcher.cs , and attach this as a component to the Controls
GameObject, created in the previous section (see Figure 5-10 ). Then take a look at Listing 5-1 for
ControlSwitcher.cs , after which comments follow.
Figure 5-10. Attaching a ControlSwitcher script to the Controls object. This script will deactivate the redundant First Person
Controller on scene start-up
Listing 5-1. ControlSwitcher.cs: Deactivates Redundant First Person Controller
01 //------------------------------------------------
02 using UnityEngine;
03 using System.Collections;
04 //------------------------------------------------
05 public class ControlSwitcher : MonoBehaviour
06 {
07 //------------------------------------------------
08 //Reference to desktop first person controller (default)
09 public GameObject DesktopFirstPerson = null;
10
11 //Reference to mobile first person controller
12 public GameObject MobileFirstPerson = null;
13
14 //------------------------------------------------
15 //Select appropriate first person control for platform
 
Search WWH ::




Custom Search