ImageViewerManager.cs
3.71 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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;
int lineHeight = 75; // 每行高
int totalLines = Mathf.CeilToInt(content.Length / 30f);//每行30个字数
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);
}
}
}