From 47e7bbf2860d27fb12d6876a860abc46b380e7b9 Mon Sep 17 00:00:00 2001
From: lcy <1459594991@qq.com>
Date: 星期五, 27 二月 2026 17:26:30 +0800
Subject: [PATCH] 492 武将登场-客户端 主键界面右侧按钮 支持指定某个入口固定放在某个固定位置,以及多个入口竞争同一个固定位置
---
Main/System/Main/HomeGridLayout.cs.meta | 11 ++
Main/System/Main/HomeGridLayout.cs | 144 ++++++++++++++++++++++++++++++++++++
Main/System/Main/HomeGridLayoutCell.cs | 41 ++++++++++
Main/System/Main/HomeGridLayoutCell.cs.meta | 11 ++
4 files changed, 207 insertions(+), 0 deletions(-)
diff --git a/Main/System/Main/HomeGridLayout.cs b/Main/System/Main/HomeGridLayout.cs
new file mode 100644
index 0000000..141a92c
--- /dev/null
+++ b/Main/System/Main/HomeGridLayout.cs
@@ -0,0 +1,144 @@
+using UnityEngine;
+using System.Collections.Generic;
+
+[ExecuteAlways]
+[RequireComponent(typeof(RectTransform))]
+public class HomeGridLayout : MonoBehaviour
+{
+ public RectOffset padding = new RectOffset(); // 杈硅窛
+ public Vector2 cellSize = new Vector2(100f, 100f); // 鍗曞厓鏍煎ぇ灏�
+ public Vector2 spacing = new Vector2(10f, 10f); // 鍏冪礌闂磋窛
+ public int rows = 7; // 鍥哄畾琛屾暟
+
+ private void OnRectTransformDimensionsChange() => UpdateLayout();
+ private void OnTransformChildrenChanged() => UpdateLayout();
+
+#if UNITY_EDITOR
+ private void Update()
+ {
+ if (!Application.isPlaying) UpdateLayout();
+ }
+#endif
+
+ public void UpdateLayout()
+ {
+ if (rows <= 0 || transform.childCount == 0) return;
+
+ List<RectTransform> flowChildren = new List<RectTransform>();
+ List<RectTransform> fixedChildren = new List<RectTransform>();
+
+ // 1. 閬嶅巻鏀堕泦骞跺垎绫绘墍鏈夋縺娲荤殑瀛愮墿浣�
+ for (int i = 0; i < transform.childCount; i++)
+ {
+ RectTransform child = transform.GetChild(i) as RectTransform;
+ if (child == null || !child.gameObject.activeSelf) continue;
+
+ HomeGridLayoutCell cell = child.GetComponent<HomeGridLayoutCell>();
+
+ if (cell != null && cell.isFixedPosition)
+ fixedChildren.Add(child);
+ else
+ flowChildren.Add(child);
+ }
+
+ // 2. 鎺掑簭娴佸姩鐗╀綋 (鎸� sortIndex)
+ flowChildren.Sort((a, b) =>
+ {
+ var cellA = a.GetComponent<HomeGridLayoutCell>();
+ var cellB = b.GetComponent<HomeGridLayoutCell>();
+ int indexA = cellA != null ? cellA.sortIndex : int.MaxValue;
+ int indexB = cellB != null ? cellB.sortIndex : int.MaxValue;
+ return indexA.CompareTo(indexB);
+ });
+
+ // 3. 鎺掑簭鍥哄畾鐗╀綋 (浼樺厛 sortIndex锛屽叾娆� subSortIndex)
+ fixedChildren.Sort((a, b) =>
+ {
+ var cellA = a.GetComponent<HomeGridLayoutCell>();
+ var cellB = b.GetComponent<HomeGridLayoutCell>();
+
+ int sortA = cellA != null ? cellA.sortIndex : int.MaxValue;
+ int sortB = cellB != null ? cellB.sortIndex : int.MaxValue;
+
+ if (sortA != sortB) return sortA.CompareTo(sortB);
+
+ int subA = cellA != null ? cellA.subSortIndex : int.MaxValue;
+ int subB = cellB != null ? cellB.subSortIndex : int.MaxValue;
+ return subA.CompareTo(subB);
+ });
+
+ // 4. 寮�濮嬪垎閰嶇綉鏍� (鏍稿績浼樺寲閮ㄥ垎)
+ int currentGridIndex = 0; // 褰撳墠鎺ㄦ紨鍒扮殑鐪熷疄缃戞牸鍧戜綅
+ int flowIndex = 0;
+ int fixedIndex = 0;
+ int totalValidChildren = flowChildren.Count + fixedChildren.Count;
+
+ for (int i = 0; i < totalValidChildren; i++)
+ {
+ RectTransform targetChild = null;
+ HomeGridLayoutCell nextFixedCell = null;
+
+ if (fixedIndex < fixedChildren.Count)
+ {
+ nextFixedCell = fixedChildren[fixedIndex].GetComponent<HomeGridLayoutCell>();
+ }
+
+ // 鍒嗘敮 A锛氬浐瀹氱墿浣撳凡缁忓埌浜嗗畠鏈熸湜鐨勭綉鏍间綅缃紙鎴栧凡缁忚鍓嶉潰鐨勫厓绱犳尋鍒颁簡褰撳墠浣嶇疆锛�
+ if (nextFixedCell != null && nextFixedCell.sortIndex <= currentGridIndex)
+ {
+ targetChild = fixedChildren[fixedIndex];
+ fixedIndex++;
+ }
+ // 鍒嗘敮 B锛氬綋鍓嶄綅缃病鏈夊浐瀹氱墿浣撴姠鍗狅紝涓旇繕鏈夋祦鍔ㄧ墿浣撴帓闃燂紝璁╂祦鍔ㄧ墿浣撳~琛ョ┖缂�
+ else if (flowIndex < flowChildren.Count)
+ {
+ targetChild = flowChildren[flowIndex];
+ flowIndex++;
+ }
+ // 鍒嗘敮 C锛氭病鏈夋祦鍔ㄧ墿浣撳~琛ョ┖缂轰簡锛岀洿鎺ヨ缃戞牸绱㈠紩鈥滃揩杩涒�濆埌涓嬩竴涓浐瀹氱墿浣撶殑浣嶇疆
+ else if (nextFixedCell != null)
+ {
+ currentGridIndex = nextFixedCell.sortIndex; // 蹇繘璺宠繃涓棿鐨勬墍鏈夌┖鐧芥牸瀛�
+ targetChild = fixedChildren[fixedIndex];
+ fixedIndex++;
+ }
+
+ // 搴旂敤璁$畻濂界殑缃戞牸鍧愭爣
+ if (targetChild != null)
+ {
+ SetChildTransform(targetChild, currentGridIndex);
+ currentGridIndex++; // 鍗犱綅鎴愬姛锛屽潙浣嶅悗绉�
+ }
+ }
+ }
+
+ /// <summary>
+ /// 灏嗗瓙鐗╀綋鏀剧疆鍒版寚瀹氱殑缃戞牸绱㈠紩浣嶇疆
+ /// </summary>
+ private void SetChildTransform(RectTransform child, int gridIndex)
+ {
+ int col = gridIndex / rows;
+ int row = gridIndex % rows;
+
+ // 1. 鍏堣绠楀嚭濡傛灉杞村績鍦ㄥ彸涓婅鏃剁殑鐞嗚杈圭紭鍧愭爣
+ float edgeXPos = -padding.right - col * (cellSize.x + spacing.x);
+ float edgeYPos = -padding.top - row * (cellSize.y + spacing.y);
+
+ // 2. 涓轰簡娑堥櫎鍥㈤槦娼滆鍒欙紝鎴戜滑灏嗘牸瀛愮殑杞村績鏀瑰洖姝d腑蹇� (0.5, 0.5)
+ // 鍥犳瀹為檯鍧愭爣闇�瑕佸悜宸︺�佸悜涓嬪啀鍋忕Щ鍗婁釜鍗曞厓鏍肩殑澶у皬
+ float centerXPos = edgeXPos - (cellSize.x * 0.5f);
+ float centerYPos = edgeYPos - (cellSize.y * 0.5f);
+
+ // 缁熶竴閿氱偣涓哄彸涓婅 (1, 1)
+ child.anchorMin = new Vector2(1, 1);
+ child.anchorMax = new Vector2(1, 1);
+
+ // 杞村績锛圥ivot锛夋敼涓烘涓績锛�
+ child.pivot = new Vector2(0.5f, 0.5f);
+
+ child.sizeDelta = cellSize;
+
+ // 璧嬩簣涓績鍧愭爣
+ child.anchoredPosition = new Vector2(centerXPos, centerYPos);
+ }
+}
\ No newline at end of file
diff --git a/Main/System/Main/HomeGridLayout.cs.meta b/Main/System/Main/HomeGridLayout.cs.meta
new file mode 100644
index 0000000..0839346
--- /dev/null
+++ b/Main/System/Main/HomeGridLayout.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: b7ec546648dc7bc4297af70316bca133
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Main/System/Main/HomeGridLayoutCell.cs b/Main/System/Main/HomeGridLayoutCell.cs
new file mode 100644
index 0000000..a6fc2d1
--- /dev/null
+++ b/Main/System/Main/HomeGridLayoutCell.cs
@@ -0,0 +1,41 @@
+using UnityEngine;
+
+public class HomeGridLayoutCell : MonoBehaviour
+{
+ [Header("妯″紡")]
+ [Tooltip("鍕鹃�夊垯鍥哄畾鍦ㄧ洰鏍囨牸瀛愪笂鍗犱綅锛涗笉鍕惧垯浣滀负娴佹按鍏冪礌鑷姩濉┖銆�")]
+ public bool isFixedPosition;
+
+ [Header("鎺掑簭")]
+ [Tooltip("鍐冲畾鍥哄畾鍏冪礌鐨勬墍鍦ㄦ牸瀛�,鎴栨祦姘村厓绱犵殑鍑哄満椤哄簭(娉ㄦ剰浠�0寮�濮嬭)銆�")]
+ public int sortIndex;
+
+ [Tooltip("褰撳涓厓绱犳姠鍗犲悓涓�涓牸瀛愭垨鎺掑簭鐩稿悓鏃讹紝璇ュ�艰秺灏忚秺浼樺厛銆�")]
+ public int subSortIndex;
+
+ private void OnEnable()
+ {
+ NotifyParentToUpdate();
+ }
+
+ private void OnDisable()
+ {
+ NotifyParentToUpdate();
+ }
+
+ /// <summary>
+ /// 閫氱煡鐖剁骇鐨勭綉鏍艰剼鏈噸鏂版帓鐗�
+ /// </summary>
+ private void NotifyParentToUpdate()
+ {
+ // 濡傛灉鐗╀綋琚攢姣佹垨鑰呮病鏈夌埗绾т簡锛屽氨涓嶇浜�
+ if (transform.parent == null)
+ return;
+
+ HomeGridLayout layout = transform.parent.GetComponent<HomeGridLayout>();
+ if (layout != null)
+ {
+ layout.UpdateLayout();
+ }
+ }
+}
\ No newline at end of file
diff --git a/Main/System/Main/HomeGridLayoutCell.cs.meta b/Main/System/Main/HomeGridLayoutCell.cs.meta
new file mode 100644
index 0000000..cb587da
--- /dev/null
+++ b/Main/System/Main/HomeGridLayoutCell.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 28e84dc55c523d64bbeb2631a3355c73
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
--
Gitblit v1.8.0