AIM: Develop and test a simple unity project that exports to both web and ... http://
docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html.
1. Unity investigation 1: Touch input && mouse and keyboard input
AIM: Develop and test a simple unity project that exports to both web and mobile with "Mouse and keyboard control" as well as "touch and multitouch". (prototype with example code) We built a prototype game to test some of these features, due to my inexperience unity3d and c# I did not develop a robust touch/multi touch system. We got some good swiping in, but due to some prevailing issues we settled for “tap the buttons”, which factored more into the binary controls of the existing game. Similar to flash, you can have click, and mouse up and mouse down events. Unity sadly does not combine these or correctly mirror these functions across to touch devices. Instead it listens to the activity which needs to be measures. And so of course you have to write specific code. For example “tap” you have to listen for the touch event to begin and take the first event, figure out it’s x,y position (add a z=0) and perform hit detection on your 2d button elements With the unity export, you have to state which platform you are exporting to. Since I worked in mainly with the flash export in mind, many traditional scripts would not compile as flash lacked the relevant libraries. To counteract this issue, Unity Editor and compiler can “filter off” platform dependencies via if statements within the code. http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html So here is the basic movement script for my in-game character for “both” web and ios deployment sat in the same code (c#). First off I have to remove the controlling movement buttons for every platform except iOS (note you can put in andriod here as well) void Awake() { finiteStateMachine = FindObjectOfType(typeof(FSM)) as FSM; if (!finiteStateMachine){ Debug.Log("No link to Level Status"); } #if (!UNITY_IPHONE || UNITY_EDITOR) leftButton.enabled = false; rightButton.enabled = false; upButton.enabled = false; #endif }
void Update() { Vector2 pos = Vector2.zero; Vector3 pos3d = Vector3.zero; #if (UNITY_IPHONE) // here we capture “touch” and “swipes”. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) { Touch primaryTouch = Input.GetTouch(0); Velocity_X = primaryTouch.deltaPosition.x / primaryTouch.deltaTime;
Velocity_Y = primaryTouch.deltaPosition.y / primaryTouch.deltaTime; }
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { /* Touch a 2d reference needs it’s third component in 3d space in order to hit test against the 3d elements in the scene, including the gui. */ pos = Input.GetTouch(0).position; pos3d = new Vector3(pos.x,pos.y,0);
peform a
} #endif /* in unity one can check for “horizontal// vertical movement” from all the control systems but to eliminate slide on my game character, I chose specific key actions */ if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow) || leftButton.HitTest(pos3d)) { // left movement code } if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow) || rightButton.HitTest(pos3d)) { // right movement code } if (Input.GetButton("Jump") || upButton.HitTest(pos3d)) { // jump }
Similarly I have a button script which can take/swap two texture for the hover/active states, and sends a request (upwards) to a parent object, in my case the camera, which I’ve used use same “listening” script to pool all the call to actions for the game. The code even listens for “touch” based taps as well. using UnityEngine; using System.Collections; public class ButtonBehaviour : MonoBehaviour { public public public string
Texture2D normal; Texture2D hover; string buttonName; mouseover;
void Awake() { }
// Use this for initialization void Start () {
} void OnMouseOver() { guiTexture.texture = hover; } void OnMouseExit() { guiTexture.texture = normal; } void OnMouseDown() { SendMessageUpwards("buttonPress",buttonName); }
// Update is called once per frame void Update() { #if (UNITY_IPHONE) if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { Vector2 pos = Input.GetTouch(0).position; if (guiTexture.HitTest(new Vector3(pos.x,pos.y,0))) { SendMessageUpwards("buttonPress",buttonName); }
} #endif } }