- Home /
GUITexture Button Help, Android touch
I have a GUITexture button im trying to get working from a mobile app im working on for android. Im trying to be able to touch the button have it display "TOUCHED" in the Debug.Log. The Problem is that when i touch the GUITexture button, the only place that it registers the touch is in the top right corner of the GUITexture Button. This is the code i have so far:
private var gui : GUITexture;
private var defaultRect : Rect;
private var guiTouchOffset : Vector2;
function Start(){
gui = GetComponent( GUITexture );
// get where the gui texture was originally placed
defaultRect = gui.pixelInset;
// get our offset for center instead of corner
guiTouchOffset.x = defaultRect.width;
guiTouchOffset.y = defaultRect.height;
}
function Update(){
var count = Input.touchCount;
// account for the offset in our calculations
for (var i: int = 0; i < count; i++){
var touch : Touch = Input.GetTouch(i);
var guiTouchPos : Vector2 = touch.position - guiTouchOffset;
if (gui.HitTest( touch.position )){
Debug.Log("Touched");
gui.pixelInset.x = guiTouchPos.x;
gui.pixelInset.y = guiTouchPos.y;
}
}
}
any help Is greatly appreciated. This thing has been troubling me for the longest time now. Thanks
Answer by Triqy · Oct 01, 2011 at 06:04 PM
Never mind. I answered my own question. Here is two ways to do it:
> function Update() > > { > > for (var touch : Touch in > Input.touches) > > { > > if (guiTexture.HitTest > (touch.position)) >
> { > > // we are now in the guitexture > > Debug.Log("Touch"); > > > > } > > } > > }
private var gui : GUITexture;
function Start(){
gui = GetComponent( GUITexture );
var newInset : Rect = new Rect(0,350,134,40);
guiTexture.pixelInset = newInset;
}
function Update(){
if(Input.touchCount > 0){
for(var i : int = 0; i < Input.touchCount; i++){
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began &&
guiTexture.HitTest(touch.position)){
Debug.Log("logged");
}
}
}
}
Answer by Triqy · Mar 22, 2013 at 05:58 AM
This is a cleaner and more understandable answer!
http://answers.unity3d.com/questions/422187/android-guitexture-touch.html
Your answer
Follow this Question
Related Questions
Detect touch on a specific GUITexture 3 Answers
GUITexture and Touch Input 1 Answer
Touch Button for 4 animation 2 Answers
Buttons and Touch area issue on android 0 Answers
Android touch latency vs. audio playback 0 Answers