Search Unity

Detecting touch on a guiTexture

Discussion in 'iOS and tvOS' started by gevarre, Sep 18, 2009.

  1. gevarre

    gevarre

    Joined:
    Jan 19, 2009
    Posts:
    132
    Can anyone help with a code snippet on how to detect touching a guiTexture?

    I've gone through the Penelope example, and while there is a lot of good stuff in there on moving a guiTexture around for a joystick, it's so complex that I can't reverse engineer and extract the bit I need to just detect when it's touched.

    One thought I had: since I'm just making a button that never moves, should I actually be trying to detect hitting the button itself, or do I really just want to detect when I'm touching the screen space occupied by the button? I may be over thinking it, but in any case, an actual script example would go a long way.

    Thanks
     
  2. HanulTech

    HanulTech

    Joined:
    Apr 5, 2009
    Posts:
    312
    To detect a hit on a GUITexture, you need to use the HitTest method on the object as follows:

    In your loop over all the touches (see the examples), add the following:

    (variable declarations ommitted)

    touchObj = iPhoneInput.GetTouch(touchCounter);

    if (touchObj.phase == iPhoneTouchPhase.Began) {

    if (myGuiTextObject.HitTest(touchObj.position, touchCamera)) {

    // Do something with the touch event.

    }
    }

    touchCamera is either a reference to the main camera or a designated one if you are using multiple cameras. The HitTest method returns true if the GUITexture has been touched.
     
  3. gevarre

    gevarre

    Joined:
    Jan 19, 2009
    Posts:
    132
    Sweet. That was exactly what I needed. Thanks:)
     
  4. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,397
    damn my method used the x/y coords and the height/width to make a bounding box...

    your way looks much cleaner...


    scripting fail :)
     
  5. HanulTech

    HanulTech

    Joined:
    Apr 5, 2009
    Posts:
    312
    Glad it helped. I can't take credit for it. I stole if from someone else here many months ago (probably Eric) :D
     
  6. fallingbrickwork

    fallingbrickwork

    Joined:
    Mar 16, 2009
    Posts:
    1,072
    I too have been using x/y and width/height.

    Was great seeing this new method, far cleaner indeed.

    Many thanks,
    matt.
     
  7. spacefrog

    spacefrog

    Joined:
    Jun 14, 2009
    Posts:
    734
    I use this (c#)
    (looks to me to be the least cpu-cycle eating one)

    Code (csharp):
    1.  
    2. touchObj = iPhoneInput.GetTouch(touchCounter);
    3.  
    4. if (touchObj.phase == iPhoneTouchPhase.Began)
    5. {
    6.     if( myguiTexture.GetScreenRect().Contains( touchObj.position) )
    7.     {
    8.         // DoSomething
    9.     }
    10. }
     
  8. maxfax2009

    maxfax2009

    Joined:
    Feb 4, 2009
    Posts:
    410
    Hi

    Code (csharp):
    1.  
    2. (variable declarations ommitted)
    3.  
    4. touchObj = iPhoneInput.GetTouch(touchCounter);
    5.  
    6. if (touchObj.phase == iPhoneTouchPhase.Began) {
    7.  
    8. if (myGuiTextObject.HitTest(touchObj.position, touchCamera)) {
    9.  
    10. // Do something with the touch event.
    11.  
    12. }
    13. }
    14.  

    I'm trying to add a "Fire" button to the Joystick.js from Penelope

    But I keep getting a "HitTest" is not a member of iPhoneTouch
     
  9. HanulTech

    HanulTech

    Joined:
    Apr 5, 2009
    Posts:
    312
    HitTest is a method on a GUITexture object. Not a touch object. Take a look at the code snippet again and you will see this.
     
  10. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    For we non-programmer types, can you clarify what you mean?

    I've been trying to get this Javascript to work, but not sure what to add/remove.

    Cheers :oops:
     
  11. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The GUITexture and GUIText classes have the HitTest method, but the iPhoneTouch class doesn't. You just call the HitTest method on a GUIText/GUITexture object with a point on the screen to see if it "hits".
     
  12. Jfhutchi

    Jfhutchi

    Joined:
    Jan 5, 2010
    Posts:
    32
    does anyone here have any examples of how to convert the EVAC-City Tutorial to unity Iphone. I pretty stumped.. I went through the Penelope tutorial and to be honest I still can't get a simple script that gets that I touched a GUITexture on a main menu to do well anything.
     
  13. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    This is what I use (and variations of depending on situations) to detect touch on a GUITexture. Stick it in a script and attach the script to the GUITexture

    Code (csharp):
    1.  
    2. function Update() {
    3.    
    4.    
    5.     if(iPhoneInput.touchCount > 0)
    6.     {
    7.         var touch: iPhoneTouch = iPhoneInput.touches[0];
    8.  
    9.         if(touch.phase == iPhoneTouchPhase.Began  guiTexture.HitTest(touch.position))
    10.        
    11.         {
    12.                          //Do really amazing stuff here
    13.          }
    14.     }
    15. }
    16.  
    17.  
     
  14. markus_santoso

    markus_santoso

    Joined:
    May 24, 2011
    Posts:
    2
    Dear Guys,

    To be honest I am new for Unity3D.
    I try to make some touch screen function using the script given by groovfruit, then I add some action that is Application.LoadLevel(...);
    the Final script like this:
    function Update() {

    if(iPhoneInput.touchCount > 0)
    {
    var touch: iPhoneTouch = iPhoneInput.touches[0];

    if(touch.phase == iPhoneTouchPhase.Began guiTexture.HitTest(touch.position))
    {
    Application.LoadLevel(4); //Do really amazing stuff here
    }
    }
    }

    I use this script in GUI Texture to be act as a Button. The problem was come up when there were two GUI Texture in the same place, using this script seems like it touched twice even though I just touch it once. Anybody can help me? Thanks before
     
  15. zhapness

    zhapness

    Joined:
    Apr 10, 2011
    Posts:
    58
    I did another HitTest on the area that was not covered by another gui element.

     
    Last edited: Oct 4, 2011
  16. Krimlin

    Krimlin

    Joined:
    Oct 12, 2011
    Posts:
    1
    I don't understand, when i do this i got an error "Update() can not be a coroutine"
    What's it ? And how can i solve it ?
     
  17. LukaKotar

    LukaKotar

    Joined:
    Sep 25, 2011
    Posts:
    394
    Can you please give us the variable? Thanks! :D

     
  18. gamefreak77777

    gamefreak77777

    Joined:
    Feb 12, 2012
    Posts:
    2
    everything else seems to be ok but can't get my way through this touchcounter variable it will be realy appreciable if u can tell that how you defined it.
     
  19. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    What I just learned yesterday and what Im happy to share is:

    1) Create a GUITexture
    2) add the script below to the GUITexture transform or gameObject

    Im not 100% but I believe the for loop is for multiple touches which iterates through the count (Input.touchCount) so that you can be touching something else on the screen and still be able to use the button :) The Really nice thing I like about this is that I can move the GUITexture around in the X and Y on screen and it still works in different positions and scales nice with different resolutions.

    Code (csharp):
    1. function Update()
    2. {
    3.     var count : int = Input.touchCount;
    4.    
    5.     for(var i: int = 0;i < count; i++)//for multi touch
    6.     {
    7.         var touch : Touch = Input.GetTouch(i);
    8.    
    9.         if(guiTexture.HitTest(touch.position)  touch.phase == TouchPhase.Began)
    10.         {
    11.             print("Red Button Clicked or EVEN BETTER AWESOME STUFF!");
    12.         }
    13.     }
    14. }
     
  20. gamefreak77777

    gamefreak77777

    Joined:
    Feb 12, 2012
    Posts:
    2
    i was trying to detect whether my touch is inside or outside my joystick GUI texture and finallly this code worked for me.(just attach the script to the texture)

    function Update()
    {
    if (Input.touchCount > 0 )
    {
    var touchPosition:Vector2 = Input.GetTouch(0).position;
    if (guiTexture.HitTest(touchPosition) )
    {print("in");}
    else
    {print("out");}
    }
    }
     
  21. seansteezy

    seansteezy

    Joined:
    Nov 28, 2013
    Posts:
    122
    THANK YOU! After a day of searching I think this is the answer I was looking for. Missing the HitTest part the whole time.... derp.
     
  22. TheStarbox

    TheStarbox

    Joined:
    Sep 19, 2014
    Posts:
    1
    thank you soo for your answer :D I LOVE UNİTY :D