常见UI组件的一个模板
效果:
代码:
1 using UnityEngine; 2 using UnityEditor; 3 using System.Collections; 4 using System.IO; 5 using UnityEngine.SceneManagement; 6 7 public class Test: EditorWindow 8 { 9 [MenuItem("地图/测试")] 10 11 static void AddWindow() 12 { 13 //创建窗口 14 Test window = (Test)EditorWindow.GetWindow(typeof(Test), false, "test"); 15 window.Show(); 16 17 } 18 19 enum RGB 20 { 21 red, 22 green, 23 blue 24 } 25 26 RGB rgb; 27 28 private int selectedMethod; 29 bool IsFoldOut; 30 string folderpath; 31 32 bool toggle_1; 33 bool toggle_2; 34 bool toggle_3; 35 36 void OnGUI() 37 { 38 // if (GUILayout.Button("test")) 39 // { 40 // Debug.Log("test"); 41 42 // } 43 //最上分栏的小按钮,系统自带图标名称链接:http://www.xuanyusong.com/archives/3777 44 GUILayout.BeginHorizontal(); 45 GUILayout.FlexibleSpace(); 46 GUIStyle buttonStyle; 47 buttonStyle = new GUIStyle(UnityEditor.EditorStyles.miniButtonLeft); 48 if (selectedMethod == 0) 49 buttonStyle.normal.background = buttonStyle.onActive.background; 50 if (GUILayout.Button(EditorGUIUtility.IconContent("TerrainInspector.TerrainToolRaise"), buttonStyle)) 51 { 52 53 selectedMethod = 0; 54 } 55 buttonStyle = new GUIStyle(UnityEditor.EditorStyles.miniButtonRight); 56 if (selectedMethod == 1) 57 buttonStyle.normal.background = buttonStyle.onActive.background; 58 if (GUILayout.Button(EditorGUIUtility.IconContent("TerrainInspector.TerrainToolSetheight"), buttonStyle)) 59 { 60 61 selectedMethod = 1; 62 } 63 GUILayout.FlexibleSpace(); 64 EditorGUILayout.EndHorizontal(); 65 //说明栏 66 EditorGUILayout.BeginVertical(GUI.skin.box); 67 GUILayout.Label("这是一个说明栏", EditorStyles.wordWrappedLabel); 68 EditorGUILayout.EndVertical(); 69 70 //枚举pop 71 rgb =(RGB) EditorGUILayout.EnumPopup("rgb枚举",rgb); 72 73 //收起控件 74 IsFoldOut = EditorGUILayout.Foldout(IsFoldOut, "展开控件"); 75 if (IsFoldOut) 76 { 77 EditorGUILayout.LabelField(new GUIContent("hello")); 78 } 79 //打开文件夹 80 EditorGUILayout.BeginHorizontal(); 81 GUILayout.Label("打开文件夹:"); 82 folderpath = EditorGUILayout.TextField(folderpath); 83 if (GUILayout.Button("浏览")) 84 { EditorApplication.delayCall += OpenFolder; } 85 EditorGUILayout.EndHorizontal(); 86 87 GUIStyle labStyle = new GUIStyle(UnityEditor.EditorStyles.label); 88 labStyle.fontSize = 15;//设置字体大小 89 labStyle.fontStyle = FontStyle.Bold;//设置粗体 90 labStyle.alignment = TextAnchor.MiddleCenter;//字体垂直居中 91 labStyle.normal.textColor =Color.red;//字体颜色 92 GUILayout.Label(new GUIContent("GUILayout.Label"),labStyle); 93 94 95 GUIStyle btnStyle = new GUIStyle(GUI.skin.GetStyle("Button")); 96 btnStyle.normal.textColor = Color.red;//修改按钮字体颜色 97 //btnStyle.normal.background 98 if (GUILayout.Button("GUILayout.Button", btnStyle)) 99 { 100 101 } 102 103 EditorGUILayout.BeginToggleGroup("勾选框",true); 104 105 EditorGUILayout.EndToggleGroup(); 106 // GUIStyle btnStyle = new GUIStyle(GUI.skin.GetStyle("")); 107 // GUIStyle buttonStyle= new GUIStyle(UnityEditor.EditorStyles.label); 108 // EditorGUILayout.LabelField(new GUIContent("EditorGUILayout.LabelField")); 109 this.Repaint(); 110 } 111 112 void OpenFolder() 113 { 114 string scenePath = SceneManager.GetActiveScene().path; 115 string path = EditorUtility.OpenFolderPanel("选择文件夹", "",""); 116 if (path.Length == 0) 117 return; 118 folderpath = path; 119 } 120 }