Author Topic: Differentiating between Widget click, object click, and terrain click  (Read 11472 times)

smartman

  • Guest
Re: Differentiating between Widget click, object click, and terrain click
« Reply #15 on: July 15, 2012, 03:40:07 PM »
I guess I'm still confused.
Intially, I was trying to have one catch-all script, but as per Arek's suggestion, I moved it to scripts attached to each object/collider.

Yes, for the case of OnSelect(true) I can look at the object and know what was clicked - but in my case, I need to be able to know who was clicked in the case of OnSelect(false)

I say this because the scenario is this - I click a tower, I get an NGUI Button. I now click the button. What happens? An OnSelect(true) fires for the button, and an OnSelect(false) fire for the tower, which is great. BUT, in the OnSelect(false) for the tower, I need to ask - "Who was clicked? If it was not a button, then I want to go do action x (where x is remove the panel I drew previously)".

That's the part I'm stuck at. If there was a guarantee of the order the events fire (such as all OnSelect(false) fire after OnSelect(true)) then I could manage who was selected, but I don't think thats the case.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Differentiating between Widget click, object click, and terrain click
« Reply #16 on: July 15, 2012, 04:10:55 PM »
OnSelect(false) will always fire on the current object before the next object's OnSelect(true).

UICamera.lastHit.transform tells you the next object.

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Differentiating between Widget click, object click, and terrain click
« Reply #17 on: July 15, 2012, 04:50:14 PM »
Quote
I need to ask - "Who was clicked? If it was not a button, then I want to go do action x (where x is remove the panel I drew previously)".

void OnSelect(bool selected)
{
  if (selected)
  {
    // if you have reached this location, it is because the gameobject that this script is attached to has
    // be sent the OnSelect(true) event by a UICamera where the mouse has clicked, a ray was cast, and
    // the collider for the object was the first intercept.  So now you can do something like...

    SomeClass.SomeGlobalGameObjectVariable = this.gameObject;
    SomeClass.SomeGlobalMethod(eventFlagOrSomething);

  }
}



Depends upon how you're managing the focus/selection of game objects, which is entirely up to your own personal coding style and logic.

smartman

  • Guest
Re: Differentiating between Widget click, object click, and terrain click
« Reply #18 on: July 15, 2012, 11:51:38 PM »
Thanks for all the help. A combination of what I previously mentioned and using UICamera.lastHit.transform as Aren mentioned allowed me to get it working. Hopefully this thread is useful for others in the future!