[转载]unity 基本函数整理[3_GUI控件+GUILayout界面布局]

本文转载自:https://blog.csdn.net/peanut__love/article/details/43537483

移动


    
    
  1. //this.transform.Translate (Vector3.forward * Time.deltaTime);
  2. //this.transform.Translate (Vector3.right * Time.deltaTime);
  3. //this.transform.Translate (Vector3.up * Time.deltaTime);


旋转


    
    
  1. //this.transform.Rotate (Vector3.up * Time.deltaTime * 5);
  2. //this.transform.Rotate (new Vector3(0,1,0));


贴图显示


    
    
  1. public Texture2D play;
  2. public string info;
  3. // Use this for initialization
  4. void Start () {
  5. info = "hello";
  6. }
  7. void OnGUI(){
  8. GUI.Label ( new Rect( 150, 150, 150, 200),info);
  9. /*
  10. * 连续按钮
  11. if (GUI.RepeatButton (new Rect (50, 100, 150, 200), "I'm Label")) {
  12. //GUI.Label(new Rect(200,200,20,80),"HELLO");
  13. this.transform.Rotate (new Vector3(0,1,0));
  14. }
  15. */
  16. //贴图按钮
  17. if (GUI.Button ( new Rect ( 50, 50, play.width, play.height), play)) {
  18. info = " always enter paly";
  19. }
  20. }

登陆:


    
    
  1. public string username;
  2. public string password;
  3. void OnGUI(){
  4. username = GUI.TextField ( new Rect( 50, 50, 100, 30),username, 15);
  5. password = GUI.PasswordField ( new Rect( 50, 100, 100, 30),password, "*"[ 0], 15);
  6. }


ToggleBar


    
    
  1. public string[] resource = { "第一个", "第二个", "第三个", "第四个"};
  2. public bool s1 = false;
  3. public bool s2 = false;
  4. public int select;
  5. void OnGUI(){
  6. select = GUI.Toolbar ( new Rect( 10, 10,resource.Length * 100, 30), select,resource);
  7. switch( select){
  8. case 0:
  9. s1 = GUI.Toggle( new Rect( 10, 50, 150, 30),s1, "一大个工具栏1");
  10. s2 = GUI.Toggle( new Rect( 10, 100, 150, 30),s2, "一大个工具栏2");
  11. break;
  12. case 1:
  13. s1 = GUI.Toggle( new Rect( 10, 50, 150, 30),s1, "二大个工具栏1");
  14. s2 = GUI.Toggle( new Rect( 10, 100, 150, 30),s2, "二大个工具栏2");
  15. break;
  16. case 2:
  17. s1 = GUI.Toggle( new Rect( 10, 50, 150, 30),s1, "三大个工具栏1");
  18. s2 = GUI.Toggle( new Rect( 10, 100, 150, 30),s2, "三大个工具栏2");
  19. break;
  20. case 3:
  21. s1 = GUI.Toggle( new Rect( 10, 50, 150, 30),s1, "四大个工具栏1");
  22. s2 = GUI.Toggle( new Rect( 10, 100, 150, 30),s2, "四大个工具栏2");
  23. break;
  24. }
  25. }


VerticalSlider(滑动杆):



    
    
  1. public float verticalvalue = 0;
  2. public float horizontalvalue = 0;
  3. void OnGUI(){
  4. horizontalvalue = GUI.HorizontalSlider ( new Rect( 20, 20, 100, 10),horizontalvalue, 100, 0);
  5. verticalvalue = GUI.VerticalSlider ( new Rect( 50, 50, 30, 100),verticalvalue, 0, 100);
  6. GUI.Label ( new Rect( 100, 100, 200, 30),horizontalvalue + " " + verticalvalue);
  7. }

