using SK.Framework; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// /// UI工具管理类,通用方法 /// public class UIUtils : MonoSingleton { private List phaseObjectiveItems = new List(); #region ScrollView部分 /// /// 向滚动视图添加子项。 /// /// /// public void AddItemToScrollView(RectTransform item, RectTransform scrollViewContent) { GameObject newItem = Instantiate(item.gameObject, scrollViewContent.transform); newItem.Activate(); if (newItem.GetComponent() != null) { phaseObjectiveItems.Add(newItem.GetComponent()); newItem.GetComponent().deleteBtn.onClick.AddListener(() => { phaseObjectiveItems.Remove(newItem.GetComponent()); RefreshAll(); Destroy(newItem); }); } AdjustContentLength(scrollViewContent); } /// /// 调整内容长度以适应子项数量和布局设置。 /// 计算内容长度,以便在滚动视图中正确显示所有子项。 /// /// public void AdjustContentLength(RectTransform contentRect) { GridLayoutGroup gridLayout = contentRect.GetComponent(); 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部分 /// /// 检查输入框 /// /// public bool IsInputCorrect(InputField inputField) { if (string.IsNullOrEmpty(inputField.text) || string.IsNullOrWhiteSpace(inputField.text)) { //ShowWarming(inputField.transform.GetChild(0).GetComponent().text); return false; } return true; } #endregion public GameObject particalObj; public void SetParticalActive(bool isActive) { particalObj.SetActive(isActive); } private void RefreshAll() { StartCoroutine(LateRefresh()); } private IEnumerator LateRefresh() { yield return new WaitForEndOfFrame(); for (int i = 0; i < phaseObjectiveItems.Count; i++) { phaseObjectiveItems[i].RefreshWhileDelete(); } } /// /// UUid /// private string uuid; public string GetUuid() { return uuid; } /// /// 项目名称 /// public string ProjectName { get; set; } /// /// 项目服务对象 /// public string ProjectServiceTarget { get; set; } /// /// 项目目标 /// public float ProjectTarget { get; set; } /// /// 项目周期 /// public string ProjectPeriod { get; set; } protected override void Initialize() { uuid = System.Guid.NewGuid().ToString(); phaseObjectiveItems.Clear(); } }