- Home /
Making a button in unity iphone?
Ok I know how to make the boring buttons like:
if (GUI.Button(Rect(20,50,10,30),"Click"))
or with texture:
if (GUI.Button(Rect(10,10,50,50),btnTexture))
But how to transform a Gui texture into a button? (without using guiskin) That kind of things really improve the user experience. I know it is simple but how?
Answer by Bunny83 · Feb 01, 2011 at 09:27 PM
...boring buttons...
I just ignored that... :D
Well you want to use a GUTTexture. Just place a script on that gameobject that contains the GUITexture and use something like that:
Doesn't work on iPhone:
// Javascript
function OnMouseDown() {
// I just get clicked!
}
function Update(){
if (Input.touchCount>0)
{
foreach(Touch T in Input.touches)
{
if(T.phase = TouchPhase.Began && guiTexture.HitTest(T.position))
{
// Just touched the GUITexture
}
}
}
}
You don't need to use TouchPhase.Began, you can also use TouchPhase.Ended
But mouseDown isnt only for pc? I need the button to detect the touch.
ahh, my bad, just hopped over the "iphone" part :D. You're right On$$anonymous$$ouseDown don't get fired on iPhone. Well, then you have to check it yourself with HitTest(). I'll edit my post
Answer by Noah-1 · Feb 26, 2011 at 10:03 PM
Hey guys I found a simple and cool way on how to create a button,I have seen this question like 6 or 7 times and there wasnt a correct answer anywhere.
Finally the the answer:
var recoveryTime = 10;
private var delay = 0;
function Update () { if (delay>0){delay -=1;}
if (delay == 0){ if (iPhoneInput.touchCount ==1){
var currentTouch:iPhoneTouch = iPhoneInput.touches[0];
if(currentTouch.phase ==
iPhoneTouchPhase.Began && guiTexture.HitTest(currentTouch.position)){ Application.LoadLevel ("FirstLevel");
}
}
} }
I hope it helps guys, just add the code to the GUI texture.
If this helps you, dont forget to rate....:)
@stety2: Have you read beep's answer? You just have to check all touches. Input.touches is an array.
Sorry but I do not get it, can you please explain it to me? And show me some example please.
Answer by beep · Apr 13, 2011 at 06:52 PM
Try this:
function Update () {
var fingerCount = 0;
for (var touch : Touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled && guiTexture.HitTest(touch.position))
fingerCount++;
}
if (fingerCount > 0) {
// your action goes here....
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613102309im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make a Button out of a Textured Plane 2 Answers
Buttons and images 1 Answer
GUI Button Texture only showing as small 1 Answer
Detect Mouse Click OnGUI 1 Answer
GUI buttons look fine on full screen but distorted otherwise? 0 Answers