2013-11-23 51 views
2

只要鼠標按鈕懸停在該按鈕上,我已經可以顯示每個按鈕上的工具提示。但是,按鈕功能沒有運行,但顯示了工具提示。顯示工具提示使按鈕功能不會運行c#

下面是代碼:

public static float textHeight = 25; 
    public static float textWidth = 1350; 
    public static float buttonHeight = 25; 
    public static float buttonWidth = 100; 
    public static float screenTextHeight = 745; 

public virtual void TurnOnGUI() 
    { 
     Rect buttonRect = new Rect(0, Screen.height - buttonHeight * 3, buttonWidth, buttonHeight); 
     Rect tooltipRect = new Rect(100 + 10, Screen.height - buttonHeight * 3 + 5, buttonWidth, buttonHeight); 
     Rect textRect = new Rect(10, Screen.height - screenTextHeight, textWidth, textHeight); 
     Rect _buttonRect = new Rect(0, Screen.height - buttonHeight * 2, buttonWidth, buttonHeight); 
     Rect _buttonRect_ = new Rect(0, Screen.height - buttonHeight * 1, buttonWidth, buttonHeight); 
     Rect _tooltipRect = new Rect(100 + 10, Screen.height - buttonHeight * 2 + 5, buttonWidth, buttonHeight); 
     Rect _tooltipRect_ = new Rect(100 + 10, Screen.height - buttonHeight * 1 + 5, buttonWidth, buttonHeight); 

     label1.normal.textColor = Color.red; 

     //Text Field 
     if (GUI.Button(textRect, "")) 
     { 

     } 

     //ToolTip Text 
     //GUI.Button(buttonRect, new GUIContent("Move", "Move the Player")); 
     //GUI.Label(tooltipRect, GUI.tooltip, label1); 

     GUI.tooltip = null; 
     //Move Button 
     if (GUI.Button(buttonRect, "Move")) 
     { 
      if (!moving) 
      { 
       GameManager.instance.RemoveTileHighlights(); 
       moving = true; 
       attacking = false; 
       GameManager.instance.HighlightTilesAt(gridPosition, Color.blue, movementPerActionPoint); 
      } 

      else 
      { 
       moving = false; 
       attacking = false; 
       GameManager.instance.RemoveTileHighlights(); 
      } 
     } 

     //ToolTip Text 
     //GUI.Button(_buttonRect, new GUIContent("Attack", "Attack the Player")); 
     //GUI.Label(_tooltipRect, GUI.tooltip, label1); 

     GUI.tooltip = null; 
     //Attack Button 
     if (GUI.Button(_buttonRect, "Attack")) 
     { 
      if (!attacking) 
      { 
       GameManager.instance.RemoveTileHighlights(); 
       moving = false; 
       attacking = true; 
       GameManager.instance.HighlightTilesAt(gridPosition, Color.red, attackRange); 
      } 

      else 
      { 
       moving = false; 
       attacking = false; 
       GameManager.instance.RemoveTileHighlights(); 
      } 
     } 

     //ToolTip Text 
     //GUI.Button(_buttonRect_, new GUIContent("End Turn", "End Turn the Player")); 
     //GUI.Label(_tooltipRect_, GUI.tooltip, label1); 

     GUI.tooltip = null; 
     //End Turn Button 
     if (GUI.Button(_buttonRect_, "End Turn")) 
     { 
      GameManager.instance.RemoveTileHighlights(); 
      actionPoints = 2; 
      moving = false; 
      attacking = false; 
      GameManager.instance.NextTurn(); 
     } 
    } 

這裏是,所示的工具提示但是當我點擊「移動」按鈕,它的屏幕截圖(在「移動」按鈕,使紅色工具提示鼠標懸停)將不會運行(不會顯示針對移動的格藍):

enter image description here

但是當我刪除的提示文本,「移動」按鈕功能完美運行:

enter image description here

我該如何解決這個問題?由於

回答

1

一個小例子

using UnityEngine; 
using System.Collections; 

public class Demo : MonoBehaviour { 
    bool clicked = false; 

    void OnGUI() { 
     clicked = GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", "This is the tooltip")); 
     GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip); 
     if(clicked) 
     { 
     //do your stuff after click 
      print(clicked); 
     } 
    } 
} 

public static float textHeight = 25; 
public static float textWidth = 1350; 
public static float buttonHeight = 25; 
public static float buttonWidth = 100; 
public static float screenTextHeight = 745; 

bool move=false,attack=false,endTurn=false; //Added 

public virtual void TurnOnGUI() 
{ 
    Rect buttonRect = new Rect(0, Screen.height - buttonHeight * 3, buttonWidth, buttonHeight); 
    Rect tooltipRect = new Rect(100 + 10, Screen.height - buttonHeight * 3 + 5, buttonWidth, buttonHeight); 
    Rect textRect = new Rect(10, Screen.height - screenTextHeight, textWidth, textHeight); 
    Rect _buttonRect = new Rect(0, Screen.height - buttonHeight * 2, buttonWidth, buttonHeight); 
    Rect _buttonRect_ = new Rect(0, Screen.height - buttonHeight * 1, buttonWidth, buttonHeight); 
    Rect _tooltipRect = new Rect(100 + 10, Screen.height - buttonHeight * 2 + 5, buttonWidth, buttonHeight); 
    Rect _tooltipRect_ = new Rect(100 + 10, Screen.height - buttonHeight * 1 + 5, buttonWidth, buttonHeight); 

    label1.normal.textColor = Color.red; 

    //Text Field 
    if (GUI.Button(textRect, "")) 
    { 

    } 

    //ToolTip Text 
    move = GUI.Button(buttonRect, new GUIContent("Move", "Move the Player")); 
    GUI.Label(tooltipRect, GUI.tooltip, label1); 

    GUI.tooltip = null; 
    //Move Button 
    if (move) //edited 
    { 
     if (!moving) 
     { 
      GameManager.instance.RemoveTileHighlights(); 
      moving = true; 
      attacking = false; 
      GameManager.instance.HighlightTilesAt(gridPosition, Color.blue, movementPerActionPoint); 
     } 

     else 
     { 
      moving = false; 
      attacking = false; 
      GameManager.instance.RemoveTileHighlights(); 
     } 
    } 

    //ToolTip Text 
    attack = GUI.Button(_buttonRect, new GUIContent("Attack", "Attack the Player")); 
    GUI.Label(_tooltipRect, GUI.tooltip, label1); 

    GUI.tooltip = null; 
    //Attack Button 
    if (attack) //edited 
    { 
     if (!attacking) 
     { 
      GameManager.instance.RemoveTileHighlights(); 
      moving = false; 
      attacking = true; 
      GameManager.instance.HighlightTilesAt(gridPosition, Color.red, attackRange); 
     } 

     else 
     { 
      moving = false; 
      attacking = false; 
      GameManager.instance.RemoveTileHighlights(); 
     } 
    } 

    //ToolTip Text 
    endTurn = GUI.Button(_buttonRect_, new GUIContent("End Turn", "End Turn the Player")); 
    GUI.Label(_tooltipRect_, GUI.tooltip, label1); 

    GUI.tooltip = null; 
    //End Turn Button 
    if (endTurn) //edited 
    { 
     GameManager.instance.RemoveTileHighlights(); 
     actionPoints = 2; 
     moving = false; 
     attacking = false; 
     GameManager.instance.NextTurn(); 
    } 
} 
+0

感謝先生,其現在的工作!乾杯 – Kaoru