2017-05-04 155 views
2

當使用我的HoloLens時,當我在Unity腳本中拋出異常時,Visual Studio中的調試輸出顯示沒有行號的堆棧跟蹤。從Visual Studio運行Unity HoloLens程序時,拋出異常時,如何獲取堆棧跟蹤中的行號?

我該如何獲得行號以及堆棧跟蹤?我會很好的將它記錄在調試輸出以外的其他地方。

下面是在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) 

以及相應的統一的腳本(我做了一個立方體,並附NewBehaviourScript分量):

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

我試圖從發佈修改構建調試不會給出行號。

我試着用搜索引擎,它看起來像它沒有顯示的行數爲他人,以及:http://answers.unity3d.com/questions/1315985/null-reference-in-line-0.html

我試着問上Microsoft's forums,但沒有得到任何有用的回覆。

回答

0

我不認爲你會得到行號,因爲它不再存在。你可以在Unity編輯器中找到它,因爲你沒有運行完整的應用程序,所以Unity仍然可以訪問非編譯代碼。在設備上運行時,它會向VS控制檯發送有關打印和錯誤的調試命令,但所有代碼都是二進制的,因此沒有理由也無法提供行號。

其實這並不是特定於Hololens,但你會得到相同的Android或iOS。一旦編譯完成,代碼不再一樣,編譯器執行優化時,它甚至不會一一匹配。

你可以做的是放置調試命令來查看它發生的地方。

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宏,如果你將有隻有運行調試目的的代碼。通過這種方式,您可以在導出時輕鬆排除它。調試的第二個調用不需要在宏中,因爲當您將構建版本設置爲Release或Master時,構建過程將丟棄它。

相關問題