【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

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

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

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來接收你的上傳文件。

在網頁的右上角有個 + 號,點擊創建一個(看板)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的網站上看:

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

 

後記

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

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

改造後的文件:https://download.csdn.net/download/cyf649669121/12346924

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章