ScrollView视图(游戏的开始菜单说明):


    
    
  1. public Vector2 scrollPosition;
  2. void Start () {
  3. //滚动条中的:滚动按钮相对于整个滚动按钮的位置
  4. scrollPosition.x = 0;
  5. scrollPosition.y = 0;
  6. }
  7. void OnGUI(){
  8. //怎么说呢?或许只有整理过一遍才算是学习过了一遍吧···
  9. //第一个参数:滚动显示视图的范围
  10. //第二个参数:设置滚动条中的滚动按钮相对于滚动条的位置
  11. //第三个参数:整个滚动视图大小
  12. //第四个、第五个参数皆为true时,表示仅当内容超过时才显示滚动条
  13. scrollPosition = GUI.BeginScrollView ( new Rect (Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.8f, Screen.height * 0.8f), scrollPosition,
  14. new Rect ( 0, 0, Screen.width * 0.8f, 300), true, true);
  15. GUI.Label ( new Rect( 100, 40,Screen.width, 30), "test GUI.ScrollPosition test GUI.ScrollPosition test GUI.ScrollPosition test GUI.ScrollPosition ");
  16. GUI.Label ( new Rect( 100, 80,Screen.width, 30), "test GUI.ScrollPosition test GUI.ScrollPosition test GUI.ScrollPosition test GUI.ScrollPosition ");
  17. GUI.EndScrollView ();
  18. }


GUI.DrawTexture
用以绘制游戏界面的背景贴图
注:摆在最前面,显示的时候在最底层



    
    
  1. public Texture2D tx1;
  2. public Texture2D tx2;
  3. public Texture2D background;
  4. void OnGUI(){
  5. GUI.DrawTexture ( new Rect( 0, 0,Screen.width,Screen.height),background);
  6. if(GUI.Button( new Rect( 50, 50,tx1.width,tx1.height),tx1)){
  7. Application.LoadLevel( "level_1");
  8. }
  9. if(GUI.Button( new Rect( 50, 100,tx2.width,tx2.height),tx2)){
  10. Application.LoadLevel( "level_1");
  11. }
  12. }

群组视图(GroupView)
用以设计游戏界面
注:群组内的视图永远以群组的左上角坐标作为自己的相对坐标位置
另外,群组视图仅是将一群图标绑定在一起,它并无外边框,可是窗口有,请看下一条~:


    
    
  1. public Texture2D tx1;
  2. public Texture2D tx2;
  3. void OnGUI(){
  4. GUI.BeginGroup ( new Rect( 10, 50, 200, 400));
  5. GUI.DrawTexture ( new Rect( 0, 0, 50, 50),tx1);
  6. GUI.Label ( new Rect( 0, 50, 50, 50), "one");
  7. GUI.Button ( new Rect( 0, 100, 50, 50), "oneButton");
  8. GUI.EndGroup ();
  9. GUI.BeginGroup ( new Rect( 210, 50, 200, 400));
  10. GUI.DrawTexture ( new Rect( 0, 0, 50, 50),tx2);
  11. GUI.Label ( new Rect( 0, 50, 50, 50), "TWO");
  12. GUI.Button ( new Rect( 0, 100, 50, 50),tx2);
  13. GUI.EndGroup ();
  14. }


GUI.Box:

GUI.Box (new Rect(0,0,150,150),"窗口ID:");

    
    

窗口:


    
    
  1. void OnGUI(){
  2. GUI.Window ( 0, new Rect( 20, 20, 200, 200),oneWindow, "first window");
  3. GUI.Window ( 1, new Rect( 250, 20, 200, 200),oneWindow, "second window");
  4. }
  5. void oneWindow(int windowID){
  6. GUI.Box ( new Rect( 0, 20, 150, 150), "窗口ID:"+windowID);
  7. if (GUI.Button ( new Rect ( 0, 50, 150, 50), "Button")) {
  8. Debug.Log( "窗口ID:"+windowID);
  9. }
  10. }


GUI Skin
Project : Create ->  GUI Skin

GUI,可选择设置项:


    
    
  1. Font: 设置字体
  2. Box: 设置盒子
  3. Button: 设置按钮
  4. Toggle: 设置多选框
  5. Label: 设置标签
  6. Text Field: 设置单行输入框
  7. Text Area: 设置多行输入框
  8. Window: 设置窗口
  9. Horizontal Slider: 设置水平滑动条
  10. Horizontal Slider Thumb: 设置水平滑动条按钮
  11. Vertical Slider: 设置垂直滑动条
  12. Vertical Slider Thumb: 设置垂直滑动条按钮
  13. Horizontal Scrollbar: 设置滚动视图水平滚动条
  14. Horizontal Scrollbar Thumb: 设置滚动视图水平滚动条滑动按钮
  15. Horizontal Scrollbar Left Button: 设置滚动视图水平滚动条左侧按钮
  16. Horizontal Scrollbar Right Button:设置滚动视图水平滑动条右侧按钮
  17. Vertical Scrollbar: 设置滚动视图垂直滑动条
  18. Vertical Scrollbar Thumb: 设置滚动视图垂直滑动条按钮
  19. Vertical Scrollbar Up Button: 设置滚动视图垂直滚动条上侧按钮
  20. Vertical Scrollbar Down Button: 设置滚动视图垂直滚动条下侧按钮
  21. Scroll View: 设置滚动视图
  22. Custom Styles: 设置客户自定义风格
  23. Setting: 其他的一些设置

