x


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

more ▼

asked Oct 01 '11 at 04:40 PM

bchilds88 gravatar image

bchilds88
70 5 10 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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");
}
}
}
}


more ▼

answered Oct 01 '11 at 06:04 PM

bchilds88 gravatar image

bchilds88
70 5 10 14

(comments are locked)
10|3000 characters needed characters left

This is a cleaner and more understandable answer!

http://answers.unity3d.com/questions/422187/android-guitexture-touch.html

more ▼

answered Mar 22 at 05:58 AM

bchilds88 gravatar image

bchilds88
70 5 10 14

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2448
x576
x476
x107

asked: Oct 01 '11 at 04:40 PM

Seen: 4690 times

Last Updated: Mar 22 at 05:58 AM