using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; public class itemGetPrefab : MonoBehaviour { [Header("item prefab")] public GameObject itemPrefab; public Transform displayParent; [Header("rarity")] public ItemRarityColorConfigSO rarityColorConfig; [Header("btn")] public Button close; private readonly List spawnedItems = new List(); private Coroutine layoutRefreshRoutine; private void Awake() { if (close != null) { close.onClick.RemoveListener(CloseSelf); close.onClick.AddListener(CloseSelf); } } private void OnDestroy() { if (close != null) { close.onClick.RemoveListener(CloseSelf); } } public void ClearItems() { for (int i = 0; i < spawnedItems.Count; i++) { if (spawnedItems[i] != null) { Destroy(spawnedItems[i]); } } spawnedItems.Clear(); if (displayParent != null) { for (int i = displayParent.childCount - 1; i >= 0; i--) { Destroy(displayParent.GetChild(i).gameObject); } } RequestLayoutRebuild(); } public get_item_prefab AddItem() { if (itemPrefab == null || displayParent == null) { return null; } GameObject instance = Instantiate(itemPrefab, displayParent); spawnedItems.Add(instance); UI_OrderedEntryAnimator.PlayFadeOnly(instance, spawnedItems.Count - 1, 0.2f, 0.025f, true); RequestLayoutRebuild(); return instance.GetComponent(); } private void RequestLayoutRebuild() { if (displayParent == null) { return; } if (layoutRefreshRoutine != null) { StopCoroutine(layoutRefreshRoutine); layoutRefreshRoutine = null; } if (isActiveAndEnabled) { layoutRefreshRoutine = StartCoroutine(RebuildLayoutNextFrame()); return; } RebuildLayoutNow(); } private System.Collections.IEnumerator RebuildLayoutNextFrame() { yield return null; RebuildLayoutNow(); layoutRefreshRoutine = null; } private void RebuildLayoutNow() { RectTransform rect = displayParent as RectTransform; if (rect == null) { return; } Canvas.ForceUpdateCanvases(); LayoutRebuilder.ForceRebuildLayoutImmediate(rect); Canvas.ForceUpdateCanvases(); } public void CloseSelf() { UI_PanelExitUtility.PlayExitThen(gameObject, () => { if (gameObject != null) { Destroy(gameObject); } }); } }