長話短說,我正在嘗試創建一個 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);
}
}
}
uj5u.com熱心網友回復:
首先,您要添加的內容稱為餡餅選單。您可以為餅選單中的按鈕創建腳本。然后讓該腳本從 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://www.uj5u.com/caozuo/403252.html
標籤:
上一篇:只有一個事件操作為空
下一篇:如何獲得3點之間的角度?
