Hi , I am trying to create a custom button for example a fire button for unity … I don’t know how can I to do so :
for example I have GUI texture like button and should be a fire with touch
function Update () {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
// if the button == myButton ,, the button do something
}
}
Thank you
I use HitTest for this, works pretty well:
var fireButton : GUITexture;
function Update(){
for (var evt : Touch in Input.touches) {
var HitTest1 = fireButton.HitTest(evt.position);
if (evt.phase == TouchPhase.Began) {
if(HitTest1){
//do something when button is hit
} //end If
} //end If
} //end For
} //end Update
Done this way, Unity checks your touch is to see if it intersects with a pixel on your button image. If so, the hit test is positive. This code allows you to change the button by drag/dropping a new texture on the fireButton
variable in the Inspector, and using a GUITexture
allows you to have differently shaped buttons/button like areas as well. Using TouchPhase.Began
limits the player so they cannot ‘hold’ the button to rapidfire; you can change this by using TouchPhase.Stationary
and some code to limit how often the action occurs. Hope this helps.