ImageViewerManager.cs
2.15 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
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 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)
{
panel.SetActive(true);
StartCoroutine(ImageLoader.LoadImage(url, fullImage));
StartCoroutine(ImageLoader.LoadImage(logo, logoImage));
contentTextMeshProUGUI.text = content;
//时间
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+"\">";
}
//初始高度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);
}
}