Using the Debug Class

在应用程序测试中,使用调试器可能影响程序运行。可通过Debug.Write或Debug.WriteLine方法将值输出到Output窗口。Debug类有多个实用方法,如Assert、Write、WriteLine等。Assert可在条件不满足时输出信息,WriteLineIf可在指定条件为真时输出内容。
Using the Debug Class
During the testing of your application, you may find that when you are
stepping through code the application works fine, but when you run the
program straight through it does not work the way you expected. The reason
for this is that you can change the way your program runs by using the
debugger梩he fact that you're executing the code extremely slowly can
affect the behavior of the application! Instead of using the debugger to
test values, you may find it convenient to print those values to the
Immediate window.

You may print directly to the Output window (use the View, Other Windows,
Output menu item to display this window and select Debug from the drop-down
list at the top of the window) from within your application by executing
the Debug.Write or Debug.WriteLine method. The values placed into the
Immediate window using either of these methods will still be available in
Break or Design mode. It can be useful to see those values without having
to enter Break mode and thereby possibly change the execution of your
program. You'll most likely use the WriteLine method of the Debug object,
simply sending the method a string to display in the Output window, like
this:

Debug.WriteLine("Now executing btnStart_Click()")

TIP

The Output window can display both Build and Debug information. Make sure
you select Debug from the drop-down list at the top of the window in order
to see the Debug.WriteLine output.



To illustrate these points, make sure DebugForm.aspx is the start page for
the sample project, run the project, and select Debug Class on the Web
page. Clicking this button loads a Web Form named DebugEvents.aspx. This
form includes several calls to the Debug.WriteLine method in several
different event procedures. This page illustrates the use of the
Debug.WriteLine method and also gives you an idea of which events are fired
when a Web Form loads and unloads. Figure 9.23 shows some of the sample
output from these Debug.WriteLine method calls.

Figure 9.23. Sample output from DebugEvents Web Form.


NOTE

If you're a VB6 developer, you may be used to the output from the Debug
class going into the Immediate window. Look there all you like in Visual
Studio .NET, but you won't find what you're looking for. This content now
goes to the Output window instead.



Listing 9.1 shows part of the code that creates the output shown in Figure
9.23.

Listing 9.1 The Example Uses This Code to Help Demonstrate Debugging
Techniques
Private Sub Page_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
  'Put user code to initialize the page here

  Debug.WriteLine("Page_Load")

End Sub

