Download Downloading - All IT eBooks
Transcript
292
CHAPTER 12
Deploying your game to players’ devices
if (Application.platform == RuntimePlatform.IPhonePlayer)
val = _TestNumber();
Call this if platform
return val;
is IPhonePlayer
}
public static string TestString(string test) {
string val = "";
if (Application.platform == RuntimePlatform.IPhonePlayer)
val = _TestString(test);
return val;
}
}
First, note that the static Initialize() function creates a permanent object in the
scene so that you don’t have to do it manually in the editor. You haven’t previously
seen code to create an object from scratch because it’s a lot simpler to use a prefab in
most cases, but in this case it’s cleaner to create the object in code (so that you can use
the plug-in script without editing the scene).
The main wizardry going on here involves the DLLImport and static extern commands. Those commands tell Unity to link up to functions in the native code you provide. Then you can use those referenced functions in this script’s methods (with a
check to make sure the code is running on iPhone/iOS).
Next you’ll use these plug-in functions to test them. Create a new script called
MobileTestObject, create an empty object in the scene, and then attach the script (see
the next listing) to the object.
Listing 12.5 Using the plug-in from MobileTestObject
using UnityEngine;
using System.Collections;
public class MobileTestObject : MonoBehaviour {
private string _message;
void Awake() {
TestPlugin.Initialize();
}
Initialize the plug-in
at the beginning.
// Use this for initialization
void Start() {
_message = "START: " + TestPlugin.TestString("ThIs Is A tEsT");
}
// Update is called once per frame
void Update() {
// Make sure the user touched the screen
if (Input.touchCount==0){return;}
Respond to touch input.
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) {
Licensed to Mark Watson <[email protected]>