2017-05-04 158 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时,构建过程将丢弃它。

相关问题