#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

public class MinimapCapture : MonoBehaviour
{
    [SerializeField] private Camera _minimapCamera;
    [SerializeField] private int _resolution = 1024;
    [SerializeField] private string _savePath = "Assets/08_UI/MinimapImage.png";

    [ContextMenu("미니맵 이미지 캡처")]
    public void CaptureMapImage()
    {
        RenderTexture rt = new RenderTexture(_resolution, _resolution, 24);
        _minimapCamera.targetTexture = rt;
        _minimapCamera.Render();

        RenderTexture.active = rt;
        Texture2D tex = new Texture2D(_resolution, _resolution, TextureFormat.RGB24, false);
        tex.ReadPixels(new Rect(0, 0, _resolution, _resolution), 0, 0);
        tex.Apply();

        byte[] bytes = tex.EncodeToPNG();
        System.IO.File.WriteAllBytes(_savePath, bytes);

        _minimapCamera.targetTexture = null;
        RenderTexture.active = null;

#if UNITY_EDITOR
        DestroyImmediate(rt);
        DestroyImmediate(tex);
        AssetDatabase.Refresh();
        Debug.Log($"미니맵 이미지 저장 완료: {_savePath}");
#else
        Destroy(rt);
        Destroy(tex);
#endif
    }
}
