卡组优化(分帧加载)
This commit is contained in:
92
Assets/TcgEngine/Scripts/Menu/CardUILoading.cs
Normal file
92
Assets/TcgEngine/Scripts/Menu/CardUILoading.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TcgEngine.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 卡组分帧加载器
|
||||
/// </summary>
|
||||
public class CardUILoading : MonoBehaviour
|
||||
{
|
||||
public CardGrid grid_content;
|
||||
public GameObject card_prefab;
|
||||
|
||||
private List<CollectionCard> all_list = new List<CollectionCard>();
|
||||
|
||||
private bool spawned = false;
|
||||
|
||||
// 分帧加载
|
||||
private Coroutine spawnRoutine;
|
||||
|
||||
private static CardUILoading instance;
|
||||
|
||||
protected void Awake()
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
grid_content = CollectionPanel.Get().grid_content;
|
||||
card_prefab = CollectionPanel.Get().card_prefab;
|
||||
|
||||
if (!spawned)
|
||||
{
|
||||
if (spawnRoutine != null)
|
||||
StopCoroutine(spawnRoutine);
|
||||
spawnRoutine = StartCoroutine(SpawnCardsCoroutine());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private IEnumerator SpawnCardsCoroutine()
|
||||
{
|
||||
spawned = true;
|
||||
foreach (CollectionCard card in all_list)
|
||||
Destroy(card.gameObject);
|
||||
all_list.Clear();
|
||||
|
||||
// 首帧压力大,先让出 帧
|
||||
for (int i = 0; i < 5; i++)
|
||||
yield return null;
|
||||
|
||||
int batchSize = 15; // 每帧生成
|
||||
int counter = 0;
|
||||
|
||||
List<VariantData> variants = VariantData.GetAll();
|
||||
List<CardData> cards = CardData.GetAll();
|
||||
|
||||
foreach (VariantData variant in variants)
|
||||
{
|
||||
foreach (CardData card in cards)
|
||||
{
|
||||
GameObject nCard = Instantiate(card_prefab, grid_content.transform);
|
||||
CollectionCard dCard = nCard.GetComponent<CollectionCard>();
|
||||
dCard.SetCard(card, variant, 0);
|
||||
|
||||
all_list.Add(dCard);
|
||||
nCard.SetActive(false);
|
||||
|
||||
counter++;
|
||||
if (counter % batchSize == 0)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<CollectionCard> CardList()
|
||||
{
|
||||
return all_list;
|
||||
}
|
||||
|
||||
public static CardUILoading Get()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user