Long story short, I'm trying to create a UI panel where you hold and drag your mouse wheel button and select the option you want (while holding that button). So it is a pop-up menu that is triggered when you press the mouse wheel button. When you release the button, there are 2 possible situations:
- You didn't move your cursor to a valid position. The pop-up menu is closed.
- You moved your cursor to a valid position. You triggered an option from this pop-up menu.
To give you an example, the weapon switch system in, say, Tomb Raider does this similarly. It looks like a roulette, you hold the button then move your cursor to a certain location that "belongs" to, say, shotgun. Then you release the button, the menu is closed and now you are equiped with a shotgun. Right now, the pop-up panel kinda works. However it's not a hold-release mechanism but a click mechanism. You click the button once, then the menu pops up and stays there.
How do you do that in Unity3D?
Here is my script so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PosSelector : MonoBehaviour
{
Animator posSelectorAnimator;
public GameObject posSelector;
public Animator posButtonAnimator;
void Start ()
{
posSelectorAnimator = posSelector.GetComponent<Animator>();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Mouse2))
{
Open();
}
if (Input.GetKeyUp(KeyCode.Mouse2))
{
Close();
}
}
void Open()
{
Vector3 mousePos = Input.mousePosition;
Debug.Log("Middle button is pressed.");
posSelector.transform.position = (mousePos);
posSelectorAnimator.SetBool("ButtonDown", true);
}
void Close()
{
if (posButtonAnimator.GetCurrentAnimatorStateInfo(0).IsName("Highlighted"))
{
Debug.Log("Position selected.");
Debug.Log(posButtonAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash);
}
else
{
Debug.Log("Input not found.");
Debug.Log(posButtonAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash);
}
}
}
CodePudding user response:
First of all, what you are cresting is called a pie menu. You could create a script for the buttons inside your pie menu. Then let that script inherit from MonoBehaviour, IPointerEnterHandler and IPointerExitHandler. These interfaces force you to implement the following methods:
public void OnPointerEnter(PointerEventData eventData)
{
PosSelector.selectedObject = gameObject;
}
public void OnPointerExit(PointerEventData eventData)
{
PosSelector.selectedObject = null;
}
Now create a static field in your PosSelector script called selectedObject:
public static GameObject selectedObject;
Then in your Close() Method, you can use selectedObject as your output:
void Close()
{
//Close your menu here using the animator
//Return if nothing was selected
if(selectedObject == null)
return;
//Select a weapon or whatever you want to do with your output
}
Also, consider renaming your question to something like "how to create a pie menu in unity? " so that other people with the same question have an easier time finding this question