Hi chaps,
I’ve got two solution approaches and would like to hear which is best.
The work to do is a zoom button for a touch input android device. When button is pressed, the game camera will pull back/zoom out allowing the player to see a bit more of the scenery.
Approach 1: Add a GUItexture gameObject with a script attached.
Approach 2: A scrip attached to an empty gameObject with OnGUI() function and a GUIStyle.
The code for both approaches is:
using UnityEngine;
using System.Collections;
// On the making!
public class ZoomButton : MonoBehaviour {
private float screenWidth;
private float screenHeight;
private float unitW, unitH;
private Rect pixelInsetRect;
void Start () {
// Size related.
screenWidth = Screen.width;
screenHeight = Screen.height;
unitW = screenWidth/20;
unitH = screenHeight/20;
pixelInsetRect = new Rect((this.guiTexture.pixelInset.x) * unitW, (this.guiTexture.pixelInset.y) * unitH,
2.5f*unitW, 2.5f*unitW);
this.guiTexture.pixelInset = pixelInsetRect;
}
// 1st aproach:
private int countedTouches;
void Update (){
countedTouches = Input.touchCount;
for (int i = 0; i < countedTouches; i++) {
Touch touch = Input.GetTouch(i);
if (this.guiTexture.HitTest( touch.position )) {
Debug.Log("Do the zoom out! - 1st aproach");
}
}
}
// 2nd aproach
public GUIStyle zoomButtonStyle;
void OnGUI() {
if (GUI.Button(new Rect(16*unitW, 16*unitH, 3*unitW, 2*unitH), "", zoomButtonStyle)){
Debug.Log("Do the zoom out! - 2nd aproach");
}
}
}
What would be more effective and require less effort to the hardware?
Thanks for your any comments.