ImageViewerManager.cs
2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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);
}
}