我想要點擊按鈕時,它會顯示一些GUI按鈕。我已經嘗試了委託,但事件沒有被解僱(我的意思是沒有顯示GUI按鈕)。統一委託事件
這裏是我使用的代碼:
下面的代碼是顯示GUI按鈕,當我運行遊戲。
public class IItemDatabase : MonoBehaviour
{
public delegate void Action(); // Set the event for the button
public static event Action onClicked; // The event for when the button has been clicked
protected virtual void OnGUI()
{
// Call the SetStyle method
SetStyle();
// Set the GUIContent as the tooltip
GUIContent buttonText = new GUIContent("Open Shop");
// Set the GUIContent as the tooltip
GUIContent buttonTexts = new GUIContent("Open Inventory");
// This GUILayoutUtility is useful because it is to fit the content
Rect buttonGUI = GUILayoutUtility.GetRect(buttonText, "Button");
// This GUILayoutUtility is useful because it is to fit the content
Rect buttonGUIs = GUILayoutUtility.GetRect(buttonTexts, "Button");
// Set where have to the Rect displayed
buttonGUI.x = 5;
buttonGUI.y = Screen.height - 25;
// Set where have to the Rect displayed
buttonGUIs.x = 125;
buttonGUIs.y = Screen.height - 25;
// If the button has been clicked
if (GUI.Button(buttonGUI, buttonText, style))
{
if (onClicked != null)
{
onClicked();
}
}
if (GUI.Button(buttonGUIs, buttonTexts, style))
{
if (onClicked != null)
{
onClicked();
}
}
// End of the clicked button event
}
}
這裏是我希望它顯示,當按鈕被點擊:
public class IInventory : MonoBehaviour
{
protected virtual void OnEnable()
{
IItemDatabase.onClicked += DoGUI;
}
protected virtual void OnDisable()
{
IItemDatabase.onClicked -= DoGUI;
}
protected virtual void DoGUI()
{
Rect slotRect = new Rect(x * 35 + (Screen.width/3) + 50, y * 35 + (Screen.height/3) - 10, 30, 30);
GUI.Box(slotRect, GUIContent.none);
}
}
但是,當我按下按鈕它想解僱DoGUI()在IInventory類,它不運行該功能。
我該如何解決這個問題?
謝謝。
您的回答非常感謝!
那麼,爲了解決這個問題,我該怎麼做先生? 對不起,延遲迴復。 謝謝 – Kaoru
@Kaoru我加了一個可能的解決方案的例子 –
解決!謝謝你,先生!我以另一種方式做了它,而不使用委託。謝謝 – Kaoru