只要鼠标按钮悬停在该按钮上,我就可以在每个按钮上显示工具提示。但是,按钮功能没有运行,但是显示了工具提示。
以下是代码:
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();
}
}
这是截图(鼠标悬停在“移动”按钮并制作红色工具提示),工具提示显示但是当我点击“移动”按钮时,它将不会运行(不会显示蓝色网格运动):
但是当我删除工具提示文本时,“移动”按钮功能完美运行:
我该如何解决这个问题?感谢
答案 0 :(得分: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();
}
}