ImageViewerManager.cs 3.79 KB
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
using TMPro;

/**
 * 功能描述: 图片放大组件
 * @author wangQc
 * @date 2025/6/4 09:17
 */
public class ImageViewerManager : MonoBehaviour
{
    public static ImageViewerManager Instance;
    public GameObject panel;
    
    public Image contentBg;

    public Image imageContainer;
    public Image fullImage;
    public TextMeshProUGUI contentTextMeshProUGUI;
    public Image logoImage;

        
    public Transform timeGrid; 
    public GameObject timePrefab;

    
    void Awake()
    {
        Instance = this;
        panel.SetActive(false);
        var panelBtn = panel.GetComponent<Button>();
        panelBtn.onClick.AddListener(Hide);
    }

    public void ShowImage(string url,string content,string logo,string time)
    {
        
        int initHeight = 180;
        float wordWidth = 40f; // 每字宽度
        float wordsPerLine = 1160 / wordWidth;

        int lineHeight = 75; // 每行高
        int totalLines = Mathf.CeilToInt(content.Length / wordsPerLine);

        int contentBgHeight = totalLines * lineHeight;

        if (contentBgHeight <= initHeight)
        {
            contentBgHeight = initHeight;
        }
        contentBg.rectTransform.sizeDelta = new Vector2(contentBg.rectTransform.sizeDelta.x, contentBgHeight + 20);
        contentTextMeshProUGUI.text = content;

        
        //清空
        fullImage.sprite = null;

        panel.SetActive(true);
        
        StartCoroutine(ImageLoader.LoadImage(url, fullImage, (width, height) => {
            Debug.Log($"图片原始宽高 = {width} x {height}");
            
            float targetWidth = 1160f;  // 目标参考宽度
            float maxHeight = 2000f;    // 高度最大不超过 2000

            // 原图比例
            float aspectRatio = (float)height / width;

            // 计算按目标宽度缩放后的高度
            float scaledHeight = targetWidth * aspectRatio;

            // 当前缩放系数(默认按 targetWidth 缩放)
            float scale = targetWidth / width;

            // 如果缩放后的高度 > 2000,高度优先限制
            if (scaledHeight > maxHeight)
            {
                // 按高度 2000 缩放
                scale = maxHeight / height;
            }
            // 如果图片原图宽度 < 1160,放大
            else if (width < targetWidth)
            {
                scale = targetWidth / width;
            }

            // 最终缩放后的宽高
            float finalWidth = width * scale;
            float finalHeight = height * scale;

            // 应用 sizeDelta
            imageContainer.rectTransform.sizeDelta = new Vector2(finalWidth, finalHeight);

            Debug.Log($"最终显示宽高 = {finalWidth} x {finalHeight}");

            // 加载 logo
            StartCoroutine(ImageLoader.LoadImage(logo, logoImage));
            //logo根据图片大小缩放比例
            // logoImage.rectTransform.sizeDelta = new Vector2(finalHeight * 0.2f, finalHeight * 0.1f);
        }));

        
        //时间
        foreach (Transform child in timeGrid)
        {
            Destroy(child.gameObject);
        }
        time = time.Replace("年", "p").Replace("月", "p").Replace("日", "");
        foreach (char c in time)
        {
            GameObject timeObj = Instantiate(timePrefab, timeGrid);
            var timeTextMeshPro = timeObj.GetComponent<TextMeshProUGUI>();
            
            timeTextMeshPro.text = "<sprite name=\""+c+"\">";
        }


        
    }

    public void Hide()
    {
        fullImage.sprite = null;
        panel.SetActive(false);
        //清空原来的时间
        foreach (Transform child in timeGrid)
        {
            Destroy(child.gameObject);
        }
    }
}