1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
| using UnityEngine; using UnityEditor; using UnityEditor.SceneManagement; using System.IO;
public class BugReportWindow : EditorWindow { private string bugName = ""; private GameObject bugGameObject; private string bugDescription = "";
private Vector2 scrollPosition; private int selectedSeverity = 0; private string[] severityOptions = { "低", "中", "高", "紧急" }; private bool showAdvanced = false; private string savePath = "Assets/BugReports/";
private float minRange = 0f; private float maxRange = 100f; private int selectedTab = 0; private string[] tabOptions = { "基本信息", "详细信息", "附件" };
[MenuItem("Tools/Bug Reporter %_b")] static void ShowWindow() { var window = GetWindow<BugReportWindow>("Bug Reporter"); window.minSize = new Vector2(400, 500); window.Show(); }
private void OnGUI() { DrawHeader(); DrawBasicInfo(); DrawAdvancedOptions(); DrawButtons(); }
private void DrawHeader() { EditorGUILayout.Space(10);
var titleStyle = new GUIStyle(EditorStyles.boldLabel) { fontSize = 24, alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Bold };
EditorGUILayout.LabelField("🐛 Bug 报告工具", titleStyle);
EditorGUILayout.Space(10); }
private void DrawBasicInfo() { bugName = EditorGUILayout.TextField(new GUIContent("Bug 名称", "简短描述问题"), bugName);
selectedSeverity = EditorGUILayout.Popup("严重程度", selectedSeverity, severityOptions);
bugGameObject = EditorGUILayout.ObjectField( new GUIContent("关联物体", "相关的游戏对象"), bugGameObject, typeof(GameObject), true ) as GameObject;
EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField("当前场景:", EditorStyles.miniLabel); EditorGUILayout.LabelField(EditorSceneManager.GetActiveScene().name, EditorStyles.miniLabel); GUILayout.FlexibleSpace(); EditorGUILayout.LabelField(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), EditorStyles.miniLabel); } EditorGUILayout.EndHorizontal();
EditorGUILayout.Space(); EditorGUILayout.LabelField("Bug 详细描述:"); bugDescription = EditorGUILayout.TextArea( bugDescription, GUILayout.MinHeight(100), GUILayout.MaxHeight(200) );
EditorGUILayout.HelpBox( $"字符数: {bugDescription.Length}", bugDescription.Length > 0 ? MessageType.Info : MessageType.Warning ); }
private void DrawAdvancedOptions() { EditorGUILayout.Space();
showAdvanced = EditorGUILayout.Foldout(showAdvanced, "高级选项", true);
if (showAdvanced) { EditorGUI.indentLevel++;
EditorGUILayout.MinMaxSlider( new GUIContent("范围选择"), ref minRange, ref maxRange, 0f, 100f ); EditorGUILayout.LabelField($"最小值: {minRange:F1}", $"最大值: {maxRange:F1}");
EditorGUILayout.Space();
selectedTab = GUILayout.Toolbar(selectedTab, tabOptions);
switch (selectedTab) { case 0: DrawBasicTab(); break; case 1: DrawDetailTab(); break; case 2: DrawAttachmentTab(); break; }
EditorGUI.indentLevel--; } }
private void DrawBasicTab() { EditorGUILayout.LabelField("基本选项内容"); EditorGUILayout.Toggle("发送邮件通知", false); EditorGUILayout.Toggle("标记为已修复", false); }
private void DrawDetailTab() { EditorGUILayout.LabelField("详细选项内容"); EditorGUILayout.EnumPopup("Bug 类型", BugType.Graphical); EditorGUILayout.IntField("重现步骤数量", 3); }
private void DrawAttachmentTab() { EditorGUILayout.LabelField("附件内容");
DrawPathSelector(); }
private void DrawPathSelector() { EditorGUILayout.LabelField("保存路径", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal(); { Rect pathRect = EditorGUILayout.GetControlRect(true, 20); savePath = EditorGUI.TextField(pathRect, savePath);
if (GUILayout.Button("浏览...", GUILayout.Width(80))) { savePath = EditorUtility.SaveFolderPanel( "选择保存路径", savePath, "BugReports" );
if (string.IsNullOrEmpty(savePath)) { savePath = "Assets/BugReports/"; } } } EditorGUILayout.EndHorizontal();
HandleDragAndDrop(pathRect); }
private void HandleDragAndDrop(Rect rect) { Event currentEvent = Event.current;
if ((currentEvent.type == EventType.DragUpdated || currentEvent.type == EventType.DragExited) && rect.Contains(currentEvent.mousePosition)) { DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0) { if (currentEvent.type == EventType.DragUpdated) { DragAndDrop.AcceptDrag(); } else { savePath = DragAndDrop.paths[0]; } } } }
private void DrawButtons() { EditorGUILayout.Space(20);
EditorGUILayout.BeginHorizontal(); { if (GUILayout.Button("保存 Bug", GUILayout.Height(30))) { SaveBugReport(); }
if (GUILayout.Button("保存并截图", GUILayout.Height(30))) { SaveBugReportWithScreenshot(); } } EditorGUILayout.EndHorizontal();
if (string.IsNullOrEmpty(bugName)) { EditorGUILayout.HelpBox("请填写 Bug 名称", MessageType.Warning); } }
private void SaveBugReport() { if (!ValidateInput()) return;
Directory.CreateDirectory(savePath);
string timestamp = System.DateTime.Now.ToString("yyyyMMdd_HHmmss"); string fileName = $"{timestamp}_{bugName}"; string filePath = Path.Combine(savePath, fileName + ".txt");
using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine($"=== Bug 报告 ==="); writer.WriteLine($"Bug 名称: {bugName}"); writer.WriteLine($"严重程度: {severityOptions[selectedSeverity]}"); writer.WriteLine($"关联物体: {(bugGameObject ? bugGameObject.name : "无")}"); writer.WriteLine($"场景名称: {EditorSceneManager.GetActiveScene().name}"); writer.WriteLine($"创建时间: {System.DateTime.Now}"); writer.WriteLine($"\n=== 详细描述 ==="); writer.WriteLine(bugDescription); }
AssetDatabase.Refresh();
Debug.Log($"Bug 报告已保存: {filePath}"); EditorUtility.DisplayDialog("成功", "Bug 报告已保存!", "确定"); }
private void SaveBugReportWithScreenshot() { if (!ValidateInput()) return;
Directory.CreateDirectory(savePath);
string timestamp = System.DateTime.Now.ToString("yyyyMMdd_HHmmss"); string fileName = $"{timestamp}_{bugName}";
string textPath = Path.Combine(savePath, fileName + ".txt"); using (StreamWriter writer = new StreamWriter(textPath)) { writer.WriteLine($"=== Bug 报告 ==="); writer.WriteLine($"Bug 名称: {bugName}"); writer.WriteLine($"关联物体: {(bugGameObject ? bugGameObject.name : "无")}"); writer.WriteLine($"场景名称: {EditorSceneManager.GetActiveScene().name}"); writer.WriteLine($"创建时间: {System.DateTime.Now}"); writer.WriteLine($"\n=== 详细描述 ==="); writer.WriteLine(bugDescription); }
string screenshotPath = Path.Combine(savePath, fileName + ".png"); ScreenCapture.CaptureScreenshot(screenshotPath);
AssetDatabase.Refresh();
Debug.Log($"Bug 报告和截图已保存"); EditorUtility.DisplayDialog("成功", "Bug 报告和截图已保存!", "确定"); }
private bool ValidateInput() { if (string.IsNullOrEmpty(bugName)) { EditorUtility.DisplayDialog("错误", "请填写 Bug 名称!", "确定"); return false; } return true; }
private enum BugType { Graphical, Audio, Gameplay, Network, Performance } }
|