天天看點

【Unity】【PC】【錯誤上報】Bug上報插件 Trello Bug Tracker 使用介紹 (一):使用者上報部分前言原理插件的使用後記

前言

最近需要做一個在PC端的Bug上報功能,之前的Bugly是用在移動端的,其實不是很适合。

是以需要一個在PC端的Bug上報,然後在網上找到一個還不錯的插件:Trello Bug Tracker

https://assetstore.unity.com/packages/tools/integration/trello-bug-tracker-pro-75613

【Unity】【PC】【錯誤上報】Bug上報插件 Trello Bug Tracker 使用介紹 (一):使用者上報部分前言原理插件的使用後記

不過網上對這個的介紹很少,是以一邊學一邊記錄一下。

其實這類功能最關鍵的要實作的功能有這麼幾個:

1、使用者回報能上報;

2、能抓取日志;

3、自動抓取報錯上報;

4、有一個給開發者的檢視界面。

前面的都能在用戶端實作,關鍵是第四個對吧。這個插件其實取了個巧,他是相當于白嫖了Trello這個網絡協作插件來進行上報(其實按照他這個原理理論上也是可以接入别的看闆網頁之類,比如企業微信的TAPD這種)。

原理

這個插件的原理就是調用了Trello的開發者API,然後用WWW的方式來上傳一個卡片(Card)到Trello的看闆(Borad)。你可以了解為,這個插件白嫖了 Trello 的網站、資料存儲,來實作了自己的錯誤上報功能。

插件的使用

原版的插件下載下傳下來之後,他分為2個部分。一個是錯誤上報,還有一個部分是輔助類的,比如畫圖什麼的。個人認為畫圖什麼的沒啥大用,可以不用管,但是上報的部分可以留着。

可以先打開它的例子程式來操作一下,點右下那個蟲就可以上報了。

之後更多的是在程式方面的工作了。

初始化

當然,你需要先去Trello 的官網上注冊一個賬号先:https://trello.com。

之後需要将你的Key和Token取出來,用來初始化。

擷取Key和Token的位址為 : https://trello.com/app-key

然後你需要在Trello上建立立一個Board來接收你的上傳檔案。

【Unity】【PC】【錯誤上報】Bug上報插件 Trello Bug Tracker 使用介紹 (一):使用者上報部分前言原理插件的使用後記

在網頁的右上角有個 + 号,點選建立一個(看闆)Board,然後記下這個看闆的名字。

之後轉入到代碼上:

public string Key;
        public string Token;
        public string BoardName;

        private Trello trello;
        public const string ListName_Advice = "PlayerAcvice";
        public const string ListName_PlayerReport = "PlayerReport";
        public const string ListName_AutoReport = "AutoReport";

        IEnumerator StartInit()
        {
            if (trello != null && trello.IsConnected())
            {
                Debug.Log("Trello 已經連接配接上!");
                yield break;
            }
            trello = new Trello(Key, Token);
            yield return trello.PopulateBoardsRoutine();
            //設定目前看闆;
            trello.SetCurrentBoard(BoardName);
            //擷取目前看闆下的清單;
            yield return trello.PopulateListsRoutine();

            /*
             * 從網上拉下來的List可能與設定的有所不同。
             * 設定的是需要 PlayerAdvice , PlayerReprot , AutoReport 三個。
             * 分别對應的是玩家建議、玩家的錯誤回報、自動的錯誤上報。
             * 如果這三個标簽沒有則需要手動添加;
             */

            bool isNeedReCache = false;

            if (!trello.IsListCached(ListName_Advice))
            {
                yield return trello.UploadListRoutine(NewList(ListName_Advice));
                isNeedReCache = true;
            }
            if (!trello.IsListCached(ListName_PlayerReport))
            {
                yield return trello.UploadListRoutine(NewList(ListName_PlayerReport));
                isNeedReCache = true;
            }
            if (!trello.IsListCached(ListName_AutoReport))
            {
                yield return trello.UploadListRoutine(NewList(ListName_AutoReport));
                isNeedReCache = true;
            }
            //如果修改了List,那就重新緩存一下;
            if (isNeedReCache)
                yield return trello.PopulateListsRoutine();
        }

        private TrelloList NewList(string name)
        {
            TrelloList list =  trello.NewList();
            list.name = name;
            return list;
        }
           

玩家上報

玩家上報需要建立一個新的UI面闆來處理上報,這裡就不用贅述了。直接跳到擷取到了所需的資訊之後,如何上傳。

