전체 글 (632)

2025
07.01

gist의 코드를 참조함.
https://gist.github.com/filod/ba1e1522c1821cd24ca1a0c9090eb440#file-asmdefdebug-cs-L59

 

gist의 코드를 참조하고 30초를 최대로 그래프로 나타내는 커스텀로그를 추가하였음.

에디터 폴더에 넣으면 동작합니다.

 

테스트 버전 Unity 6.1.2f1

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using UnityEditor;
using UnityEditor.Compilation;

/// <summary>
/// https://gist.github.com/filod/ba1e1522c1821cd24ca1a0c9090eb440#file-asmdefdebug-cs-L59
/// </summary>
[InitializeOnLoad]
public class CompileTimeChecker
{
    const string AssemblyReloadEventsEditorPref = "AssemblyReloadEventsTime";
    const string AssemblyCompilationEventsEditorPref = "AssemblyCompilationEvents";
    static readonly int ScriptAssembliesPathLen = "Library/ScriptAssemblies/".Length;
    private static string AssemblyTotalCompilationTimeEditorPref = "AssemblyTotalCompilationTime";

    static Dictionary<string, DateTime> s_StartTimes = new Dictionary<string, DateTime>();

    static StringBuilder s_BuildEvents = new StringBuilder();
    static double s_CompilationTotalTime;

    static CompileTimeChecker()
    {
        CompilationPipeline.assemblyCompilationStarted += CompilationPipelineOnAssemblyCompilationStarted;
        CompilationPipeline.assemblyCompilationFinished += CompilationPipelineOnAssemblyCompilationFinished;
        AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEventsOnBeforeAssemblyReload;
        AssemblyReloadEvents.afterAssemblyReload += AssemblyReloadEventsOnAfterAssemblyReload;
    }

    static void CompilationPipelineOnAssemblyCompilationStarted(string assembly)
    {
        s_StartTimes[assembly] = DateTime.UtcNow;
    }

    static void CompilationPipelineOnAssemblyCompilationFinished(string assembly, CompilerMessage[] arg2)
    {
        var timeSpan = DateTime.UtcNow - s_StartTimes[assembly];
        s_CompilationTotalTime += timeSpan.TotalMilliseconds;
        s_BuildEvents.AppendFormat("{0:0.00}s {1}\n", timeSpan.TotalMilliseconds / 1000f,
            assembly.Substring(ScriptAssembliesPathLen, assembly.Length - ScriptAssembliesPathLen));
    }

    static void AssemblyReloadEventsOnBeforeAssemblyReload()
    {
        var totalCompilationTimeSeconds = s_CompilationTotalTime / 1000f;
        s_BuildEvents.AppendFormat("------- compilation total: {0:0.00}s\n", totalCompilationTimeSeconds);
        EditorPrefs.SetString(AssemblyReloadEventsEditorPref, DateTime.UtcNow.ToBinary().ToString());
        EditorPrefs.SetString(AssemblyCompilationEventsEditorPref, s_BuildEvents.ToString());
        EditorPrefs.SetString(AssemblyTotalCompilationTimeEditorPref, totalCompilationTimeSeconds.ToString(CultureInfo.InvariantCulture));
    }

    static void AssemblyReloadEventsOnAfterAssemblyReload()
    {
        var binString = EditorPrefs.GetString(AssemblyReloadEventsEditorPref);
        var totalCompilationTimeSeconds = float.Parse(EditorPrefs.GetString(AssemblyTotalCompilationTimeEditorPref), CultureInfo.InvariantCulture); 

        long bin;
        if (long.TryParse(binString, out bin))
        {
            var date = DateTime.FromBinary(bin);
            var time = DateTime.UtcNow - date;
            var compilationTimes = EditorPrefs.GetString(AssemblyCompilationEventsEditorPref);
            var totalTimeSeconds = totalCompilationTimeSeconds + time.TotalSeconds;
            if (!string.IsNullOrEmpty(compilationTimes))
            {
                Debug.Log($"<color=cyan>- 컴파일 시간 : {LogProgress(totalTimeSeconds)} ---------------------------</color>\n" + compilationTimes + "Assembly Reload Time: " + time.TotalSeconds + "s\n");
            }
        }
    }

    static string LogProgress(double elapsedSeconds)
    {
        const double maxTime = 30.0;
        const int totalBlocks = 15;
        const double secondsPerBlock = maxTime / totalBlocks;

        // 채워질 블록 수 계산
        int filledBlocks = (int)(elapsedSeconds / secondsPerBlock);
        if (filledBlocks > totalBlocks) filledBlocks = totalBlocks;

        // 블록 문자열 생성
        string filled = new string('■', filledBlocks);
        string empty = new string('□', totalBlocks - filledBlocks);

        return $"[ {filled}{empty} ] / {elapsedSeconds:0.0}초";
    }
}
#endif