对于这些设置下的选项有:


    
    
  1. Name:名字
  2. Normal:默认显示颜色和背景
  3. Hover:鼠标滑动经过时的显示颜色和背景
  4. Active:激活状态时的显示颜色和背景
  5. Focused:获得焦点时的现实颜色和背景
  6. On Normal:默认状态
  7. On Hover:停留状态
  8. On Active:激活状态
  9. On Focused:获得焦点状态
  10. Border:边界的设置
  11. Padding:显示的内容与边缘按钮的偏移
  12. Margin:设置整体位置的偏移
  13. Overflow:原有按钮超出原有大小的距离
  14. Font:针对本控件的字体
  15. Image Position:图片位置
  16. Alignment:设置内容方式
  17. World: Wrap:是否换行
  18. Text Clipping:文字剪切方式
  19. Content Offset:内容偏移量
  20. Fixed Width:边缘固定的宽度
  21. Fixed Height:边缘固定的高度
  22. Font Size:字体大小,默认为 0
  23. Font Style:字体风格(加粗,斜体等等)
  24. Stretch Width:是否延伸宽度
  25. Stretch Height:是否延伸高度

好了、最后就是设置自定义风格组件:



    
    
  1. public GUISkin myGUIskin;
  2. string info = "我是要做海贼王的男人!";
  3. void OnGUI(){
  4. GUI.skin = myGUIskin;
  5. if (GUI.Button ( new Rect ( 50, 150, 300, 50), "你好!JAY!", "Custom0")) {
  6. info = "button effect";
  7. }
  8. }


OK、3-1的GUI控件到此为止,就基本结束了,接下来学习GUILayout界面布局:


3-2、GUILayout游戏布局

可以使得游戏更好地运行在不同屏幕分辨率的手机上~
经过试验:


    
    
  1. string info = "Nothig";
  2. void OnGUI(){
  3. if (GUI.Button ( new Rect ( 50, 50, 80, 30), info)) {
  4. info += info;
  5. }
  6. if (GUILayout.Button (info)) {
  7. info += info;
  8. }
  9. }
发现:GUILayout的适应性非常好~


GUILayout提供设置的选项:


    
    
  1. GUILayout.Width() GUILayout布局宽度
  2. GUILayout.Height() GUILayout布局高度
  3. GUILayout.MinWidth() GUILayout布局最小宽度
  4. GUILayout.MinHeight() GUILayout布局最小高度
  5. GUILayout.MaxWidth() GUILayout布局最大宽度
  6. GUILayout.MaxHeight() GUILayout布局最大高度
  7. GUILayout.ExpandWidth() GUILayout布局整体宽度
  8. GUILayout.ExpandHeight()GUILayout布局整体高度


    
    
  1. void OnGUI(){
  2. GUILayout.Button ( "我是要做海贼王的男人",GUILayout.Width( 200),GUILayout.Height( 30));
  3. GUILayout.Button ( "宽度不等于最宽按钮",GUILayout.ExpandWidth( false));
  4. }



线性布局(水平布局or垂直布局):


    
    
  1. void OnGUI(){
  2. GUILayout.BeginHorizontal ();
  3. GUILayout.Button ( "我是要做海贼王的男人!");
  4. GUILayout.Label ( "我是蒙奇.路飞~");
  5. GUILayout.EndHorizontal ();
  6. GUILayout.BeginVertical ();
  7. GUILayout.Button ( "我是要做海贼王的男人!");
  8. GUILayout.Label ( "我是蒙奇.路飞~");
  9. GUILayout.EndVertical ();
  10. }