#region 使用者上報部分

        /// <summary>
        /// 發送使用者的建議;
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public void SendAdvice(string title, string content)
        {
            TrelloCard card = trello.NewCard(title, content, ListName_Advice);
            StartCoroutine(SendReportRoutine(card, null));
        }

        /// <summary>
        /// 發送使用者的錯誤報告
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public void SendPlayerReport(string title, string content)
        {
            //與玩家建議不同的是,玩家錯誤報告需要上傳錯誤日志;
            TrelloCard card = trello.NewCard(title, content, ListName_PlayerReport);
            StartCoroutine(SendReportRoutine(card, null, true));
        }

        public IEnumerator SendReportRoutine(TrelloCard card, List<Texture2D> screenshots = null, bool isLog = false)
        {
            // We upload the card with an async custom coroutine that will return the card ID
            // Once it has been uploaded.
            CustomCoroutine cC = new CustomCoroutine(this, trello.UploadCardRoutine(card));
            yield return cC.coroutine;

            // The uploaded card ID
            string cardID = (string)cC.result;

            //這裡是截圖上傳,如果是玩家建議則不需要截圖;
            if (screenshots != null && screenshots.Count > 0)
            {
                int i = 0;
                foreach (Texture2D screenshot in screenshots)
                {
                    i++;
                    // We can now attach the screenshot to the card given its ID.
                    yield return trello.SetUpAttachmentInCardRoutine(cardID, "ScreenShot" + i + ".png", screenshot);
                }
            }

            if (isLog)
            {
#if UNITY_STANDALONE
                // We make sure the log exists before trying to retrieve it.
                if (System.IO.File.Exists(logPath))
                {
                    // We make a copy of the log since the original is being used by Unity.
                    System.IO.File.Copy(logPath, logPathCopy, true);

                    // We attach the Unity log file to the card.
                    yield return trello.SetUpAttachmentInCardFromFileRoutine(cardID, "output_log.txt", logPathCopy);
                }
#endif
            }

            //這裡是上傳自定義的玩家資訊:
            string userData = $"UserName : {"測試使用者1"};\n" +
                $"玩家等級{1}";
            yield return trello.SetUpAttachmentInCardRoutine(cardID, "UserInfo.txt", userData);
        }

        // Platform dependent Log Path
        private string logPath
        {
            get
            {

#if UNITY_STANDALONE_WIN
                var absolutePath = "%USERPROFILE%/AppData/LocalLow/" + Application.companyName + "/" + Application.productName + "/output_log.txt";
                var filePath = System.Environment.ExpandEnvironmentVariables(absolutePath);
                return filePath;
                //Old windows log path
                //return System.Diagnostics.Process.GetCurrentProcess().ProcessName + "_Data/output_log.txt";

#elif UNITY_STANDALONE_LINUX
                return "~/.config/unity3d/" + Application.companyName + "/" + Application.productName + "/Player.log";

#elif UNITY_STANDALONE_OSX
                return "~/Library/Logs/Unity/Player.log";
#else
                return "";
#endif
            }
        }
        // Platform dependent Log Path copy
        private string logPathCopy
        {
            get
            {
#if UNITY_STANDALONE_WIN
                var absolutePath = "%USERPROFILE%/AppData/LocalLow/" + Application.companyName + "/" + Application.productName + "/output_logCopy.txt";
                var filePath = System.Environment.ExpandEnvironmentVariables(absolutePath);
                return filePath;
                //Old windows log path
                //return System.Diagnostics.Process.GetCurrentProcess().ProcessName + "_Data/output_log2.txt";

#elif UNITY_STANDALONE_LINUX
                return "~/.config/unity3d/" + Application.companyName + "/" + Application.productName + "/PlayerCopy.log";

#elif UNITY_STANDALONE_OSX
               return "~/Library/Logs/Unity/PlayerCopy.log";
#else
                return "";
#endif
            }
        }

        #endregion
           

這樣就算完成了一個簡易地,由玩家觸發的上傳機制。

接下來測試一下,我在面闆裡分别上傳了1個建議和1個報錯,然後我們轉到Trello的網站上看:

【Unity】【PC】【錯誤上報】Bug上報插件 Trello Bug Tracker 使用介紹 (一):使用者上報部分前言原理插件的使用後記

可以看到已經成功上傳了。後續隻需要調整一些格式和内容,根據項目進行調優即可。

後記

還有一個很重要的功能就是Bug的自動上報,也就是當我們檢測到控制台列印出Error或者Exception的時候,就自動抓取進行上報。這個功能我打算新開一篇來寫,這一篇篇幅略長。

新的一篇在這裡:https://blog.csdn.net/cyf649669121/article/details/105654015

改造後的檔案:https://download.csdn.net/download/cyf649669121/12346924

繼續閱讀