與 VSTU 共用 Unity 記錄回呼Share the Unity log callback with VSTU

Visual Studio Tools for Unity 使用 Unity 註冊記錄回呼,以便將其主控台串流至 Visual Studio。Visual Studio Tools for Unity registers a log callback with Unity to be able to stream its console to Visual Studio. 如果您的編輯器指令碼也使用 Unity 註冊記錄回呼,VSTU 回呼可能會與您的回呼相衝突。If your editor scripts also register a log callback with Unity, the VSTU callback might interfere with your callback. 若要避免這種可能性,請使用 VisualStudioIntegration.LogCallback 事件來與 VSTU 合作。To prevent this possibility, use the VisualStudioIntegration.LogCallback event to cooperate with VSTU.

示範Demonstrates

如何共用 Visual Studio Tools for Unity 所建立的 Unity 記錄回呼。How to share the Unity Log Callback created by Visual Studio Tools for Unity.

範例Example

#if ENABLE_VSTU
using System;

using UnityEngine;
using UnityEditor;

using SyntaxTree.VisualStudio.Unity.Bridge;

[InitializeOnLoad]
public class LogCallbackHook
{
    static LogCallbackHook()
    {
        VisualStudioIntegration.LogCallback += (string condition, string trace, LogType type) =>
        {
            // place code that implements your log callback here
        };
    }
}
#endif

另請參閱See also

範例:產生專案檔Example: Project file generation