ImageViewerManager.cs
1.63 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
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 imageContainer;
public Image fullImage;
void Awake()
{
Instance = this;
imageContainer.SetActive(false);
var panelBtn = imageContainer.GetComponent<Button>();
panelBtn.onClick.AddListener(Hide);
}
public void ShowImage(string url)
{
//清空
fullImage.sprite = null;
imageContainer.SetActive(true);
StartCoroutine(ImageLoader.LoadImage(url, fullImage, (width, height) => {
Debug.Log($"图片原始宽高 = {width} x {height}");
float maxWidth = 1800f;
float maxHeight = 1000f;
float widthRatio = maxWidth / width;
float heightRatio = maxHeight / height;
// 取较小的缩放因子,保证不超出限制,并始终进行缩放(无论放大或缩小)
float scaleFactor = Mathf.Min(widthRatio, heightRatio);
float finalWidth = width * scaleFactor;
float finalHeight = height * scaleFactor;
fullImage.rectTransform.sizeDelta = new Vector2(finalWidth, finalHeight);
Debug.Log($"最终显示宽高 = {finalWidth} x {finalHeight}");
}));
}
public void Hide()
{
fullImage.sprite = null;
imageContainer.SetActive(false);
}
}