본문 바로가기
프로그래밍

유니티: 현재 씬의 모든 버튼 클릭 이벤트를 로그로 출력하기

by 어부 2025. 1. 25.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AllFindButton : MonoBehaviour
{
    void Start()
    {
        FindAndLogAllButtons();
    }

    private void FindAndLogAllButtons()
    {
        // 씬 내 모든 활성화된 버튼 가져오기
        Button[] buttons = FindObjectsOfType<Button>();

        foreach (Button button in buttons)
        {
            LogButtonClickEvents(button);
        }
    }

    private void LogButtonClickEvents(Button button)
    {
        int eventCount = button.onClick.GetPersistentEventCount();
        Debug.LogWarning($"Button '{button.gameObject.name}' has {eventCount} onClick delegates:");

        for (int i = 0; i < eventCount; i++)
        {
            LogPersistentEvent(button, i);
        }
    }

    private void LogPersistentEvent(Button button, int eventIndex)
    {
        var target = button.onClick.GetPersistentTarget(eventIndex);
        var methodName = button.onClick.GetPersistentMethodName(eventIndex);

        if (target == null)
        {
            Debug.LogWarning($"onClick {eventIndex}: Target is null.");
        }
        else
        {
            Debug.Log($"onClick {eventIndex}: {target}.{methodName}()");
        }
    }
}