MomentManager.cs 3.33 KB
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}");
        }
    }
}