控件偏移:


    
    
  1. void OnGUI(){
  2. GUILayout.BeginArea ( new Rect( 0, 0, 200, 60));
  3. GUILayout.BeginHorizontal ();
  4. GUILayout.BeginVertical ();
  5. GUILayout.Box ( "Test1");
  6. GUILayout.Space ( 10);
  7. GUILayout.Box ( "Test3");
  8. GUILayout.EndVertical ();
  9. GUILayout.Space ( 20);
  10. GUILayout.BeginVertical ();
  11. GUILayout.Box ( "Test2");
  12. GUILayout.Space ( 10);
  13. GUILayout.Box ( "Test4");
  14. GUILayout.EndVertical ();
  15. GUILayout.EndHorizontal();
  16. GUILayout.EndArea ();
  17. }

对齐方式:
GUILayout.FlexibleSpace();
直接就产生对齐撑开抵满两边的效果:
|##       GUILayout.FlexibleSpace();          ##|




窗口的实现
我勒个去,原来游戏里出来的[确认/否定]是个窗口性质
 



    
    
  1. public ArrayList winArrayList;
  2. public Texture icon;
  3. void Start(){
  4. winArrayList = new ArrayList ();
  5. winArrayList.Add ( new Rect(winArrayList.Count * 250, 50, 100, 100));
  6. }
  7. void OnGUI(){
  8. int count = winArrayList.Count;
  9. for ( int i = 0; i<count; i++) {
  10. //GUILayout.Window(i,(Rect)winArrayList[i],AddWindow,"WindowID: " +i);
  11. winArrayList[i] = GUILayout.Window(i,(Rect)winArrayList[i],AddWindow, "WindowID: " +i);
  12. //winArrayList[i] = GUILayout.Window(i,,AddWindow,"");
  13. }
  14. }
  15. void AddWindow(int WindowID){
  16. GUILayout.BeginHorizontal ();
  17. GUILayout.Label (icon,GUILayout.Width( 50),GUILayout.Height( 50));
  18. GUILayout.Label ( "this is a new window");
  19. GUILayout.EndHorizontal ();
  20. GUILayout.BeginHorizontal ();
  21. if (GUILayout.Button ( "new a window")) {
  22. winArrayList.Add( new Rect(winArrayList.Count * 150, 50, 100, 100));
  23. }
  24. if (GUILayout.Button ( "Remove window")) {
  25. winArrayList.Remove(WindowID);
  26. }
  27. GUILayout.EndHorizontal ();
  28. GUI.DragWindow ( new Rect( 0, 0,Screen.width,Screen.height));
  29. }
———————————


    
    
  1. public ArrayList winArrayList;
  2. public Texture icon;
  3. void Start(){
  4. winArrayList = new ArrayList ();
  5. winArrayList.Add ( new Rect(winArrayList.Count * 250, 50, 100, 100));
  6. }
  7. void OnGUI(){
  8. int count = winArrayList.Count;
  9. for ( int i = 0; i<count; i++) {
  10. //GUILayout.Window(i,(Rect)winArrayList[i],AddWindow,"WindowID: " +i);
  11. winArrayList[i] = GUILayout.Window(i,(Rect)winArrayList[i],AddWindow, "WindowID: " +i);
  12. //winArrayList[i] = GUILayout.Window(i,,AddWindow,"");
  13. }
  14. }
  15. void AddWindow(int WindowID){
  16. GUILayout.BeginHorizontal ();
  17. GUILayout.Label (icon,GUILayout.Width( 50),GUILayout.Height( 50));
  18. GUILayout.Label ( "this is a new window");
  19. GUILayout.EndHorizontal ();
  20. GUILayout.BeginHorizontal ();
  21. if (GUILayout.Button ( "new a window")) {
  22. winArrayList.Add( new Rect(winArrayList.Count * 150, 50, 100, 100));
  23. }
  24. if (GUILayout.Button ( "Remove window")) {
  25. winArrayList.Remove(WindowID);
  26. }
  27. GUILayout.EndHorizontal ();
  28. GUI.DragWindow ( new Rect( 0, 0,Screen.width,Screen.height));
  29. }



设置字体

Project:   Creat->GUI Skin ,设置font


    
    
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Show : MonoBehaviour {
  4. public GUISkin myGUISkin;
  5. void OnGUI(){
  6. GUI.skin = myGUISkin;
  7. GUI.Label ( new Rect( 50, 50, 100, 30), "王的男人!");
  8. }
  9. }

动态加载图片
将图片文件c.jpg放入Resources文件夹中:


    
    
  1. public Texture2D txt;
  2. void OnGUI(){
  3. if(GUI.Button( new Rect( 50, 50, 100, 30), "load texture")){
  4. txt = Resources.Load( "c");
  5. }
  6. }



            </div>

