2014-05-11 106 views
0

我想要點擊按鈕時,它會顯示一些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類,它不運行該功能。

我該如何解決這個問題?

謝謝。

您的回答非常感謝!

回答

2

您的問題不是事件或代表,而是對OnGUI()工作方式的誤解。

OnGUI()方法可能會每幀調用多次,但您的onClicked事件只會在您單擊按鈕後調用一次。所以您的庫存僅在幾毫秒內可見。

有幾種方法可以解決這個問題。您的目標是在每個OnGUI()呼叫期間撥打DoGUI(),只要您希望顯示廣告資源即可。您可以引入一個布爾值來保存庫存菜單的狀態,並決定菜單是否可見。如果你希望你的Open Inventory按鈕來切換菜單,你可以,如果你想保留的情況下取決於你的應用嘗試類似的東西

public class IItemDatabase : MonoBehaviour 
{ 
    public static event Action onClicked; // The event for when the button has been clicked 

    private bool showInventory = false; 

    protected virtual void OnGUI() 
    { 
     // I removed your other code to simplify the example 

     if (GUI.Button("Open Inventory")) 
     { 
      // toggle the status 
      showInventory = !showInventory 
     } 

     if (showInventory && onClicked != null) 
     { 
      onClicked(); 
     } 

    } 
} 

。如果你保留它,我會將它重命名爲showInventoryGUI或類似的東西,以更好地反映它的意圖。另一方面,您可以直接刪除該事件並直接調用方法IInventory.DoGUI()

+0

那麼,爲了解決這個問題,我該怎麼做先生? 對不起,延遲迴復。 謝謝 – Kaoru

+0

@Kaoru我加了一個可能的解決方案的例子 –

+0

解決!謝謝你,先生!我以另一種方式做了它,而不使用委託。謝謝 – Kaoru