64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using Lean.Pool;
|
||
using UnityEngine.Serialization;
|
||
using UnityEngine.UI;
|
||
|
||
namespace Ichni.Editor
|
||
{
|
||
public class DynamicUIContainer : MonoBehaviour
|
||
{
|
||
public TMP_Text title;
|
||
public RectTransform rect;
|
||
public List<DynamicUISubcontainer> subcontainers;
|
||
|
||
public int gridWidth, gridHeight;
|
||
|
||
public void Initialize(string titleText)
|
||
{
|
||
// 支持对象池复用:重置 title 激活状态与内容
|
||
this.title.gameObject.SetActive(true);
|
||
this.title.text = titleText;
|
||
// 复用对象时 subcontainers 可能已存在,直接清空即可
|
||
if (this.subcontainers == null)
|
||
this.subcontainers = new List<DynamicUISubcontainer>();
|
||
else
|
||
this.subcontainers.Clear();
|
||
this.gridWidth = 0;
|
||
this.gridHeight = 0;
|
||
}
|
||
|
||
public DynamicUISubcontainer GenerateSubcontainer(int elementCountPerRow, float height = 100)
|
||
{
|
||
// 使用 LeanPool 替代 Instantiate,支持对象池复用
|
||
DynamicUISubcontainer subcontainer =
|
||
LeanPool.Spawn(EditorManager.instance.basePrefabs.dynamicUISubcontainer, rect)
|
||
.GetComponent<DynamicUISubcontainer>();
|
||
|
||
subcontainer.Initialize(this, elementCountPerRow, height);
|
||
subcontainers.Add(subcontainer);
|
||
return subcontainer;
|
||
}
|
||
|
||
public Vector2Int GetGrid(int elementCountPerRow, int heightUnit)
|
||
{
|
||
return new Vector2Int(600 / elementCountPerRow, 100 * heightUnit);
|
||
}
|
||
|
||
public void CheckGrid(Vector2Int newGrid)
|
||
{
|
||
if(newGrid.x > gridWidth)
|
||
{
|
||
gridWidth = newGrid.x;
|
||
}
|
||
|
||
if(newGrid.y > gridHeight)
|
||
{
|
||
gridHeight = newGrid.y;
|
||
}
|
||
}
|
||
}
|
||
} |