UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Debug.Log
# [Debug](https://docs.unity3d.com/cn/2019.4/ScriptReference/Debug.html).Log public static void **Log** (object **message**); public static void **Log** (object **message**, [Object](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.html) **context**); ## 参数 | message | 字符串或对象,将被转换为字符串表示进行显示。 | | --------- | ---------------------------------------------- | | context | 此消息应用到的对象。 | ## 描述 将消息记录到 Unity 控制台。 使用 [Debug.Log](https://docs.unity3d.com/cn/2019.4/ScriptReference/Debug.Log.html) 输出可帮助您调试应用程序的信息性消息。例如,您可以输出包含 [GameObject.name](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject-name.html) 的消息以及有关该对象当前状态的信息。 可以使用字符串连接来设置消息的格式: `Debug.Log("Text: " + myText.text);` 也可以使用[富文本](https://docs.unity3d.com/cn/2019.4/Manual/StyledText.html)标记。 如果作为可选的 `context` 参数来传递 [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html) 或 [Component](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.html), 则当您在 `Console` 中单击日志消息时,Unity 将暂时在 `Hierarchy` 窗口中 高亮显示对象。当场景中有某个对象的多个实例时,请使用 `context` 对象, 以便可以识别生成该消息的实例。下面的 `示例 2/` 说明了此功能的工作方式。运行该示例时,首先单击该示例在场景中 创建的其中一个立方体。该示例将日志消息输出到 `Console`。 单击消息时,Unity 将在 `Hierarchy` 窗口中高亮显示 `context` 对象 — 在本示例中是您在场景中单击的立方体。 示例 1:显示了 [Debug.Log](https://docs.unity3d.com/cn/2019.4/ScriptReference/Debug.Log.html) 的一些用法: ``` using UnityEngine; using System.Collections; public class MyGameClass : MonoBehaviour { // A Light used in the Scene and needed by MyGameMethod(). public Light light; void MyGameMethod() { // Message with a GameObject name. Debug.Log("Hello: " + gameObject.name); // Message with light type. This is an Object example. Debug.Log(light.type); // Message using rich text. Debug.Log("<color=red>Error: </color>AssetBundle not found"); } } ``` 示例 2:显示点击的 [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html) 的选择: ``` using System.Collections; using System.Collections.Generic; using UnityEngine; // Debug.Log example // // Create three cubes. Place them around the world origin. // If a cube is clicked use Debug.Log to announce it. Use // Debug.Log with two arguments. Argument two allows the // cube to be automatically selected in the hierarchy when // the console message is clicked. // // Add this script to an empty GameObject. public class Example : MonoBehaviour { private GameObject[] cubes; void Awake() { // Create three cubes and place them close to the world space center. cubes = new GameObject[3]; float f = 25.0f; float p = -2.0f; float[] z = new float[] {0.5f, 0.0f, 0.5f}; for (int i = 0; i < 3; i++) { // Position and rotate each cube. cubes[i] = GameObject.CreatePrimitive(PrimitiveType.Cube); cubes[i].name = "Cube" + (i + 1).ToString(); cubes[i].transform.Rotate(0.0f, f, 0.0f); cubes[i].transform.position = new Vector3(p, 0.0f, z[i]); f -= 25.0f; p = p + 2.0f; } // Position and rotate the camera to view all three cubes. Camera.main.transform.position = new Vector3(3.0f, 1.5f, 3.0f); Camera.main.transform.localEulerAngles = new Vector3(25.0f, -140.0f, 0.0f); } void Update() { // Process a mouse button click. if (Input.GetMouseButtonDown(0)) { var ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { // Visit each cube and determine if it has been clicked. for (int i = 0; i < 3; i++) { if (hit.collider.gameObject == cubes[i]) { // This cube was clicked. Debug.Log("Hit " + cubes[i].name, cubes[i]); } } } } } } ``` 请注意,Unity 还会将 [Debug.Log](https://docs.unity3d.com/cn/2019.4/ScriptReference/Debug.Log.html) 消息添加到 Editor 和播放器日志文件中。有关在不同平台上访问这些文件的更多信息,请参阅[日志文件](https://docs.unity3d.com/cn/2019.4/Manual/LogFiles.html)。
da
2022年5月26日 17:49
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码