- Home /
Making a GUI Texture tappable on iPhone
Disclaimer: I've searched everywhere for this, including the Unity documentation.
In the Unity simulator, this works fine if I add a script like this to my GUITexture in the hierarchy:
function OnMouseUp (){
//do stuff
}
However when run on the iPhone, this doesn't have any effect. After a bit of searching I tried:
if(Input.touchCount>0){
//do stuff
}
This doesn't appear to do anything in the simulator or on the handset.
One thing that does work fine on the iPhone is building a GUIButton like this:
function OnGUI() {
if(GUI.Button(Rect(0,400,320,44),"I'm a code generated button!")) {
//do stuff
}
}
However this is code for dynamically drawing a button, not code for placing on a GUITexture game object.
Any clues? :(
Answer by hawken-2 · Jul 06, 2011 at 09:58 AM
Actually, I fixed this by looking at some Android questions!!
Here's the ballache answer (you need to do this per button... ):
function Update(){
if (Input.touchCount == 1)
{
var touch: Touch = Input.touches[0];
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
{
// do amazing things
}
}
}
good stuff works great just what i was looking for simple code for a simple question which no can answer and come up with some complicated method which takes up more speed or better know as lag, some guy actually wanted to assign each one to adifferent layer
Answer by Annihilator · Apr 05, 2012 at 12:39 PM
,how do you use this with multiple touches?
Lets say you have 2 GUITextures and you want each of them to have independent touches, how do you modify the code so that you
Answer by Annihilator · Apr 05, 2012 at 12:39 PM
how do you use this with multiple touches?
Lets say you have 2 GUITextures and you want each of them to have independent touches, how do you modify the code so that you if you touch the 1 texture if activates, touching the other activates that one aswell and releasing either of them deactivates the one released... Same works if they are touched independently
i actually from the code above modded it to act perfect with multiple touches, so say in my game i can hold the acceleration and turn.
basically all you have to do is check each touch in a loop{
and in this write the code, i can post a snipit of the code later if you need it but i have to go now so i havent got time
}
if (Input.touchCount>0) {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) {
}
}
}
Answer by Orthomyxovirus · Sep 11, 2012 at 04:59 AM
I have some problem, i want do something like this:
When i tap guitexture it change from texture1 to texture2, so i combine two scripts this for tap and this:
var texture1 : GUITexture;
var texture2 : GUITexture;
var someTexture : GUITexture;
someTexture = texture1;
guiTexture.texture = texture1.texture;
function Update () {
}
function OnGUI() {
if(GUI.Button(Rect(0,0,100,40),"Cambia fondo")){
if(someTexture == texture1) {
someTexture = texture2;
} else if (someTexture == texture2) {
someTexture = texture1;
}
}
}
for change GUITexture
but first what i do is check this script, it work but GUITexture are not change on screen in inspector i see changes texture1 to texture2
I want do something like this:
var texture1 : GUITexture;
var texture2 : GUITexture;
var someTexture : GUITexture;
someTexture = texture1;
guiTexture.texture = texture1.texture;
function Update(){
if (Input.touchCount == 1)
{
var touch: Touch = Input.touches[0];
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
{
if(someTexture == texture1) {
someTexture = texture2;
} else if (someTexture == texture2) {
someTexture = texture1;
}
}
}
}
I search 2h for something and i cant find anything, so… somebody can help me?