Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Buttons in Unity 4.3

Discussion in '2D' started by Ptitclem, Nov 13, 2013.

  1. Ptitclem

    Ptitclem

    Joined:
    Nov 11, 2013
    Posts:
    6
    Hello,

    Just a quick question :
    With the new 2D workflow in Unity, is there a better way to make buttons than with GUI ?

    I looked for it but found nothing. The only "official" example does not contain any button.

    Thank you for your answers
    --
    Clement
     
  2. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
    Attach a script to the 2D element which has the function OnMouseUpAsButton(). This function will fire when the element is "clicked".
     
  3. Ptitclem

    Ptitclem

    Joined:
    Nov 11, 2013
    Posts:
    6
    Ok thanks, I will test that.
    Does it works well with a touch event (all platforms) ?

    I would really love some OnClick(gameObject) or OnClick_NameOfThe2DElement( ) that would allow every click event of one scene to be handled in one class. But perhaps its already there ?

    The only way I know for buttons is the following :
    Code (csharp):
    1.  
    2. public class HomeScene : MonoBehaviour {   
    3.     public Texture2D textureStart = null;
    4.     public Texture2D textureExit = null;
    5.  
    6.     void OnGUI()
    7.     {
    8.         if (GUI.Button(new Rect(w, h, tw, th),textureStart,GUIStyle.none))
    9.         {
    10.             Application.LoadLevelAsync("Start");    
    11.         }
    12.         else if (GUI.Button(new Rect(w, h+space, tw, th),textureExit,GUIStyle.none))
    13.         {
    14.             Application.Quit();        
    15.         }
    16.     }
    17. }
    18.  
     
  4. QPRocky

    QPRocky

    Joined:
    Oct 17, 2012
    Posts:
    56
  5. Storyteller

    Storyteller

    Joined:
    May 15, 2012
    Posts:
    23
    you can use a BoxCollider, not a 2DBoxCollider, a regular one, under physics, not physics2D and get the events and click handling to work. its kludgy and I do hope the get it implemented shortly
     
  6. Ptitclem

    Ptitclem

    Joined:
    Nov 11, 2013
    Posts:
    6
    Looks great!
    Plus that answer my question : No GUI with the new Unity2D workflow : Unity2D is developed in parallel with Unity GUI, and not integrated right now. It may be integrated in the future ... I bet not before 2014. No information on what feature will be pro and what won't be.

    Can you explain why (as it works well with a 2DBoxCollider)? Performances ?
     
    Last edited: Nov 14, 2013
  7. jonomf

    jonomf

    Joined:
    Nov 7, 2010
    Posts:
    7
    I believe that OnMouseDown, since a few versions ago, now also does fire for touch events. So a sprite with a collider and a script handling OnMouseDown should do the trick.
     
  8. Ptitclem

    Ptitclem

    Joined:
    Nov 11, 2013
    Posts:
    6
    In case it can be of some use, here is my code (attached to every button with a collider (2D or 3D)) :
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ButtonEvent : MonoBehaviour {
    6.    
    7.     public Color colorOver = new Color(1f,0.88f,0.55f);
    8.     public Color colorPushed  = new Color(0.66f,0.66f,0.48f);  
    9.     public string level; //If empty quit the application
    10.  
    11.     private Color originalColor;
    12.  
    13.     void Start(){      
    14.         originalColor = gameObject.renderer.material.color;    
    15.     }
    16.  
    17.     void OnMouseEnter()    
    18.     {
    19.         gameObject.renderer.material.color= colorOver;
    20.     }  
    21.    
    22.     void OnMouseExit()
    23.     {
    24.         gameObject.renderer.material.color= originalColor;
    25.     }
    26.  
    27.     void OnMouseDown()
    28.     {
    29.         gameObject.renderer.material.color= colorPushed;
    30.     }
    31.  
    32.     void OnMouseUpAsButton()       
    33.     {      
    34.         if(level.Length>0)
    35.             Application.LoadLevel(level);
    36.         else
    37.             Application.Quit();
    38.     }
    39. }
    40.  
     
  9. Storyteller

    Storyteller

    Joined:
    May 15, 2012
    Posts:
    23
    I just put your script on a sprite with a Box Collder 2D and it doesnt work.
    BoxCollider2D does not function with raycasts or the mouse events. you must have a traditional collider.

    please, if you can get things to work, with a BoxCollider2D, share how its done, but for me to get those events to work, I have to add a Box Collider (not 2D, traditional)

    further, how would you get Raycast2D to work with mouse positions? the overloads are completely different.
     
    Last edited: Nov 14, 2013
  10. Ptitclem

    Ptitclem

    Joined:
    Nov 11, 2013
    Posts:
    6
    With this exact code it works perfectly well for me with a 2D collider.
    Environment:
    - Unity 4.3
    - 2D project (new project- setup default to 2D)
    - Orthogonal projection
    - sprites
    - 2D collider on my sprite
    - the ButtonEvent script above on this sprite too

    Hope it helps.
     
  11. Storyteller

    Storyteller

    Joined:
    May 15, 2012
    Posts:
    23
    ok I tried this again and it works. Im not sure what the conflict was but for 2 day I could not get this to function. now it does. If I figure out what was keeping it from working, Ill post it. Thanks for the help!
     
unityunity