长话短说,我正在尝试创建一个UI面板,您可以在其中按住并拖动鼠标轮按钮,并选择您想要的选项(同时按住该按钮)。所以这是一个弹出式菜单,当你按下鼠标轮按钮时会触发它。当您释放按钮时,有两种可能的情况:
举个例子,武器交换系统在,比如说,墓穴掠夺者也是这样做的。它看起来像一个轮盘赌,你拿着按钮,然后移动你的光标到某个“属于”的位置,比如说,猎枪。然后你释放按钮,菜单关闭,现在你装备了一支猎枪。现在,弹出面板还能用。然而,它不是一个保持释放机制,而是一个点击机制。单击按钮一次,菜单就会弹出并停留在那里。
如何在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);
}
}
}发布于 2021-12-31 09:27:46
首先,你正在做的事叫做馅饼菜单。您可以为饼菜单中的按钮创建脚本。然后让该脚本继承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
}同时,考虑将问题重命名为“如何统一创建饼菜单?”这样,其他有同样问题的人就更容易找到这个问题。
https://stackoverflow.com/questions/70540686
复制相似问题