Search Unity

Simple touch events on GUIText/GUITexture?

Discussion in 'Scripting' started by Alienchild, Apr 17, 2012.

  1. Alienchild

    Alienchild

    Joined:
    Oct 14, 2010
    Posts:
    364
    Hi!

    I would like to know what the simplest method of checking if a GUIText/GUITexture object has been touched is. I use these for menu objects and in-game GUI simply using OnMouseDown and it would be a rather large job to redesign everything if I have to check and execute all functions from one single script (which I gather you have to do when using hitTest?). Also, I would like to not have to input the coordinates of the object, as that can vary.

    Anyone know of a simple and easy to use solution? The best would of course be a OnMouseDown-like event registered on the GUIText/GUITexture-components.
     
  2. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    Hmmmm... I haven't really worked with guis that much within Unity, but I'll give it a shot. What if you created an interface that exposes some method, for example : OnClick(object sender) and create a scipt or set of scripts to implement it. Then you attach each script to the GUI elements that require input. In your main code, where you do your raycasting, you check to see if the hit.target contains a component for the interface, and if it does you call its OnClick method. Or, if you don't want to assign a script to each GUI element that requires input, then you assign each of them an ID or tag, and based on that you can exectute the appropriate functions. The first options sounds more up your alley though since the second one is monolithic.
     
  3. Alienchild

    Alienchild

    Joined:
    Oct 14, 2010
    Posts:
    364
    Thank you. The entire script to do exactly what you described for both 3d objects and GUI objects alike are now as follows;

    Code (csharp):
    1.  
    2. /*  Written by Thomas S. Berge
    3.     Emblem Entertainment Pty Ltd
    4.     www.emblementertainment.com.au
    5.    
    6.     Feel free to use it in any way you like, commercial or otherwise.
    7. */ 
    8.  
    9. #pragma strict
    10.  
    11. private var test : GUILayer;    
    12. test = Camera.main.GetComponent(GUILayer);
    13.  
    14. function Update ()
    15. {
    16.     for (var i = 0; i < Input.touchCount; ++i)
    17.     {
    18.         if (Input.GetTouch(i).phase == TouchPhase.Began)
    19.         {
    20.  
    21.             var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
    22.             var hit : RaycastHit;
    23.            
    24.             if (Physics.Raycast (ray,hit))
    25.             {
    26.                 hit.transform.gameObject.SendMessage ("OnTouch");
    27.             }
    28.         }
    29.     }
    30.  
    31.     if(test.HitTest(Input.mousePosition) != null)
    32.     {
    33.         test.HitTest(Input.mousePosition).gameObject.SendMessage("OnTouch");
    34.     }
    35. }
    36.  
     
  4. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    Good job; some optimization tips would be to put all your input gui controls into a layer that is masked for raycasting, and another would be to cast to the interface and call it yourself rather than use SendMessage.