Unity Editor自定义一个记录Bug的窗口

直接上代码:

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");
    }
}

最终效果:

 

那个截屏的功能,Unity帮我换成了别的函数方法,但是截不了图,如果想要截图功能的可以到官网查找相关的函数。如果想了解详细的信息可以点击下面的链接,那个文章里说得很仔细。

本文章参考:https://blog.csdn.net/qq_39020624/article/details/89387005?spm=1001.2014.3001.5506

猜你喜欢

转载自blog.csdn.net/weixin_47042152/article/details/126054466