移动


  
  
  1. //this.transform.Translate (Vector3.forward * Time.deltaTime);
  2. //this.transform.Translate (Vector3.right * Time.deltaTime);
  3. //this.transform.Translate (Vector3.up * Time.deltaTime);


旋转


  
  
  1. //this.transform.Rotate (Vector3.up * Time.deltaTime * 5);
  2. //this.transform.Rotate (new Vector3(0,1,0));


贴图显示


  
  
  1. public Texture2D play;
  2. public string info;
  3. // Use this for initialization
  4. void Start () {
  5. info = "hello";
  6. }
  7. void OnGUI(){
  8. GUI.Label ( new Rect( 150, 150, 150, 200),info);
  9. /*
  10. * 连续按钮
  11. if (GUI.RepeatButton (new Rect (50, 100, 150, 200), "I'm Label")) {
  12. //GUI.Label(new Rect(200,200,20,80),"HELLO");
  13. this.transform.Rotate (new Vector3(0,1,0));
  14. }
  15. */
  16. //贴图按钮
  17. if (GUI.Button ( new Rect ( 50, 50, play.width, play.height), play)) {
  18. info = " always enter paly";
  19. }
  20. }

登陆:


  
  
  1. public string username;
  2. public string password;
  3. void OnGUI(){
  4. username = GUI.TextField ( new Rect( 50, 50, 100, 30),username, 15);
  5. password = GUI.PasswordField ( new Rect( 50, 100, 100, 30),password, "*"[ 0], 15);
  6. }


ToggleBar


  
  
  1. public string[] resource = { "第一个", "第二个", "第三个", "第四个"};
  2. public bool s1 = false;
  3. public bool s2 = false;
  4. public int select;
  5. void OnGUI(){
  6. select = GUI.Toolbar ( new Rect( 10, 10,resource.Length * 100, 30), select,resource);
  7. switch( select){
  8. case 0:
  9. s1 = GUI.Toggle( new Rect( 10, 50, 150, 30),s1, "一大个工具栏1");
  10. s2 = GUI.Toggle( new Rect( 10, 100, 150, 30),s2, "一大个工具栏2");
  11. break;
  12. case 1:
  13. s1 = GUI.Toggle( new Rect( 10, 50, 150, 30),s1, "二大个工具栏1");
  14. s2 = GUI.Toggle( new Rect( 10, 100, 150, 30),s2, "二大个工具栏2");
  15. break;
  16. case 2:
  17. s1 = GUI.Toggle( new Rect( 10, 50, 150, 30),s1, "三大个工具栏1");
  18. s2 = GUI.Toggle( new Rect( 10, 100, 150, 30),s2, "三大个工具栏2");
  19. break;
  20. case 3:
  21. s1 = GUI.Toggle( new Rect( 10, 50, 150, 30),s1, "四大个工具栏1");
  22. s2 = GUI.Toggle( new Rect( 10, 100, 150, 30),s2, "四大个工具栏2");
  23. break;
  24. }
  25. }


VerticalSlider(滑动杆):



  
  
  1. public float verticalvalue = 0;
  2. public float horizontalvalue = 0;
  3. void OnGUI(){
  4. horizontalvalue = GUI.HorizontalSlider ( new Rect( 20, 20, 100, 10),horizontalvalue, 100, 0);
  5. verticalvalue = GUI.VerticalSlider ( new Rect( 50, 50, 30, 100),verticalvalue, 0, 100);
  6. GUI.Label ( new Rect( 100, 100, 200, 30),horizontalvalue + " " + verticalvalue);
  7. }

ScrollView视图(游戏的开始菜单说明):


  
  
  1. public Vector2 scrollPosition;
  2. void Start () {
  3. //滚动条中的:滚动按钮相对于整个滚动按钮的位置
  4. scrollPosition.x = 0;
  5. scrollPosition.y = 0;
  6. }
  7. void OnGUI(){
  8. //怎么说呢?或许只有整理过一遍才算是学习过了一遍吧···
  9. //第一个参数:滚动显示视图的范围
  10. //第二个参数:设置滚动条中的滚动按钮相对于滚动条的位置
  11. //第三个参数:整个滚动视图大小
  12. //第四个、第五个参数皆为true时,表示仅当内容超过时才显示滚动条
  13. scrollPosition = GUI.BeginScrollView ( new Rect (Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.8f, Screen.height * 0.8f), scrollPosition,
  14. new Rect ( 0, 0, Screen.width * 0.8f, 300), true, true);
  15. GUI.Label ( new Rect( 100, 40,Screen.width, 30), "test GUI.ScrollPosition test GUI.ScrollPosition test GUI.ScrollPosition test GUI.ScrollPosition ");
  16. GUI.Label ( new Rect( 100, 80,Screen.width, 30), "test GUI.ScrollPosition test GUI.ScrollPosition test GUI.ScrollPosition test GUI.ScrollPosition ");
  17. GUI.EndScrollView ();
  18. }


