Author Topic: Context Menu Show/hide with LeftClick Press/Release  (Read 4215 times)

weltraumaffe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Context Menu Show/hide with LeftClick Press/Release
« on: April 30, 2013, 01:32:48 PM »
Hi,

I'm currently working on a context menu that appears if the player presses a button for a certain amount of time. After the player releases the button the Menu disappears.
It's already working that it shows on click and disappears on release of the mouse button.
However the effects like coloring, resizing and so on don't work.
This is my OnPress function:
  1. void OnPress(bool pressed) {
  2.                 if (pressed) {
  3.                         if (UICamera.currentTouchID == (int)menuButton) {
  4.                                 RaycastHit hit;
  5.                                 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity)) {
  6.  
  7.                                         ShipComponent hitComponenent = hit.transform.GetComponent<ShipComponent>();
  8.  
  9.                                         if (hitComponenent) {
  10.                                                 if (hitComponenent == _selectShipComponent.selectedComponent) {
  11.                                                         EnableCountdownForMenu();
  12.                                                         _menuPos = Input.mousePosition;
  13.                                                 }
  14.                                         }
  15.                                 }
  16.                         }
  17.  
  18.                 } else {
  19.                         DisableCountdownForMenu();
  20.                         DestroyMenu();
  21.                 }
  22.         }
  23.  

And this code is responsible for showing the menu:
  1. NGUITools.SetActive(menu, true);
  2. menu.transform.localPosition = _menuPos;
  3.  
I hide the menu with
  1. NGUITools.SetActive(menu, false);

If I disable the hiding the button works as expected (scaling and coloring...)

The Menu is a simple gameobject with some buttons and a sprite as children
Any idea what I do wrong?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Context Menu Show/hide with LeftClick Press/Release
« Reply #1 on: April 30, 2013, 11:03:42 PM »
You don't need to Raycast. UICamera.lastHit already gives you all the RaycastHit info. Likewise UICamera.lastTouchPosition already gives you the touch position, so you shouldn't be using Input.mousePosition (which only works with the mouse!).

What's 'menu'? Is it a prefab? Is it an instantiated object? Is it an object in the scene?

While holding a mouse button there are no "hover" events, so the highlighting won't work.

weltraumaffe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Context Menu Show/hide with LeftClick Press/Release
« Reply #2 on: May 01, 2013, 03:41:09 AM »
menu is a GameObject that holds all the buttons for the menu.
It's just some buttons..

Thanks for the remarks about lasthit and lastTouchPosition!

A bit bummed that hover doesn't work while mouse down... Is there a way to work around this limitation?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Context Menu Show/hide with LeftClick Press/Release
« Reply #3 on: May 02, 2013, 04:13:00 AM »
If you check UICamera's sticky press option, you will be receiving press events as you move from one button to the next. They're not hover events, but still something.

weltraumaffe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Context Menu Show/hide with LeftClick Press/Release
« Reply #4 on: May 02, 2013, 10:49:34 AM »
Thank you very much! Looks very promising for what we need :)