Search Unity

How to use that script with a on Click (on active) ?

Discussion in 'Scripting' started by BooBi, Jan 24, 2011.

  1. BooBi

    BooBi

    Joined:
    Jan 18, 2010
    Posts:
    534
    Hi,

    I would like to know if it's possible to modify that script to use it with an on click.


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class example : MonoBehaviour {
    5. public string lastTooltip = " ";
    6. void OnGUI() {
    7. GUILayout.Button(new GUIContent("Play Game", "Button1"));
    8. GUILayout.Button(new GUIContent("Quit", "Button2"));
    9. if (Event.current.type == EventType.Repaint  GUI.tooltip != lastTooltip) {
    10. if (lastTooltip != "")
    11. SendMessage(lastTooltip + "OnMouseOut", SendMessageOptions.DontRequireReceiver);
    12.  
    13. if (GUI.tooltip != "")
    14. SendMessage(GUI.tooltip + "OnMouseOver", SendMessageOptions.DontRequireReceiver);
    15.  
    16. lastTooltip = GUI.tooltip;
    17. }
    18. }
    19. void Button1OnMouseOver() {
    20. Debug.Log("Play game got focus");
    21. }
    22. void Button2OnMouseOut() {
    23. Debug.Log("Quit lost focus");
    24. }
    25. }
     
  2. Kemical

    Kemical

    Joined:
    Oct 4, 2010
    Posts:
    24
    to do something on click you just have to do :

    Code (csharp):
    1.  
    2. if ( GUILayout.Button(new GUIContent("Play Game", "Button1") )
    3. {
    4. Debug.Log("Play has been clicked");
    5. }
    6.  
    Click is handle by unity, so you don't have to use tricks needed for rollover/rollout ;)
     
  3. BooBi

    BooBi

    Joined:
    Jan 18, 2010
    Posts:
    534
    lol i really need to get some sleep, sorry about that....