ImageViewerManager.cs 1.65 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 fullImage;
    public TextMeshProUGUI contentTextMeshProUGUI;
    public Image logoImage;
    public TextMeshProUGUI timeTextMeshProUGUI;

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

    public void ShowImage(string url,string content,string logo,string time)
    {
        panel.SetActive(true);
        StartCoroutine(ImageLoader.LoadImage(url, fullImage));
        StartCoroutine(ImageLoader.LoadImage(logo, logoImage));
        contentTextMeshProUGUI.text = content;
        timeTextMeshProUGUI.text = time;
        
        //初始高度180
        int initHeight = 180;
        RectTransform contentParentRect = contentTextMeshProUGUI.transform.parent.GetComponent<RectTransform>();
        contentParentRect.sizeDelta = new Vector2(contentParentRect.sizeDelta.x, initHeight);
        //动态设置高度,每行23个字
        int height =  (int)Math.Ceiling((double)content.Length / 23) * 65;
        if (height>initHeight)
        {
            contentParentRect.sizeDelta = new Vector2(contentParentRect.sizeDelta.x, height);
        }
        
    }

    public void Hide()
    {
        panel.SetActive(false);
    }
}