簡體   English   中英

從Visual Studio運行Unity HoloLens程序時,引發異常時,如何獲取堆棧跟蹤中的行號?

[英]When running a Unity HoloLens program from Visual Studio, when an exception is thrown how do I get the line numbers in the stack trace?

當前,當在使用HoloLens的同時從Unity腳本中引發異常時,Visual Studio中的“調試輸出”將顯示堆棧跟蹤而不顯示行號。

如何獲得行號和堆棧跟蹤? 我可以將其記錄在除Debug Output之外的其他地方。

這是Visual Studio中的一些示例輸出:

Exception thrown: 'System.NullReferenceException' in Assembly-CSharp.dll
NullReferenceException: Object reference not set to an instance of an object.
   at NewBehaviourScript.Update()
   at NewBehaviourScript.$Invoke6Update(Int64 instance, Int64* args)
   at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method) 
(Filename: <Unknown> Line: 0)

以及相應的Unity腳本(我制作了一個多維數據集,並附加了NewBehaviourScript組件):

public class NewBehaviourScript : MonoBehaviour {           
    // Update is called once per frame
    void Update ()
    {
        object a = null;
        a.GetType();    
    }
}

我嘗試將版本從Release更改為Debug,但沒有給出行號。

我嘗試了Google搜索,但似乎也沒有顯示其他人的行號: http : //answers.unity3d.com/questions/1315985/null-reference-in-line-0.html

我嘗試在Microsoft的論壇上提問 ,但未收到任何有用的回復。

我認為您不會獲得行號,因為它不再存在。 之所以在Unity編輯器中得到它,是因為您沒有運行應用程序的完整版本,因此Unity仍然可以訪問未編譯的代碼。 在設備上運行時,它將有關打印和錯誤的調試命令發送到VS控制台,但是此時所有代碼都是二進制的,因此沒有理由也不可能提供行號。

實際上,這並不是Hololens特有的,但是在Android或iOS中您將獲得相同的效果。 構建之后,代碼將不再相同,甚至在編譯器執行優化時也無法一對一匹配。

您可以做的是放置Debug命令以查看發生的位置。

public class NewBehaviourScript : MonoBehaviour {           
    // Update is called once per frame
    void Update ()
    {
        object a = null;
#if DEBUG
        if(a == null)
        { 
            Debug.Log("[NewBehaviourScript] Running update with null a object");
        }
#endif 
        a.GetType(); 
        Debug.Log("[NewBeahviourScript] if this line prints, method did not crash");
    } 
}

在此示例中,如果您僅出於調試目的而運行代碼,則可以使用DEBUG宏。 這樣,您可以輕松在導出時將其排除。 在宏中不需要對Debug的第二次調用,因為當您將構建設置為Release或Master時,構建過程將丟棄它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM