Version: 2017.2
public static bool Button (Rect position, string text);
public static bool Button (Rect position, Texture image);
public static bool Button (Rect position, GUIContent content);
public static bool Button (Rect position, string text, GUIStyle style);
public static bool Button (Rect position, Texture image, GUIStyle style);
public static bool Button (Rect position, GUIContent content, GUIStyle style);

参数

position 屏幕上用于按钮的矩形。
text 要在按钮上显示的文本。
image 要在按钮上显示的 Texture
content 该按钮的文本、图像和工具提示。
style 要使用的样式。如果省略,则使用当前 GUISkinbutton 样式。

返回

bool 当用户单击该按钮时,返回 /true/。

描述

创建一个单击按钮。当用户点击该按钮时,立即执行一些操作。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Texture btnTexture; void OnGUI() { if (!btnTexture) { Debug.LogError("Please assign a texture on the inspector"); return; } if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture)) Debug.Log("Clicked the button with an image"); if (GUI.Button(new Rect(10, 70, 50, 30), "Click")) Debug.Log("Clicked the button with text"); } }