Unity Editor customizes a window for recording bugs

Go directly to the code:

using System.Collections;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
     
public class MyFirstWindow : EditorWindow
{
    string bugReporterName = "";
    string description = "";
    GameObject buggyGameobject;
    MyFirstWindow()
    {
        this.titleContent = new GUIContent("Bug Reporter");
    }

    [MenuItem("Tool/Bug Reporter")]
    static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(MyFirstWindow));
    }

    void OnGUI()
    {
        EditorGUILayout.BeginVertical();

        GUILayout.Space(10);
        GUI.skin.label.fontSize = 24;
        GUI.skin.label.alignment = TextAnchor.MiddleCenter;
        GUILayout.Label("Bug Reporter");

        GUILayout.Space(10);
        bugReporterName = EditorGUILayout.TextField("Bug Name", bugReporterName);


        GUILayout.Space(10);
        GUI.skin.label.fontSize = 12;
        GUI.skin.label.alignment = TextAnchor.UpperLeft;
        GUILayout.Label("Currently Scene:" + EditorSceneManager.GetActiveScene().name);

        GUILayout.Space(10);
        GUILayout.Label("Time:" + System.DateTime.Now);

        GUILayout.Space(10);
        buggyGameobject = (GameObject)EditorGUILayout.ObjectField("Buggy Gameobject", buggyGameobject, typeof(GameObject), true);

        GUILayout.Space(10);
        GUILayout.BeginHorizontal();
        GUILayout.Label("Description", GUILayout.MaxWidth(80));
        description = EditorGUILayout.TextArea(description, GUILayout.MaxHeight(50));
        GUILayout.EndHorizontal();


        EditorGUILayout.Space();
        if (GUILayout.Button("Save Bug"))
        {
            SaveBug();
        }
        if (GUILayout.Button("Save Bug With Screenshot"))
        {
            SaveBugWithScreenshot();
        }

        GUILayout.EndVertical();
    }
    void SaveBug()
    {
        Directory.CreateDirectory("Assets/BugRepots/" + bugReporterName);
        StreamWriter sw = new StreamWriter("Assets/BugRepots/" + bugReporterName  + "/" + bugReporterName  + ".txt");
        sw.WriteLine(bugReporterName);
        sw.WriteLine(System.DateTime.Now.ToString());
        sw.WriteLine(EditorSceneManager.GetActiveScene().name);
        sw.WriteLine(description);
        sw.Close();
        

    }
    void SaveBugWithScreenshot()
    {
        Directory.CreateDirectory("Assets/BugRepots/" + bugReporterName);
        StreamWriter sw = new StreamWriter("Assets/BugRepots/" + bugReporterName + "/" + bugReporterName  + ".txt");
        sw.WriteLine(bugReporterName);
        sw.WriteLine(System.DateTime.Now.ToString());
        sw.WriteLine(EditorSceneManager.GetActiveScene().name);
        sw.WriteLine(description);
        sw.Close();
        ScreenCapture.CaptureScreenshot("Assets/BugRepots/" + bugReporterName + "/"  + bugReporterName + "ScreenShot" + ".png");
    }
}

final effect:

 

The screenshot function, Unity helped me replace it with other functions and methods, but I couldn’t take a screenshot. If you want the screenshot function, you can go to the official website to find related functions. If you want to know more detailed information, you can click the link below, the article is very detailed.

Reference for this article: https://blog.csdn.net/qq_39020624/article/details/89387005?spm=1001.2014.3001.5506

Guess you like

Origin blog.csdn.net/weixin_47042152/article/details/126054466