200 lines
6.2 KiB
C#
200 lines
6.2 KiB
C#
using SK.Framework;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
|
||
/// <summary>
|
||
/// UI工具管理类,通用方法
|
||
/// </summary>
|
||
public class UIUtils : MonoSingleton<UIUtils>
|
||
{
|
||
|
||
private List<PhaseObjectiveItem> phaseObjectiveItems = new List<PhaseObjectiveItem>();
|
||
|
||
#region ScrollView部分
|
||
|
||
/// <summary>
|
||
/// 向滚动视图添加子项。
|
||
/// </summary>
|
||
/// <param name="item"></param>
|
||
/// <param name="scrollViewContent"></param>
|
||
public void AddItemToScrollView(RectTransform item, RectTransform scrollViewContent)
|
||
{
|
||
GameObject newItem = Instantiate(item.gameObject, scrollViewContent.transform);
|
||
newItem.Activate();
|
||
|
||
if (newItem.GetComponent<PhaseObjectiveItem>() != null)
|
||
{
|
||
phaseObjectiveItems.Add(newItem.GetComponent<PhaseObjectiveItem>());
|
||
newItem.GetComponent<PhaseObjectiveItem>().deleteBtn.onClick.AddListener(() =>
|
||
{
|
||
phaseObjectiveItems.Remove(newItem.GetComponent<PhaseObjectiveItem>());
|
||
RefreshAll();
|
||
Destroy(newItem);
|
||
});
|
||
|
||
}
|
||
|
||
|
||
AdjustContentLength(scrollViewContent);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调整内容长度以适应子项数量和布局设置。
|
||
/// 计算内容长度,以便在滚动视图中正确显示所有子项。
|
||
/// </summary>
|
||
/// <param name="contentRect"></param>
|
||
public void AdjustContentLength(RectTransform contentRect)
|
||
{
|
||
GridLayoutGroup gridLayout = contentRect.GetComponent<GridLayoutGroup>();
|
||
int childCount = contentRect.childCount;
|
||
if (childCount == 0) return;
|
||
|
||
Vector2 cellSize = gridLayout.cellSize;
|
||
Vector2 spacing = gridLayout.spacing;
|
||
GridLayoutGroup.Constraint constraint = gridLayout.constraint;
|
||
int constraintCount = gridLayout.constraintCount;
|
||
GridLayoutGroup.Axis startAxis = gridLayout.startAxis;
|
||
|
||
int rows = 1;
|
||
int columns = 1;
|
||
|
||
// 根据Grid Layout Group设置计算行和列
|
||
if (startAxis == GridLayoutGroup.Axis.Vertical) // 垂直排列
|
||
{
|
||
switch (constraint)
|
||
{
|
||
case GridLayoutGroup.Constraint.FixedColumnCount:
|
||
columns = constraintCount;
|
||
rows = Mathf.CeilToInt((float)childCount / columns);
|
||
break;
|
||
case GridLayoutGroup.Constraint.FixedRowCount:
|
||
rows = constraintCount;
|
||
columns = Mathf.CeilToInt((float)childCount / rows);
|
||
break;
|
||
case GridLayoutGroup.Constraint.Flexible: // Flexible模式
|
||
float contentWidth = contentRect.rect.width;
|
||
if (contentWidth > 0)
|
||
{
|
||
columns = Mathf.FloorToInt((contentWidth + spacing.x) / (cellSize.x + spacing.x));
|
||
if (columns < 1) columns = 1;
|
||
rows = Mathf.CeilToInt((float)childCount / columns);
|
||
}
|
||
else
|
||
{
|
||
columns = 1;
|
||
rows = childCount;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
else // 水平排列
|
||
{
|
||
switch (constraint)
|
||
{
|
||
case GridLayoutGroup.Constraint.FixedColumnCount:
|
||
columns = constraintCount;
|
||
rows = Mathf.CeilToInt((float)childCount / columns);
|
||
break;
|
||
case GridLayoutGroup.Constraint.FixedRowCount:
|
||
rows = constraintCount;
|
||
columns = Mathf.CeilToInt((float)childCount / rows);
|
||
break;
|
||
case GridLayoutGroup.Constraint.Flexible: // Flexible模式
|
||
float contentHeight = contentRect.rect.height;
|
||
if (contentHeight > 0)
|
||
{
|
||
rows = Mathf.FloorToInt((contentHeight + spacing.y) / (cellSize.y + spacing.y));
|
||
if (rows < 1) rows = 1;
|
||
columns = Mathf.CeilToInt((float)childCount / rows);
|
||
}
|
||
else
|
||
{
|
||
rows = 1;
|
||
columns = childCount;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 计算所需Content尺寸
|
||
float requiredWidth = columns * cellSize.x + Mathf.Max(0, columns - 1) * spacing.x;
|
||
float requiredHeight = rows * cellSize.y + Mathf.Max(0, rows - 1) * spacing.y;
|
||
|
||
// 更新Content大小
|
||
contentRect.sizeDelta = new Vector2(requiredWidth, requiredHeight);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region InputField部分
|
||
|
||
/// <summary>
|
||
/// 检查输入框
|
||
/// </summary>
|
||
/// <param name="inputField"></param>
|
||
public bool IsInputCorrect(InputField inputField)
|
||
{
|
||
if (string.IsNullOrEmpty(inputField.text) || string.IsNullOrWhiteSpace(inputField.text))
|
||
{
|
||
//ShowWarming(inputField.transform.GetChild(0).GetComponent<Text>().text);
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
#endregion
|
||
|
||
private void RefreshAll()
|
||
{
|
||
StartCoroutine(LateRefresh());
|
||
}
|
||
|
||
private IEnumerator LateRefresh()
|
||
{
|
||
yield return new WaitForEndOfFrame();
|
||
for (int i = 0; i < phaseObjectiveItems.Count; i++)
|
||
{
|
||
phaseObjectiveItems[i].RefreshWhileDelete();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// UUid
|
||
/// </summary>
|
||
private string uuid;
|
||
public string GetUuid() { return uuid; }
|
||
|
||
/// <summary>
|
||
/// 项目名称
|
||
/// </summary>
|
||
public string ProjectName
|
||
{ get; set; }
|
||
|
||
/// <summary>
|
||
/// 项目服务对象
|
||
/// </summary>
|
||
public string ProjectServiceTarget
|
||
{ get; set; }
|
||
|
||
/// <summary>
|
||
/// 项目目标
|
||
/// </summary>
|
||
public float ProjectTarget
|
||
{ get; set; }
|
||
|
||
/// <summary>
|
||
/// 项目周期
|
||
/// </summary>
|
||
public string ProjectPeriod
|
||
{ get; set; }
|
||
|
||
protected override void Initialize()
|
||
{
|
||
uuid = System.Guid.NewGuid().ToString();
|
||
|
||
phaseObjectiveItems.Clear();
|
||
}
|
||
} |