格式化。

修复了表格右键菜单时总是会创建在canvas下的问题
This commit is contained in:
2026-07-23 09:35:11 +08:00
parent 2cf0f75fc8
commit c694115893
2 changed files with 1217 additions and 1145 deletions
+32 -6
View File
@@ -115,22 +115,26 @@ namespace XericUIEditor.XTable
MacroFile.SetClipboardObject(data, MacroFile.JsonSerializerFormatter.formatterWithZip);
Debug.Log($"[XTable] 表格数据已导出到剪贴板 ({data.Cells?.Count ?? 0} 个单元格, {data.Merges?.Count ?? 0} 个合并)");
}
if (GUILayout.Button("从剪贴板导入", GUILayout.Height(26)))
{
var data = MacroFile.GetClipboardObject<XTableClipboardData>(MacroFile.JsonSerializerFormatter.formatterWithZip);
var data = MacroFile.GetClipboardObject<XTableClipboardData>(MacroFile.JsonSerializerFormatter
.formatterWithZip);
if (data != null)
{
Undo.RecordObject(m_Table, "Import Table from Clipboard");
m_Table.ApplyClipboardData(data);
serializedObject.Update();
Repaint();
Debug.Log($"[XTable] 已从剪贴板导入表格数据 ({data.Cells?.Count ?? 0} 个单元格, {data.Merges?.Count ?? 0} 个合并)");
Debug.Log(
$"[XTable] 已从剪贴板导入表格数据 ({data.Cells?.Count ?? 0} 个单元格, {data.Merges?.Count ?? 0} 个合并)");
}
else
{
Debug.LogWarning("[XTable] 剪贴板中没有有效的表格数据");
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space(4);
@@ -228,8 +232,10 @@ namespace XericUIEditor.XTable
EditorUtility.SetDirty(m_Table);
Repaint();
}
goto skipEndChangeCheck;
}
// 默认 Text 类型
string newText = EditorGUILayout.TextField("文本内容", data.Text ?? "");
if (EditorGUI.EndChangeCheck())
@@ -242,8 +248,8 @@ namespace XericUIEditor.XTable
EditorUtility.SetDirty(m_Table);
Repaint();
}
skipEndChangeCheck:
;
skipEndChangeCheck: ;
// 样式
EditorGUILayout.Space(2);
@@ -396,14 +402,34 @@ namespace XericUIEditor.XTable
}
}
// 挂载到 Canvas
GameObject parent = GetOrCreateCanvas();
// 确定父级:优先放在右键点击的对象下,其次放在其所在的 Canvas 下
GameObject parent = GetCreateParent(menuCommand);
rootRt.SetParent(parent.transform, false);
Undo.RegisterCreatedObjectUndo(rootGo, "Create Xeric UI Action Table");
Selection.activeGameObject = rootGo;
}
/// <summary>
/// 确定创建表格时的父级对象。
/// 优先使用右键菜单点击的 GameObject(要求其处于 Canvas 层级下);
/// 若没有任何 Canvas 则自动创建一个。
/// </summary>
private static GameObject GetCreateParent(MenuCommand menuCommand)
{
// 右键菜单点击的对象
GameObject clickedGo = menuCommand?.context as GameObject;
if (clickedGo == null && menuCommand?.context is Component comp)
clickedGo = comp.gameObject;
// 点击的对象在 Canvas 层级下 → 直接作为父级
if (clickedGo != null && clickedGo.GetComponentInParent<Canvas>() != null)
return clickedGo;
// 兜底:场景中的 Canvas 或自动创建
return GetOrCreateCanvas();
}
private static GameObject GetOrCreateCanvas()
{
Canvas canvas = Object.FindObjectOfType<Canvas>();
+60 -14
View File
@@ -1,5 +1,4 @@
using UnityEditor;
using UnityEngine;
using XericLibrary.Runtime.MacroLibrary;
using XericLibrary.Runtime.SuperStyleSheet;
@@ -121,7 +120,8 @@ namespace XericUIEditor.XTable
{
s_SizeEditTargetIndex = idx;
s_SizeEditText = s_ColWidths != null && idx < s_ColWidths.Length
? s_ColWidths[idx].ToString("F0") : "";
? s_ColWidths[idx].ToString("F0")
: "";
}
}
else if (newFocus.StartsWith("XTableRowSizeEdit_") && s_ActiveTable != null)
@@ -130,7 +130,8 @@ namespace XericUIEditor.XTable
{
s_SizeEditTargetIndex = idx;
s_SizeEditText = s_RowHeights != null && idx < s_RowHeights.Length
? s_RowHeights[idx].ToString("F0") : "";
? s_RowHeights[idx].ToString("F0")
: "";
}
}
// 底部内容输入框获得焦点:种子为当前单元格内容
@@ -263,6 +264,7 @@ namespace XericUIEditor.XTable
if (coord >= acc && coord < acc + sizes[i]) return i;
acc += sizes[i];
}
return -1;
}
@@ -326,7 +328,11 @@ namespace XericUIEditor.XTable
for (int r = 0; r < rh.Length; r++)
{
float cellH = rh[r] * yScale;
if (cellH < 2) { y += cellH; continue; }
if (cellH < 2)
{
y += cellH;
continue;
}
Rect labelRect = new Rect(
tableRect.x - COL_W - MARGIN,
@@ -346,7 +352,11 @@ namespace XericUIEditor.XTable
GUI.Label(labelRect, $"{r}", style);
// 表格导航状态下才响应鼠标
if (s_State != EditorState.TableNavigation) { y += cellH; continue; }
if (s_State != EditorState.TableNavigation)
{
y += cellH;
continue;
}
if (Event.current.type == EventType.MouseDown &&
Event.current.button == 0 &&
@@ -399,7 +409,11 @@ namespace XericUIEditor.XTable
for (int c = 0; c < cw.Length; c++)
{
float cellW = cw[c] * xScale;
if (cellW < 2) { x += cellW; continue; }
if (cellW < 2)
{
x += cellW;
continue;
}
Rect labelRect = new Rect(
x,
@@ -422,7 +436,11 @@ namespace XericUIEditor.XTable
GUI.Label(labelRect, text, style);
// 表格导航状态下才响应鼠标
if (s_State != EditorState.TableNavigation) { x += cellW; continue; }
if (s_State != EditorState.TableNavigation)
{
x += cellW;
continue;
}
if (Event.current.type == EventType.MouseDown &&
Event.current.button == 0 &&
@@ -626,8 +644,16 @@ namespace XericUIEditor.XTable
{
fontSize = 11,
fontStyle = FontStyle.Bold,
normal = { textColor = new Color(0.5f, 0.5f, 0.5f), background = MakeTex(1, 1, new Color(0.25f, 0.25f, 0.25f, 0.6f)) },
hover = { textColor = new Color(0.7f, 0.7f, 0.7f), background = MakeTex(1, 1, new Color(0.35f, 0.35f, 0.35f, 0.8f)) },
normal =
{
textColor = new Color(0.5f, 0.5f, 0.5f),
background = MakeTex(1, 1, new Color(0.25f, 0.25f, 0.25f, 0.6f))
},
hover =
{
textColor = new Color(0.7f, 0.7f, 0.7f),
background = MakeTex(1, 1, new Color(0.35f, 0.35f, 0.35f, 0.8f))
},
active = { textColor = Color.white, background = MakeTex(1, 1, new Color(0.15f, 0.15f, 0.15f, 0.8f)) },
padding = new RectOffset(0, 0, 0, 0),
margin = new RectOffset(0, 0, 0, 0),
@@ -642,7 +668,11 @@ namespace XericUIEditor.XTable
};
s_BubbleDelBtnStyle = new GUIStyle(s_BubbleApplyBtnStyle)
{
normal = { textColor = new Color(0.6f, 0.3f, 0.3f), background = MakeTex(1, 1, new Color(0.3f, 0.15f, 0.15f, 0.6f)) },
normal =
{
textColor = new Color(0.6f, 0.3f, 0.3f),
background = MakeTex(1, 1, new Color(0.3f, 0.15f, 0.15f, 0.6f))
},
hover = { textColor = Color.white, background = MakeTex(1, 1, new Color(0.7f, 0.2f, 0.2f, 0.95f)) },
active = { textColor = Color.white, background = MakeTex(1, 1, new Color(0.5f, 0.1f, 0.1f, 0.95f)) }
};
@@ -686,6 +716,7 @@ namespace XericUIEditor.XTable
float by = tr.y - ROW_H - MARGIN - BUBBLE_H - 4f;
DrawBubble(table, bx, by, cw[selCol], totalW, isColumn: true, index: selCol);
}
if (selRow < rh.Length)
{
float ry = tr.y + SumBefore(rh, selRow) * yScale;
@@ -769,6 +800,7 @@ namespace XericUIEditor.XTable
table.SetRowHeight(index, value);
table.RefreshView();
}
GUI.FocusControl(null);
s_State = EditorState.TableNavigation;
SceneView.RepaintAll();
@@ -869,8 +901,14 @@ namespace XericUIEditor.XTable
{
var mergeStyle = new GUIStyle(btnStyle)
{
normal = { background = MakeTex(1, 1, new Color(0.15f, 0.3f, 0.65f, 0.8f)), textColor = Color.white },
hover = { background = MakeTex(1, 1, new Color(0.2f, 0.4f, 0.85f, 0.9f)), textColor = Color.white }
normal =
{
background = MakeTex(1, 1, new Color(0.15f, 0.3f, 0.65f, 0.8f)), textColor = Color.white
},
hover =
{
background = MakeTex(1, 1, new Color(0.2f, 0.4f, 0.85f, 0.9f)), textColor = Color.white
}
};
if (GUI.Button(new Rect(rightX - mergeBtnW - 2, row1Y, mergeBtnW, rowH), "合并", mergeStyle))
{
@@ -889,8 +927,14 @@ namespace XericUIEditor.XTable
{
var unmergeStyle = new GUIStyle(btnStyle)
{
normal = { background = MakeTex(1, 1, new Color(0.65f, 0.25f, 0.15f, 0.8f)), textColor = Color.white },
hover = { background = MakeTex(1, 1, new Color(0.85f, 0.35f, 0.2f, 0.9f)), textColor = Color.white }
normal =
{
background = MakeTex(1, 1, new Color(0.65f, 0.25f, 0.15f, 0.8f)), textColor = Color.white
},
hover =
{
background = MakeTex(1, 1, new Color(0.85f, 0.35f, 0.2f, 0.9f)), textColor = Color.white
}
};
float unmergeX = showMerge ? rightX - mergeBtnW - unmergeBtnW - 4 : rightX - unmergeBtnW - 2;
if (GUI.Button(new Rect(unmergeX, row1Y, unmergeBtnW, rowH), "拆分", unmergeStyle))
@@ -922,6 +966,7 @@ namespace XericUIEditor.XTable
displayNamespaces.Add(currentNS);
currentIdx = displayNamespaces.Count - 1;
}
if (currentIdx < 0) currentIdx = 0;
Rect dropRect = new Rect(barRect.x + 44, row2Y, barW - 44 - pad, rowH);
@@ -1163,6 +1208,7 @@ namespace XericUIEditor.XTable
outCol = col;
return false;
}
outRow = row;
outCol = col;
return true;