表格添加四叉树渲染优化逻辑。
This commit is contained in:
@@ -176,18 +176,26 @@ namespace XericUIEditor.XTable
|
||||
[MenuItem("GameObject/Xeric UI/Table/Xeric UI Action Table", false, 60)]
|
||||
private static void CreateTable(MenuCommand menuCommand)
|
||||
{
|
||||
// 1. 创建表格内容节点
|
||||
GameObject contentGo = new GameObject("Content",
|
||||
typeof(RectTransform), typeof(CanvasRenderer), typeof(XericUIActionTable));
|
||||
// 1. 创建根节点:ScrollRect + Image + XericUIActionTable 共存于同一 GameObject
|
||||
GameObject rootGo = new GameObject("Xeric UI Action Table",
|
||||
typeof(RectTransform), typeof(ScrollRect), typeof(Image), typeof(XericUIActionTable));
|
||||
RectTransform rootRt = rootGo.GetComponent<RectTransform>();
|
||||
rootRt.sizeDelta = new Vector2(800, 600);
|
||||
rootGo.GetComponent<Image>().color = new Color(0.1f, 0.1f, 0.1f, 0.3f);
|
||||
|
||||
ScrollRect scrollRect = rootGo.GetComponent<ScrollRect>();
|
||||
XericUIActionTable table = rootGo.GetComponent<XericUIActionTable>();
|
||||
|
||||
// 2. 创建 Content(纯 RectTransform,由表格组件管理动态子对象)
|
||||
GameObject contentGo = new GameObject("Content", typeof(RectTransform));
|
||||
RectTransform contentRt = contentGo.GetComponent<RectTransform>();
|
||||
contentRt.anchorMin = new Vector2(0, 1);
|
||||
contentRt.anchorMax = new Vector2(0, 1);
|
||||
contentRt.pivot = new Vector2(0, 1);
|
||||
contentRt.anchoredPosition = Vector2.zero;
|
||||
contentRt.sizeDelta = new Vector2(800, 600);
|
||||
XericUIActionTable table = contentGo.GetComponent<XericUIActionTable>();
|
||||
|
||||
// 2. 创建 Viewport(Mask 裁剪区域)
|
||||
// 3. 创建 Viewport(Mask 裁剪区域)
|
||||
GameObject viewportGo = new GameObject("Viewport",
|
||||
typeof(RectTransform), typeof(Image), typeof(Mask));
|
||||
viewportGo.GetComponent<Image>().color = new Color(1, 1, 1, 0.01f);
|
||||
@@ -197,7 +205,7 @@ namespace XericUIEditor.XTable
|
||||
vpRt.sizeDelta = Vector2.zero;
|
||||
contentRt.SetParent(vpRt, false);
|
||||
|
||||
// 3. 创建水平滚动条
|
||||
// 4. 创建水平滚动条
|
||||
GameObject hScrollbarGo = new GameObject("Scrollbar Horizontal",
|
||||
typeof(RectTransform), typeof(Scrollbar), typeof(Image));
|
||||
RectTransform hBarRt = hScrollbarGo.GetComponent<RectTransform>();
|
||||
@@ -217,7 +225,7 @@ namespace XericUIEditor.XTable
|
||||
RectTransform hSlidingRt = hSlidingGo.GetComponent<RectTransform>();
|
||||
hSlidingRt.anchorMin = Vector2.zero;
|
||||
hSlidingRt.anchorMax = Vector2.one;
|
||||
hSlidingRt.sizeDelta = new Vector2(-20, -20);
|
||||
hSlidingRt.sizeDelta = new Vector2(-20, 0);
|
||||
|
||||
GameObject hHandleGo = new GameObject("Handle",
|
||||
typeof(RectTransform), typeof(Image));
|
||||
@@ -225,11 +233,11 @@ namespace XericUIEditor.XTable
|
||||
RectTransform hHandleRt = hHandleGo.GetComponent<RectTransform>();
|
||||
hHandleRt.anchorMin = Vector2.zero;
|
||||
hHandleRt.anchorMax = Vector2.one;
|
||||
hHandleRt.sizeDelta = new Vector2(-20, -20);
|
||||
hHandleRt.sizeDelta = new Vector2(-10, -10);
|
||||
hHandleGo.GetComponent<Image>().color = new Color(0.4f, 0.4f, 0.4f, 0.8f);
|
||||
hBar.handleRect = hHandleRt;
|
||||
|
||||
// 4. 创建垂直滚动条
|
||||
// 5. 创建垂直滚动条
|
||||
GameObject vScrollbarGo = new GameObject("Scrollbar Vertical",
|
||||
typeof(RectTransform), typeof(Scrollbar), typeof(Image));
|
||||
RectTransform vBarRt = vScrollbarGo.GetComponent<RectTransform>();
|
||||
@@ -249,7 +257,7 @@ namespace XericUIEditor.XTable
|
||||
RectTransform vSlidingRt = vSlidingGo.GetComponent<RectTransform>();
|
||||
vSlidingRt.anchorMin = Vector2.zero;
|
||||
vSlidingRt.anchorMax = Vector2.one;
|
||||
vSlidingRt.sizeDelta = new Vector2(-20, -20);
|
||||
vSlidingRt.sizeDelta = new Vector2(0, -20);
|
||||
|
||||
GameObject vHandleGo = new GameObject("Handle",
|
||||
typeof(RectTransform), typeof(Image));
|
||||
@@ -257,18 +265,11 @@ namespace XericUIEditor.XTable
|
||||
RectTransform vHandleRt = vHandleGo.GetComponent<RectTransform>();
|
||||
vHandleRt.anchorMin = Vector2.zero;
|
||||
vHandleRt.anchorMax = Vector2.one;
|
||||
vHandleRt.sizeDelta = new Vector2(-20, -20);
|
||||
vHandleRt.sizeDelta = new Vector2(-10, -10);
|
||||
vHandleGo.GetComponent<Image>().color = new Color(0.4f, 0.4f, 0.4f, 0.8f);
|
||||
vBar.handleRect = vHandleRt;
|
||||
|
||||
// 5. 创建 ScrollView 根节点
|
||||
GameObject scrollGo = new GameObject("Xeric UI Action Table",
|
||||
typeof(RectTransform), typeof(ScrollRect), typeof(Image));
|
||||
RectTransform scrollRt = scrollGo.GetComponent<RectTransform>();
|
||||
scrollRt.sizeDelta = new Vector2(800, 600);
|
||||
scrollGo.GetComponent<Image>().color = new Color(0.1f, 0.1f, 0.1f, 0.3f);
|
||||
|
||||
ScrollRect scrollRect = scrollGo.GetComponent<ScrollRect>();
|
||||
// 6. 配置 ScrollRect
|
||||
scrollRect.content = contentRt;
|
||||
scrollRect.viewport = vpRt;
|
||||
scrollRect.horizontalScrollbar = hBar;
|
||||
@@ -278,11 +279,11 @@ namespace XericUIEditor.XTable
|
||||
scrollRect.movementType = ScrollRect.MovementType.Clamped;
|
||||
|
||||
// 组装层级
|
||||
vpRt.SetParent(scrollRt, false);
|
||||
hScrollbarGo.transform.SetParent(scrollRt, false);
|
||||
vScrollbarGo.transform.SetParent(scrollRt, false);
|
||||
vpRt.SetParent(rootRt, false);
|
||||
hScrollbarGo.transform.SetParent(rootRt, false);
|
||||
vScrollbarGo.transform.SetParent(rootRt, false);
|
||||
|
||||
// 绑定 ScrollRect 到表格(通过 SerializedObject 正确回写)
|
||||
// 绑定 ScrollRect 到表格
|
||||
using (SerializedObject so = new SerializedObject(table))
|
||||
{
|
||||
SerializedProperty prop = so.FindProperty("m_ScrollRect");
|
||||
@@ -295,10 +296,10 @@ namespace XericUIEditor.XTable
|
||||
|
||||
// 挂载到 Canvas
|
||||
GameObject parent = GetOrCreateCanvas();
|
||||
scrollRt.SetParent(parent.transform, false);
|
||||
rootRt.SetParent(parent.transform, false);
|
||||
|
||||
Undo.RegisterCreatedObjectUndo(scrollGo, "Create Xeric UI Action Table");
|
||||
Selection.activeGameObject = scrollGo;
|
||||
Undo.RegisterCreatedObjectUndo(rootGo, "Create Xeric UI Action Table");
|
||||
Selection.activeGameObject = rootGo;
|
||||
}
|
||||
|
||||
private static GameObject GetOrCreateCanvas()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using UnityEditor;
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
using XericLibrary.Runtime.MacroLibrary;
|
||||
using XericLibrary.Runtime.SuperStyleSheet;
|
||||
using XericUI.XTable.Core;
|
||||
using XericUI.XTable.Mapping;
|
||||
@@ -179,7 +179,9 @@ namespace XericUIEditor.XTable
|
||||
Canvas.ForceUpdateCanvases();
|
||||
#endif
|
||||
|
||||
RectTransform tableRt = table.GetComponent<RectTransform>();
|
||||
// 使用 Content 的 RectTransform 进行定位计算(而非表格组件所在的 ScrollRect 根节点)
|
||||
RectTransform tableRt = table.ContentTransform.RectTransform();
|
||||
if (tableRt == null) tableRt = table.GetComponent<RectTransform>();
|
||||
float[] rh = table.RowHeights;
|
||||
float[] cw = table.ColWidths;
|
||||
if (rh == null || cw == null || rh.Length == 0 || cw.Length == 0) return;
|
||||
@@ -361,31 +363,32 @@ namespace XericUIEditor.XTable
|
||||
s_DragStartCol = col;
|
||||
|
||||
if (Event.current.shift && table.SelectedRow >= 0)
|
||||
{
|
||||
if (!table.HasMultiSelection)
|
||||
table.StartMultiSelect(table.SelectedRow, table.SelectedCol);
|
||||
table.ExtendMultiSelect(r, col);
|
||||
}
|
||||
{
|
||||
if (!table.HasMultiSelection)
|
||||
table.SelectCell(table.SelectedRow, table.SelectedCol, r, col);
|
||||
else
|
||||
{
|
||||
table.StartMultiSelect(r, col);
|
||||
}
|
||||
|
||||
table.RefreshView();
|
||||
SyncEditText(table);
|
||||
Event.current.Use();
|
||||
SceneView.RepaintAll();
|
||||
table.SelectionHandle.ExtendTo(r, col);
|
||||
}
|
||||
else
|
||||
{
|
||||
table.SelectCell(r, col);
|
||||
}
|
||||
|
||||
y += cellH;
|
||||
table.RefreshView();
|
||||
SyncEditText(table);
|
||||
Event.current.Use();
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
|
||||
y += cellH;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region 列标签
|
||||
#region 列标签
|
||||
|
||||
private static void DrawColumnLabels(float[] cw, Rect tableRect,
|
||||
private static void DrawColumnLabels(float[] cw, Rect tableRect,
|
||||
float xScale, float yScale, XericUIActionTable table)
|
||||
{
|
||||
var style = new GUIStyle(GUI.skin.label)
|
||||
@@ -436,25 +439,26 @@ namespace XericUIEditor.XTable
|
||||
s_DragStartCol = c;
|
||||
|
||||
if (Event.current.shift && table.SelectedCol >= 0)
|
||||
{
|
||||
if (!table.HasMultiSelection)
|
||||
table.StartMultiSelect(table.SelectedRow, table.SelectedCol);
|
||||
table.ExtendMultiSelect(row, c);
|
||||
}
|
||||
{
|
||||
if (!table.HasMultiSelection)
|
||||
table.SelectCell(table.SelectedRow, table.SelectedCol, row, c);
|
||||
else
|
||||
{
|
||||
table.StartMultiSelect(row, c);
|
||||
}
|
||||
|
||||
table.RefreshView();
|
||||
SyncEditText(table);
|
||||
Event.current.Use();
|
||||
SceneView.RepaintAll();
|
||||
table.SelectionHandle.ExtendTo(row, c);
|
||||
}
|
||||
else
|
||||
{
|
||||
table.SelectCell(row, c);
|
||||
}
|
||||
|
||||
x += cellW;
|
||||
table.RefreshView();
|
||||
SyncEditText(table);
|
||||
Event.current.Use();
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
|
||||
x += cellW;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -972,15 +976,16 @@ namespace XericUIEditor.XTable
|
||||
s_DragStartCol = mdCol;
|
||||
|
||||
if (e.shift && table.SelectedRow >= 0 && table.SelectedCol >= 0)
|
||||
{
|
||||
if (!table.HasMultiSelection)
|
||||
table.StartMultiSelect(table.SelectedRow, table.SelectedCol);
|
||||
table.ExtendMultiSelect(mdRow, mdCol);
|
||||
}
|
||||
{
|
||||
if (!table.HasMultiSelection)
|
||||
table.SelectCell(table.SelectedRow, table.SelectedCol, mdRow, mdCol);
|
||||
else
|
||||
{
|
||||
table.StartMultiSelect(mdRow, mdCol);
|
||||
}
|
||||
table.SelectionHandle.ExtendTo(mdRow, mdCol);
|
||||
}
|
||||
else
|
||||
{
|
||||
table.SelectCell(mdRow, mdCol);
|
||||
}
|
||||
|
||||
table.RefreshView();
|
||||
SyncEditText(table);
|
||||
@@ -994,7 +999,7 @@ namespace XericUIEditor.XTable
|
||||
CellFromMouse(tr, xScale, yScale, out int dragRow, out int dragCol);
|
||||
if (dragRow < 0 || dragCol < 0) break;
|
||||
|
||||
table.ExtendMultiSelect(dragRow, dragCol);
|
||||
table.SelectionHandle.ExtendTo(dragRow, dragCol);
|
||||
table.RefreshView();
|
||||
SyncEditText(table);
|
||||
e.Use();
|
||||
@@ -1127,16 +1132,17 @@ namespace XericUIEditor.XTable
|
||||
}
|
||||
|
||||
if (shift)
|
||||
{
|
||||
if (!table.HasMultiSelection && table.SelectedRow >= 0 && table.SelectedCol >= 0)
|
||||
table.StartMultiSelect(table.SelectedRow, table.SelectedCol);
|
||||
table.ExtendMultiSelect(row, col);
|
||||
}
|
||||
{
|
||||
if (!table.HasMultiSelection && table.SelectedRow >= 0 && table.SelectedCol >= 0)
|
||||
table.SelectCell(table.SelectedRow, table.SelectedCol, row, col);
|
||||
else
|
||||
{
|
||||
RedirectToMergeSource(ref row, ref col);
|
||||
table.SelectCell(row, col);
|
||||
}
|
||||
table.SelectionHandle.ExtendTo(row, col);
|
||||
}
|
||||
else
|
||||
{
|
||||
RedirectToMergeSource(ref row, ref col);
|
||||
table.SelectCell(row, col);
|
||||
}
|
||||
|
||||
table.RefreshView();
|
||||
SyncEditText(table);
|
||||
|
||||
Reference in New Issue
Block a user