GUI.DrawTexture
用以绘制游戏界面的背景贴图
注:摆在最前面,显示的时候在最底层



  
  
  1. public Texture2D tx1;
  2. public Texture2D tx2;
  3. public Texture2D background;
  4. void OnGUI(){
  5. GUI.DrawTexture ( new Rect( 0, 0,Screen.width,Screen.height),background);
  6. if(GUI.Button( new Rect( 50, 50,tx1.width,tx1.height),tx1)){
  7. Application.LoadLevel( "level_1");
  8. }
  9. if(GUI.Button( new Rect( 50, 100,tx2.width,tx2.height),tx2)){
  10. Application.LoadLevel( "level_1");
  11. }
  12. }

群组视图(GroupView)
用以设计游戏界面
注:群组内的视图永远以群组的左上角坐标作为自己的相对坐标位置
另外,群组视图仅是将一群图标绑定在一起,它并无外边框,可是窗口有,请看下一条~:


  
  
  1. public Texture2D tx1;
  2. public Texture2D tx2;
  3. void OnGUI(){
  4. GUI.BeginGroup ( new Rect( 10, 50, 200, 400));
  5. GUI.DrawTexture ( new Rect( 0, 0, 50, 50),tx1);
  6. GUI.Label ( new Rect( 0, 50, 50, 50), "one");
  7. GUI.Button ( new Rect( 0, 100, 50, 50), "oneButton");
  8. GUI.EndGroup ();
  9. GUI.BeginGroup ( new Rect( 210, 50, 200, 400));
  10. GUI.DrawTexture ( new Rect( 0, 0, 50, 50),tx2);
  11. GUI.Label ( new Rect( 0, 50, 50, 50), "TWO");
  12. GUI.Button ( new Rect( 0, 100, 50, 50),tx2);
  13. GUI.EndGroup ();
  14. }


GUI.Box:

GUI.Box (new Rect(0,0,150,150),"窗口ID:");

  
  

窗口:


  
  
  1. void OnGUI(){
  2. GUI.Window ( 0, new Rect( 20, 20, 200, 200),oneWindow, "first window");
  3. GUI.Window ( 1, new Rect( 250, 20, 200, 200),oneWindow, "second window");
  4. }
  5. void oneWindow(int windowID){
  6. GUI.Box ( new Rect( 0, 20, 150, 150), "窗口ID:"+windowID);
  7. if (GUI.Button ( new Rect ( 0, 50, 150, 50), "Button")) {
  8. Debug.Log( "窗口ID:"+windowID);
  9. }
  10. }


GUI Skin
Project : Create ->  GUI Skin

GUI,可选择设置项:


  
  
  1. Font: 设置字体
  2. Box: 设置盒子
  3. Button: 设置按钮
  4. Toggle: 设置多选框
  5. Label: 设置标签
  6. Text Field: 设置单行输入框
  7. Text Area: 设置多行输入框
  8. Window: 设置窗口
  9. Horizontal Slider: 设置水平滑动条
  10. Horizontal Slider Thumb: 设置水平滑动条按钮
  11. Vertical Slider: 设置垂直滑动条
  12. Vertical Slider Thumb: 设置垂直滑动条按钮
  13. Horizontal Scrollbar: 设置滚动视图水平滚动条
  14. Horizontal Scrollbar Thumb: 设置滚动视图水平滚动条滑动按钮
  15. Horizontal Scrollbar Left Button: 设置滚动视图水平滚动条左侧按钮
  16. Horizontal Scrollbar Right Button:设置滚动视图水平滑动条右侧按钮
  17. Vertical Scrollbar: 设置滚动视图垂直滑动条
  18. Vertical Scrollbar Thumb: 设置滚动视图垂直滑动条按钮
  19. Vertical Scrollbar Up Button: 设置滚动视图垂直滚动条上侧按钮
  20. Vertical Scrollbar Down Button: 设置滚动视图垂直滚动条下侧按钮
  21. Scroll View: 设置滚动视图
  22. Custom Styles: 设置客户自定义风格
  23. Setting: 其他的一些设置

