【问题标题】: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?从 Visual Studio 运行 Unity HoloLens 程序时,如果引发异常,如何获取堆栈跟踪中的行号?
【发布时间】:2017-05-04 14:48:51
【问题描述】:

目前,当我在使用 HoloLens 时从 Unity 脚本中引发异常时,Visual Studio 中的调试输出会显示堆栈跟踪而没有行号。

如何获取行号以及堆栈跟踪?我可以将它记录在除调试输出之外的其他地方。

这是 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脚本(我做了一个Cube并附加了一个NewBehaviourScript组件):

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

我尝试将构建从 Release 更改为 Debug 没有给出行号。

我试过谷歌搜索,它似乎也没有显示其他人的行号:http://answers.unity3d.com/questions/1315985/null-reference-in-line-0.html

我尝试在Microsoft's forums 上提问,但没有收到任何有用的回复。

【问题讨论】:

    标签: visual-studio unity3d hololens


    【解决方案1】:

    我认为您不会得到行号,因为它不再存在。您可以在 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 时,构建过程将丢弃它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2010-10-10
      • 2011-08-10
      相关资源
      最近更新 更多