ImageViewerManager.cs 2.67 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(() =>
        {
            fullImage.sprite = null;
            panel.SetActive(false);
        });
    }

    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}");
            
            //三倍图
            width /= 2;
            height /= 2;
            imageContainer.rectTransform.sizeDelta = new Vector2(width, height);
            
            StartCoroutine(ImageLoader.LoadImage(logo, logoImage));
        
        
            //时间
            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()
    {
        panel.SetActive(false);
    }
}