Button Click Event Processing

 
Post new topic   Reply to topic    Unity Community Index // UnityGUI
View previous topic :: View next topic  
Author Message
ben.dixon



Joined: 10 Sep 2009
Posts: 7

PostPosted: Wed Oct 07, 2009 5:00 pm    Post subject: Button Click Event Processing Reply with quote
I have a table UI that uses a Button class for drawing cells. The Button object is eventually drawn using a GUI.Button call like follows:

GUI.Button(Boundary, Content, Style);

This works if I use the button independently of my table code and I know the code that makes this call is being reached because the button is correctly displayed in my table.

The problem is GUI.Button(...) is always returning false on click. To complicate things I have code that handles row selection which responds to clicks as well. So I'm wondering what sort of issues should I look for to find out why GUI.Button(...) would always return false. If, for example, row selection were receiving a clicked event as well would the event be consumed and not passed on to the individual cell/Button that was pressed?

Thanks,

Ben
Back to top
View user's profile Send private message
HiggyB
Unity Product Evangelist


Joined: 08 Dec 2006
Posts: 5106
Location: San Francisco CA USA

PostPosted: Wed Oct 07, 2009 5:53 pm    Post subject: Re: Button Click Event Processing Reply with quote
First off, welcome to the forums! Very Happy

ben.dixon wrote:
The problem is GUI.Button(...) is always returning false on click.

Can you show us some code that you're using where clicking the button never triggers a positive result? I don't have any general ideas as to why this might be happening and looking at some code may help us sort this out with you.

_________________
Tom Higgins - Product Evangelist at Unity Technologies ApS
http://unity3d.com | http://blogs.unity3d.com

Want to discuss my avatar? Do it here.
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger
ben.dixon



Joined: 10 Sep 2009
Posts: 7

PostPosted: Wed Oct 07, 2009 6:09 pm    Post subject: Reply with quote
I can't post all the code because its spans a bunch of classes but basically we have a UI library that maintains the state of complex UI widgets like a Table. The table ultimately delegates the cell drawing to this method on our Button class:

Code:

      protected override void DrawDelegate() {
          Boolean buttonPressed = GUI.Button(Boundary, Content, Style);
          //Debug.Log("buttonPressed: " + buttonPressed);
         if (buttonPressed) {
            Debug.Log("Checking action listeners");
            if (buttonActionListeners != null) {
               ButtonAction ba = new ButtonAction();
               ba.Type = ButtonAction.TYPE.UpDown;
               ba.Source = this;
               Debug.Log("Notifying listeners of button");
               buttonActionListeners.NotifyListeners(ba);
            }
         }
      }


This method is called and the Button appears as a cell in the Table as it should but clicking it never produces a true result and so the if statement is never satisfied. This same code is used for standalone buttons that aren't part of a table and works fine. My primary question is, is it possible that the event is being consumed as a click on one of the UI elements that make up the table, perhaps its container or something like that? Just to be sure, I disabled row selection and had the same problem.

For now I have this hack around the problem:
Code:

      private Boolean buttonPressHack(){
         if(this.State.Boundary.Contains(Event.current.mousePosition)) {
           if(Event.current.clickCount > 0){
                return true;
           }    
         }   
         
         return false;
      }


And have modified the if statement from the DrawDelegate like so:

Code:

if (buttonPressed || buttonPressHack()) { ... }
Back to top
View user's profile Send private message
pospi



Joined: 22 Oct 2009
Posts: 3

PostPosted: Thu Nov 05, 2009 2:27 am    Post subject: Reply with quote
I am having a very similar problem to this. In my instance, I am making a right-click menu for onscreen objects. The idea is that the button clicks should perform the actions assigned to them, and immediately afterwards I check for a click and close the menu if one occurred.

Without this check, everything runs fine as you would expect. As soon as you put it in, the button will never return true.

I have tried deferring the hiding of the menu until the next frame & renaming the onVisible() method (seeing as it's highlighted in the editor I thought it might conflict). Those were the only wierd things I could see in the code, and they didn't help :(

Code:

using UnityEngine;
using System.Collections;

public class RightClickMenu : MonoBehaviour {
   
   public Texture backgroundTexture;
   
   private ArrayList behaviours = new ArrayList();
   private Vector3 menuPos = new Vector3();
   
   public int MENU_WIDTH = 170;
   public int ITEM_HEIGHT = 28;
   
   public bool visible = false;
   
   //================================================================================================================
   
   public bool isVisible {
      get {
         return visible;
      } set {
         visible = value;
      }
   }
   
   //================================================================================================================

   // Use this for initialization
   void Start () {
      
   }
   
   // Update is called once per frame
   void Update () {
   
   }
   
   void OnGUI () {      
      if (isVisible) {
         return;
      }

      // Make a background box
      GUI.Box(new Rect(menuPos.x, menuPos.y, MENU_WIDTH + ITEM_HEIGHT / 2, behaviours.Count * ITEM_HEIGHT + ITEM_HEIGHT / 2), new GUIContent(backgroundTexture));

      int i = 0;
      foreach (GameBehaviour b in behaviours) {
         bool executeBehaviour = GUI.Button(
            new Rect(menuPos.x + ITEM_HEIGHT / 4, menuPos.y + i * ITEM_HEIGHT + ITEM_HEIGHT / 4, MENU_WIDTH, ITEM_HEIGHT),
            b.actionText
         );

         if (executeBehaviour) {
            Debug.Log("Executing right click menu behaviour: " + b.actionText);
            b.Execute();
         }
         ++i;
      }
      
      // if we have clicked anything at all, the menu closes
      if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) {
         isVisible = false; // if this is commented out, b.Execute() above will be called
      }
   }

   
   //================================================================================================================
   
   public void AddAction(GameBehaviour b) {
      // add to behaviours
      behaviours.Add(b);
      
      isVisible = true;
   }
   
   public void ClearActions() {
      behaviours.Clear();
      
      isVisible = false;
   }
   
   //================================================================================================================
   
   // positions the menu onscreen
   public void Position() {
      menuPos = Input.mousePosition;
      
      float screenHalf = Camera.main.pixelHeight / 2;
      
      menuPos.y = (screenHalf - menuPos.y) + screenHalf;
   }
}
Back to top
View user's profile Send private message Visit poster's website
jonas echterhoff
Unity Developer


Joined: 18 Aug 2005
Posts: 173

PostPosted: Fri Nov 06, 2009 1:09 pm    Post subject: Re: Button Click Event Processing Reply with quote
ben.dixon wrote:
If, for example, row selection were receiving a clicked event as well would the event be consumed and not passed on to the individual cell/Button that was pressed?


Sounds like your events might be consumed by some control. Top help debug this (and find out where the event gets consumed), you can check Event.current.type == EventType.Used in your code.
Back to top
View user's profile Send private message
Post new topic   Reply to topic    Unity Community Index // UnityGUI All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum