如何在Unity 3D中创建一个用于切换武器的饼图菜单?
How to create a pie menu in Unity 3D for switching weapons?
长话短说,我正在尝试创建一个 UI 面板,您可以在其中按住并拖动鼠标滚轮按钮和 select 您想要的选项(同时按住该按钮)。所以它是一个当你按下鼠标滚轮按钮时触发的弹出菜单。当你松开按钮时,有两种可能的情况:
- 您没有将光标移动到有效位置。弹出菜单已关闭。
- 您将光标移动到有效位置。您从此弹出菜单中触发了一个选项。
举个例子,古墓丽影中的武器切换系统也是这样做的。它看起来像轮盘赌,您按住按钮然后将光标移动到“属于”猎枪的某个位置。然后你松开按钮,菜单关闭,现在你装备了一把霰弹枪。
现在,弹出式面板可以正常工作。但是,它不是保持释放机制,而是点击机制。您单击一次按钮,然后弹出菜单并停留在那里。
在 Unity3D 中你是怎么做到的?
到目前为止,这是我的脚本:
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);
}
}
}
首先,你顶的是饼状菜单。
您可以为饼图菜单中的按钮创建一个脚本。然后让该脚本继承自 MonoBehaviour、IPointerEnterHandler 和 IPointerExitHandler。
这些接口强制您实现以下方法:
public void OnPointerEnter(PointerEventData eventData)
{
PosSelector.selectedObject = gameObject;
}
public void OnPointerExit(PointerEventData eventData)
{
PosSelector.selectedObject = null;
}
现在在 PosSelector 脚本中创建一个名为 selectedObject 的静态字段:
public static GameObject selectedObject;
然后在您的 Close() 方法中,您可以使用 selectedObject 作为输出:
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
}
另外,考虑将您的问题重命名为“如何在 unity 中创建饼图菜单?”这样其他有相同问题的人更容易找到这个问题
长话短说,我正在尝试创建一个 UI 面板,您可以在其中按住并拖动鼠标滚轮按钮和 select 您想要的选项(同时按住该按钮)。所以它是一个当你按下鼠标滚轮按钮时触发的弹出菜单。当你松开按钮时,有两种可能的情况:
- 您没有将光标移动到有效位置。弹出菜单已关闭。
- 您将光标移动到有效位置。您从此弹出菜单中触发了一个选项。
举个例子,古墓丽影中的武器切换系统也是这样做的。它看起来像轮盘赌,您按住按钮然后将光标移动到“属于”猎枪的某个位置。然后你松开按钮,菜单关闭,现在你装备了一把霰弹枪。 现在,弹出式面板可以正常工作。但是,它不是保持释放机制,而是点击机制。您单击一次按钮,然后弹出菜单并停留在那里。
在 Unity3D 中你是怎么做到的?
到目前为止,这是我的脚本:
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);
}
}
}
首先,你顶的是饼状菜单。 您可以为饼图菜单中的按钮创建一个脚本。然后让该脚本继承自 MonoBehaviour、IPointerEnterHandler 和 IPointerExitHandler。 这些接口强制您实现以下方法:
public void OnPointerEnter(PointerEventData eventData)
{
PosSelector.selectedObject = gameObject;
}
public void OnPointerExit(PointerEventData eventData)
{
PosSelector.selectedObject = null;
}
现在在 PosSelector 脚本中创建一个名为 selectedObject 的静态字段:
public static GameObject selectedObject;
然后在您的 Close() 方法中,您可以使用 selectedObject 作为输出:
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
}
另外,考虑将您的问题重命名为“如何在 unity 中创建饼图菜单?”这样其他有相同问题的人更容易找到这个问题