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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值