Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: weltraumaffe on April 30, 2013, 01:32:48 PM

Title: Context Menu Show/hide with LeftClick Press/Release
Post by: weltraumaffe 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?
Title: Re: Context Menu Show/hide with LeftClick Press/Release
Post by: ArenMook 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.
Title: Re: Context Menu Show/hide with LeftClick Press/Release
Post by: weltraumaffe 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?
Title: Re: Context Menu Show/hide with LeftClick Press/Release
Post by: ArenMook 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.
Title: Re: Context Menu Show/hide with LeftClick Press/Release
Post by: weltraumaffe on May 02, 2013, 10:49:34 AM
Thank you very much! Looks very promising for what we need :)