首页 > 代码库 > C#编写Unity基础GUI之按钮控件

C#编写Unity基础GUI之按钮控件

基本方法:

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

使用示例:

1.

public static bool Button(Rect position, string text);

code-

1 void OnGUI(){2         if (GUI.Button(new Rect(10, 10, 100, 50),"Button"))3         {4             print("用户单击了按钮");5         }6     }

2.

public static bool Button(Rect position, Texture image);

code-

1 public Texture btnTexture;2     void OnGUI(){3         if (GUI.Button(new Rect(10, 10, 512, 256),btnTexture))4         {5             print("用户单击了按钮");6         }7     }

 

3.

public static bool Button(Rect position, GUIContent content);

code-

1 public Texture btnTexture;2     void OnGUI(){3         if (GUI.Button(new Rect(10, 10, 512, 256),new GUIContent("Button",btnTexture)))4         {5             print("用户单击了按钮");6         }7     }

 

4.tooltip

1 void OnGUI(){2         if (GUI.Button(new Rect(10, 10, 120, 100),new GUIContent("Button","这是一个按钮")))3         {4             print("用户单击了按钮");5         }6         GUI.Label(new Rect(10,120,100,20),GUI.tooltip);7     }

 

GUIStyle需要事先定义和导入,暂时没做就不写了;

C#编写Unity基础GUI之按钮控件