Private Sub Page_PreRender( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.PreRender

  Debug.WriteLine("Page_PreRender")

End Sub

Private Sub Page_Unload( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Unload



  Debug.WriteLine("Page_Unload")

End Sub

The Debug class has several methods you might find useful, in addition to
the WriteLine method. Table 9.4 describes some of the more common ones you
might use.

Table 9.4. Methods of the Debug Class Method  Description 
Assert  Stops the program if the condition you pass to this method does not
evaluate to True. This method only works in the design mode: All Debug
statements are removed from the final compiled program. 
Write  Writes a value to the output window without a CRLF. 
WriteLine  Writes a value to the output window with a CRLF. 
WriteIf  Writes a value to the output window without a CRLF, if a specified
condition is true. 
WriteLineIf  Writes a value to the output window with a CRLF, if a
specified condition is true. 


The Assert Method
The Debug.Assert method allows you to insert assertions in your code梩hat
is, debugging statements that display information into the Output window if
a specific condition isn't met. Debug.Assert is a powerful debugging tool
in that it allows you to ensure that you've passed correct parameters to a
procedure or that a variable always contains a specific value or range of
values. Some programmers insist that you shouldn't program an application
without using the Debug.Assert method scattered throughout your whole
application anytime you make any type of assumption about the current state
of parameters of variables. The following code shows how you might check to
see whether a number typed into a text box was typed in correctly:

Private Sub AssertSample()
  Dim intNum As Integer

  intNum = CInt(Val(txtNumber.Text))


  Debug.Assert(intNum >= 0 And intNum <= 5, _
    "Number must be between 0 and 5")
End Sub

If you run this code and enter 6 into the text box on sample form
(DebugClass.aspx), the assertion will return False. The .NET runtime will
display a message into the Output window like the one shown in Figure 9.24.

Figure 9.24. Debug.Assert displays information like this into the Output
window when the assertion fails.


TIP

Debug.Assert isn't normally used to validate input, as you've seen here.
The validation controls provided by ASP.NET do a better job at that. We've
purposefully selected a simple example here, just to demonstrate how you
might use Debug.Assert. Normally, you'd use Debug.Assert to verify that the
data sent to a procedure meets certain criteria that you just assume梖or
example, that a string isn't too long or that a value that shouldn't be
zero is, in fact, not equal to zero.



The WriteLineIf Method
If you only wish to write a line to the Output window when a specified
condition is true, you can use the WriteLineIf method of the Debug class.
For example, the sample page uses code like the following to check a
condition before writing information to the Output window:

Private Sub WriteLineIfSample()
  Dim intNum As Integer

  intNum = CInt(txtNumber.Text)
  Debug.WriteLineIf(intNum >= 0 And intNum <= 5, _
   "You input a correct number")
End Sub

TIP

If you want to dig a little deeper, you'll find that the Debug class (and
its cousin, the Trace class) are far more powerful than you've seen here.
Each of these classes relies on the concept of "listener" objects梩hat is,
objects that "listen" for output and collect the output for display. By
default, the only listener for the Debug class is the Output window.
Without too much effort, you can create listeners that write output to a
text file or to the Windows Event Log. Although doing this work is beyond
the scope of this material, you can check out the Debug class in the online
help for information on creating other listeners.

提供的引用内容未涉及Dialogue使用ScriptableObject进行实例化的原因、方法或相关解决方案的信息。不过,一般来说,使用ScriptableObject实例化Dialogue有以下原因和方法: ### 原因 - **数据可复用性**:ScriptableObject允许将Dialogue数据存储为独立的资产文件,这些文件可以在项目的不同部分重复使用,避免了数据的重复编写和维护成本。 - **数据与逻辑分离**:将Dialogue的数据与实际的游戏逻辑分离,使得代码更加清晰和易于维护。不同的Dialogue可以通过修改ScriptableObject资产来调整,而不需要修改游戏逻辑代码。 - **易于编辑**:Unity的编辑器界面提供了对ScriptableObject的可视化编辑支持,非程序员也可以方便地创建和修改Dialogue内容。 ### 方法 以下是使用ScriptableObject实例化Dialogue的基本步骤和示例代码: #### 步骤 1. **定义Dialogue类**:创建一个继承自ScriptableObject的类,用于表示Dialogue数据。 2. **创建ScriptableObject资产**:在Unity编辑器中创建该类的实例。 3. **在代码中使用Dialogue资产**:在游戏逻辑中加载和使用这些资产。 #### 示例代码 ```csharp using UnityEngine; // 定义Dialogue类 [CreateAssetMenu(fileName = "NewDialogue", menuName = "Dialogue/Create Dialogue")] public class Dialogue : ScriptableObject { public string speaker; public string[] sentences; } // 在游戏逻辑中使用Dialogue资产 public class DialogueManager : MonoBehaviour { public Dialogue dialogue; void Start() { // 检查Dialogue资产是否已分配 if (dialogue != null) { Debug.Log("Speaker: " + dialogue.speaker); foreach (string sentence in dialogue.sentences) { Debug.Log(sentence); } } } } ``` ### 相关解决方案 如果在实例化过程中遇到问题,可以检查以下几点: - 确保ScriptableObject类正确继承自UnityEngine.ScriptableObject。 - 检查是否在Unity编辑器中正确创建了ScriptableObject资产。 - 确保在代码中正确引用了ScriptableObject资产。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值