对于这些设置下的选项有:


  
  
  1. Name:名字
  2. Normal:默认显示颜色和背景
  3. Hover:鼠标滑动经过时的显示颜色和背景
  4. Active:激活状态时的显示颜色和背景
  5. Focused:获得焦点时的现实颜色和背景
  6. On Normal:默认状态
  7. On Hover:停留状态
  8. On Active:激活状态
  9. On Focused:获得焦点状态
  10. Border:边界的设置
  11. Padding:显示的内容与边缘按钮的偏移
  12. Margin:设置整体位置的偏移
  13. Overflow:原有按钮超出原有大小的距离
  14. Font:针对本控件的字体
  15. Image Position:图片位置
  16. Alignment:设置内容方式
  17. World: Wrap:是否换行
  18. Text Clipping:文字剪切方式
  19. Content Offset:内容偏移量
  20. Fixed Width:边缘固定的宽度
  21. Fixed Height:边缘固定的高度
  22. Font Size:字体大小,默认为 0
  23. Font Style:字体风格(加粗,斜体等等)
  24. Stretch Width:是否延伸宽度
  25. Stretch Height:是否延伸高度

好了、最后就是设置自定义风格组件:



  
  
  1. public GUISkin myGUIskin;
  2. string info = "我是要做海贼王的男人!";
  3. void OnGUI(){
  4. GUI.skin = myGUIskin;
  5. if (GUI.Button ( new Rect ( 50, 150, 300, 50), "你好!JAY!", "Custom0")) {
  6. info = "button effect";
  7. }
  8. }


OK、3-1的GUI控件到此为止,就基本结束了,接下来学习GUILayout界面布局:


3-2、GUILayout游戏布局

可以使得游戏更好地运行在不同屏幕分辨率的手机上~
经过试验:


  
  
  1. string info = "Nothig";
  2. void OnGUI(){
  3. if (GUI.Button ( new Rect ( 50, 50, 80, 30), info)) {
  4. info += info;
  5. }
  6. if (GUILayout.Button (info)) {
  7. info += info;
  8. }
  9. }
发现:GUILayout的适应性非常好~


GUILayout提供设置的选项:


  
  
  1. GUILayout.Width() GUILayout布局宽度
  2. GUILayout.Height() GUILayout布局高度
  3. GUILayout.MinWidth() GUILayout布局最小宽度
  4. GUILayout.MinHeight() GUILayout布局最小高度
  5. GUILayout.MaxWidth() GUILayout布局最大宽度
  6. GUILayout.MaxHeight() GUILayout布局最大高度
  7. GUILayout.ExpandWidth() GUILayout布局整体宽度
  8. GUILayout.ExpandHeight()GUILayout布局整体高度


  
  
  1. void OnGUI(){
  2. GUILayout.Button ( "我是要做海贼王的男人",GUILayout.Width( 200),GUILayout.Height( 30));
  3. GUILayout.Button ( "宽度不等于最宽按钮",GUILayout.ExpandWidth( false));
  4. }



线性布局(水平布局or垂直布局):


  
  
  1. void OnGUI(){
  2. GUILayout.BeginHorizontal ();
  3. GUILayout.Button ( "我是要做海贼王的男人!");
  4. GUILayout.Label ( "我是蒙奇.路飞~");
  5. GUILayout.EndHorizontal ();
  6. GUILayout.BeginVertical ();
  7. GUILayout.Button ( "我是要做海贼王的男人!");
  8. GUILayout.Label ( "我是蒙奇.路飞~");
  9. GUILayout.EndVertical ();
  10. }

控件偏移:


  
  
  1. void OnGUI(){
  2. GUILayout.BeginArea ( new Rect( 0, 0, 200, 60));
  3. GUILayout.BeginHorizontal ();
  4. GUILayout.BeginVertical ();
  5. GUILayout.Box ( "Test1");
  6. GUILayout.Space ( 10);
  7. GUILayout.Box ( "Test3");
  8. GUILayout.EndVertical ();
  9. GUILayout.Space ( 20);
  10. GUILayout.BeginVertical ();
  11. GUILayout.Box ( "Test2");
  12. GUILayout.Space ( 10);
  13. GUILayout.Box ( "Test4");
  14. GUILayout.EndVertical ();
  15. GUILayout.EndHorizontal();
  16. GUILayout.EndArea ();
  17. }

