message | 字符串或对象,将被转换为字符串表示进行显示。 |
context | 此消息应用到的对象。 |
将消息记录到 Unity 控制台。
使用 Debug.Log 输出可帮助您调试应用程序的信息性消息。例如,您可以输出包含 GameObject.name 的消息以及有关该对象当前状态的信息。
可以使用字符串连接来设置消息的格式:
Debug.Log("Text: " + myText.text);
也可以使用富文本标记。
If you pass a GameObject or Component as the optional context
parameter,
Unity momentarily highlights that object in the Hierarchy
window when you click the
log message in the Console
. Use a context
object when you have many instances of an
object in a Scene so that you can identify which one produced the message. Example 2
,
below, illustrates how this feature works. When you run this example, first click one of
the cubes it creates in the Scene. The example prints a log message to the Console
.
When you click on the message, Unity highlights the context
object in the Hierarchy
window — in this case, the cube you clicked on in the Scene.
示例 1:显示了 Debug.Log 的一些用法:
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 的选择:
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]); } } } } } }
Note: Unity also adds Debug.Log messages to the Editor and Player log files. See Log Files for more information about accessing these files on different platforms.
See Also: MonoBehaviour.print.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.