MomentManager.cs
3.33 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
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.IO;
//朋友圈动态管理器
public class MomentManager : MonoBehaviour
{
public Transform contentParent;
public GameObject momentPrefab;
public Image coverImage;
public Image bottomImage;
void Start()
{
StartCoroutine(JsonLoaderManager.LoadJson("https://h5.edcc.cn/FriendCircle/json/friend_data.json","Jsons/friend_data",data =>
{
Debug.Log("最终获取到的 JSON 数据:\n" + data);
InitFriendCircle(data);
}));
}
private void InitFriendCircle(string jsonText)
{
MomentList data = JsonUtility.FromJson<MomentList>(jsonText);
StartCoroutine(ImageLoader.LoadImage(data.cover, coverImage));
StartCoroutine(ImageLoader.LoadImage(data.bottom, bottomImage));
//计算列表高度
//设定顶部封面高度
float coverHeight = 978.125f;
//朋友圈列表高度
float momentListHeight = 0f;
//设定底部图片高度
float bottomHeight = 400f;
//记录第几条朋友圈,埋点用
int momentCount = 0;
foreach (var moment in data.moments)
{
GameObject item = Instantiate(momentPrefab, contentParent);
int momentHeight = item.GetComponent<MomentItem>().Setup(moment,data.likeList,data.logo,++momentCount);
RectTransform momentRt = item.GetComponent<RectTransform>();
momentRt.sizeDelta = new Vector2(momentRt.sizeDelta.x, momentHeight);
//计算列表高度
// RectTransform rt = item.GetComponent<RectTransform>();
// momentListHeight+= rt.rect.height;
momentListHeight+= momentHeight;
}
//设定顶部封面高度
RectTransform coverRt = coverImage.GetComponent<RectTransform>();
coverRt.sizeDelta = new Vector2(coverRt.sizeDelta.x, coverHeight);
//设置列表高度
RectTransform momentListRt = contentParent.GetComponent<RectTransform>();
momentListRt.sizeDelta = new Vector2(momentListRt.sizeDelta.x, momentListHeight);
//设定底部封面高度
RectTransform bottomRt = bottomImage.GetComponent<RectTransform>();
bottomRt.sizeDelta = new Vector2(bottomRt.sizeDelta.x, bottomHeight);
//设置外层ScrollContent层高度
RectTransform scrollContentRt = contentParent.parent.GetComponent<RectTransform>();
scrollContentRt.sizeDelta = new Vector2(scrollContentRt.sizeDelta.x, coverHeight + momentListHeight + bottomHeight);
}
private void OnApplicationQuit()
{
ClearTemporaryCache();
}
private void ClearTemporaryCache()
{
string cachePath = Application.temporaryCachePath;
if (Directory.Exists(cachePath))
{
try
{
Directory.Delete(cachePath, true);
Debug.Log($"[CacheCleaner] 清空缓存目录成功: {cachePath}");
}
catch (System.Exception e)
{
Debug.LogError($"[CacheCleaner] 清空缓存目录失败: {e.Message}");
}
}
else
{
Debug.Log($"[CacheCleaner] 缓存目录不存在: {cachePath}");
}
}
}