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
Attach a script to the 2D element which has the function OnMouseUpAsButton(). This function will fire when the element is "clicked".
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): public class HomeScene : MonoBehaviour { public Texture2D textureStart = null; public Texture2D textureExit = null; void OnGUI() { if (GUI.Button(new Rect(w, h, tw, th),textureStart,GUIStyle.none)) { Application.LoadLevelAsync("Start"); } else if (GUI.Button(new Rect(w, h+space, tw, th),textureExit,GUIStyle.none)) { Application.Quit(); } } }
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
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 ?
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.
In case it can be of some use, here is my code (attached to every button with a collider (2D or 3D)) : Code (csharp): using UnityEngine; using System.Collections; public class ButtonEvent : MonoBehaviour { public Color colorOver = new Color(1f,0.88f,0.55f); public Color colorPushed = new Color(0.66f,0.66f,0.48f); public string level; //If empty quit the application private Color originalColor; void Start(){ originalColor = gameObject.renderer.material.color; } void OnMouseEnter() { gameObject.renderer.material.color= colorOver; } void OnMouseExit() { gameObject.renderer.material.color= originalColor; } void OnMouseDown() { gameObject.renderer.material.color= colorPushed; } void OnMouseUpAsButton() { if(level.Length>0) Application.LoadLevel(level); else Application.Quit(); } }
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.
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.
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!