对齐方式:
GUILayout.FlexibleSpace();
直接就产生对齐撑开抵满两边的效果:
|##       GUILayout.FlexibleSpace();          ##|




窗口的实现
我勒个去,原来游戏里出来的[确认/否定]是个窗口性质
 



  
  
  1. public ArrayList winArrayList;
  2. public Texture icon;
  3. void Start(){
  4. winArrayList = new ArrayList ();
  5. winArrayList.Add ( new Rect(winArrayList.Count * 250, 50, 100, 100));
  6. }
  7. void OnGUI(){
  8. int count = winArrayList.Count;
  9. for ( int i = 0; i<count; i++) {
  10. //GUILayout.Window(i,(Rect)winArrayList[i],AddWindow,"WindowID: " +i);
  11. winArrayList[i] = GUILayout.Window(i,(Rect)winArrayList[i],AddWindow, "WindowID: " +i);
  12. //winArrayList[i] = GUILayout.Window(i,,AddWindow,"");
  13. }
  14. }
  15. void AddWindow(int WindowID){
  16. GUILayout.BeginHorizontal ();
  17. GUILayout.Label (icon,GUILayout.Width( 50),GUILayout.Height( 50));
  18. GUILayout.Label ( "this is a new window");
  19. GUILayout.EndHorizontal ();
  20. GUILayout.BeginHorizontal ();
  21. if (GUILayout.Button ( "new a window")) {
  22. winArrayList.Add( new Rect(winArrayList.Count * 150, 50, 100, 100));
  23. }
  24. if (GUILayout.Button ( "Remove window")) {
  25. winArrayList.Remove(WindowID);
  26. }
  27. GUILayout.EndHorizontal ();
  28. GUI.DragWindow ( new Rect( 0, 0,Screen.width,Screen.height));
  29. }
———————————


  
  
  1. public ArrayList winArrayList;
  2. public Texture icon;
  3. void Start(){
  4. winArrayList = new ArrayList ();
  5. winArrayList.Add ( new Rect(winArrayList.Count * 250, 50, 100, 100));
  6. }
  7. void OnGUI(){
  8. int count = winArrayList.Count;
  9. for ( int i = 0; i<count; i++) {
  10. //GUILayout.Window(i,(Rect)winArrayList[i],AddWindow,"WindowID: " +i);
  11. winArrayList[i] = GUILayout.Window(i,(Rect)winArrayList[i],AddWindow, "WindowID: " +i);
  12. //winArrayList[i] = GUILayout.Window(i,,AddWindow,"");
  13. }
  14. }
  15. void AddWindow(int WindowID){
  16. GUILayout.BeginHorizontal ();
  17. GUILayout.Label (icon,GUILayout.Width( 50),GUILayout.Height( 50));
  18. GUILayout.Label ( "this is a new window");
  19. GUILayout.EndHorizontal ();
  20. GUILayout.BeginHorizontal ();
  21. if (GUILayout.Button ( "new a window")) {
  22. winArrayList.Add( new Rect(winArrayList.Count * 150, 50, 100, 100));
  23. }
  24. if (GUILayout.Button ( "Remove window")) {
  25. winArrayList.Remove(WindowID);
  26. }
  27. GUILayout.EndHorizontal ();
  28. GUI.DragWindow ( new Rect( 0, 0,Screen.width,Screen.height));
  29. }



设置字体

Project:   Creat->GUI Skin ,设置font


  
  
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Show : MonoBehaviour {
  4. public GUISkin myGUISkin;
  5. void OnGUI(){
  6. GUI.skin = myGUISkin;
  7. GUI.Label ( new Rect( 50, 50, 100, 30), "王的男人!");
  8. }
  9. }

动态加载图片
将图片文件c.jpg放入Resources文件夹中:


  
  
  1. public Texture2D txt;
  2. void OnGUI(){
  3. if(GUI.Button( new Rect( 50, 50, 100, 30), "load texture")){
  4. txt = Resources.Load( "c");
  5. }
  6. }



            </div>

猜你喜欢

转载自blog.csdn.net/qq_16440237/article/details/81179180
今日推荐