功能批量实现 优化 服务端 新浮动小游戏框架 新界面UI

This commit is contained in:
2026-04-24 13:20:16 +08:00
parent e0bc0bbf08
commit 5eec879582
257 changed files with 38481 additions and 1288 deletions
@@ -1,10 +1,10 @@
#if UNITY_EDITOR
#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;
public class IngameGalgameGraphWindow : EditorWindow
{
@@ -46,15 +46,18 @@ public class IngameGalgameGraphWindow : EditorWindow
private void CreateToolbar()
{
var toolbar = new Toolbar();
var addBtn = new ToolbarButton(() => graphView?.CreateNodeAt(graphView.GetDefaultNodePosition())) { text = "+" };
var delBtn = new ToolbarButton(() => graphView?.DeleteSelected()) { text = "-" };
toolbar.Add(addBtn);
toolbar.Add(delBtn);
soField = new ObjectField("数据") { objectType = typeof(ingame_galgame_so), allowSceneObjects = false };
toolbar.Add(new ToolbarButton(() => graphView?.CreateNodeAt(graphView.GetDefaultNodePosition())) { text = "+" });
toolbar.Add(new ToolbarButton(() => graphView?.DeleteSelected()) { text = "-" });
soField = new ObjectField("数据")
{
objectType = typeof(ingame_galgame_so),
allowSceneObjects = false
};
soField.RegisterValueChangedCallback(evt => SetSo(evt.newValue as ingame_galgame_so));
toolbar.Add(soField);
var refreshBtn = new ToolbarButton(RefreshGraph) { text = "刷新" };
toolbar.Add(refreshBtn);
toolbar.Add(new ToolbarButton(RefreshGraph) { text = "刷新" });
rootVisualElement.Add(toolbar);
}
@@ -70,12 +73,16 @@ public class IngameGalgameGraphWindow : EditorWindow
private void RefreshGraph()
{
if (graphView == null) return;
if (graphView == null)
{
return;
}
graphView.Build(currentSo);
}
}
class IngameGalgameGraphView : GraphView
internal class IngameGalgameGraphView : GraphView
{
private ingame_galgame_so currentSo;
@@ -85,9 +92,11 @@ class IngameGalgameGraphView : GraphView
this.AddManipulator(new ContentDragger());
this.AddManipulator(new SelectionDragger());
this.AddManipulator(new RectangleSelector());
var grid = new GridBackground();
Insert(0, grid);
grid.StretchToParentSize();
SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
RegisterCallback<KeyDownEvent>(OnKeyDown);
}
@@ -96,18 +105,30 @@ class IngameGalgameGraphView : GraphView
{
ClearGraph();
currentSo = so;
if (so == null) return;
if (so == null)
{
return;
}
var serialized = new SerializedObject(so);
var dialogueList = serialized.FindProperty("dialogueList");
if (dialogueList == null || !dialogueList.isArray) return;
var speakerNames = BuildSpeakerNames(serialized);
if (dialogueList == null || !dialogueList.isArray)
{
return;
}
string[] speakerNames = BuildSpeakerNames(serialized);
DialogueTextNode prev = null;
for (int i = 0; i < dialogueList.arraySize; i++)
{
var elemProp = dialogueList.GetArrayElementAtIndex(i);
var positionTagProp = elemProp.FindPropertyRelative("positionTag");
var textListProp = elemProp.FindPropertyRelative("textList");
if (textListProp == null || !textListProp.isArray) continue;
if (textListProp == null || !textListProp.isArray)
{
continue;
}
for (int j = 0; j < textListProp.arraySize; j++)
{
var textProp = textListProp.GetArrayElementAtIndex(j);
@@ -116,8 +137,7 @@ class IngameGalgameGraphView : GraphView
AddElement(node);
if (prev != null)
{
var edge = prev.output.ConnectTo(node.input);
AddElement(edge);
AddElement(prev.output.ConnectTo(node.input));
}
prev = node;
}
@@ -136,10 +156,12 @@ class IngameGalgameGraphView : GraphView
private DialogueTextNode CreateTextNode(SerializedObject so, SerializedProperty positionTagProp, SerializedProperty textProp, string[] speakerNames, int elemIndex, int textIndex)
{
var node = new DialogueTextNode();
node.title = $"段落 {elemIndex + 1} / 文本 {textIndex + 1}";
node.elementIndex = elemIndex;
node.textIndex = textIndex;
var node = new DialogueTextNode
{
title = $"段落 {elemIndex + 1} / 文本 {textIndex + 1}",
elementIndex = elemIndex,
textIndex = textIndex
};
node.input = node.InstantiatePort(Orientation.Horizontal, Direction.Input, Port.Capacity.Multi, typeof(float));
node.input.portName = "输入";
node.output = node.InstantiatePort(Orientation.Horizontal, Direction.Output, Port.Capacity.Single, typeof(float));
@@ -149,18 +171,18 @@ class IngameGalgameGraphView : GraphView
if (positionTagProp != null)
{
var field = new PropertyField(positionTagProp, "位置标签");
var field = new PropertyField(positionTagProp, "显示模式");
field.Bind(so);
node.extensionContainer.Add(field);
}
AddTextField(node, so, textProp.FindPropertyRelative("content"), "内容");
AddIntPopup(node, so, textProp.FindPropertyRelative("senderIndex"), speakerNames);
AddField(node, so, textProp.FindPropertyRelative("content"), "文本内容");
AddSpeakerPopup(node, so, textProp.FindPropertyRelative("senderIndex"), speakerNames);
AddField(node, so, textProp.FindPropertyRelative("imagePos"), "图片位置");
AddField(node, so, textProp.FindPropertyRelative("flipX"), "水平翻转");
AddField(node, so, textProp.FindPropertyRelative("appearTime"), "出现时间");
AddField(node, so, textProp.FindPropertyRelative("duration"), "持续时间");
AddField(node, so, textProp.FindPropertyRelative("stepMethod"), "进方式");
AddField(node, so, textProp.FindPropertyRelative("stepMethod"), "进方式");
AddField(node, so, textProp.FindPropertyRelative("timeAction"), "时间动作");
AddField(node, so, textProp.FindPropertyRelative("slowMotionScale"), "慢动作倍率");
AddField(node, so, textProp.FindPropertyRelative("endAction"), "结束动作");
@@ -170,35 +192,36 @@ class IngameGalgameGraphView : GraphView
return node;
}
private void AddTextField(Node node, SerializedObject so, SerializedProperty prop, string label)
private static void AddField(Node node, SerializedObject so, SerializedProperty prop, string label)
{
if (prop == null) return;
var field = new PropertyField(prop, label);
field.Bind(so);
node.extensionContainer.Add(field);
}
private void AddField(Node node, SerializedObject so, SerializedProperty prop, string label)
{
if (prop == null) return;
var field = new PropertyField(prop, label);
field.Bind(so);
node.extensionContainer.Add(field);
}
private void AddIntPopup(Node node, SerializedObject so, SerializedProperty senderIndexProp, string[] speakerNames)
{
if (senderIndexProp == null) return;
if (speakerNames == null || speakerNames.Length == 0)
if (prop == null)
{
AddField(node, so, senderIndexProp, "发言者索引");
return;
}
var field = new PropertyField(prop, label);
field.Bind(so);
node.extensionContainer.Add(field);
}
private static void AddSpeakerPopup(Node node, SerializedObject so, SerializedProperty senderIndexProp, string[] speakerNames)
{
if (senderIndexProp == null)
{
return;
}
if (speakerNames == null || speakerNames.Length == 0)
{
AddField(node, so, senderIndexProp, "发言人");
return;
}
var imgui = new IMGUIContainer(() =>
{
so.Update();
int current = Mathf.Clamp(senderIndexProp.intValue + 1, 0, speakerNames.Length - 1);
int chosen = EditorGUILayout.Popup("发言", current, speakerNames);
int chosen = EditorGUILayout.Popup("发言", current, speakerNames);
senderIndexProp.intValue = Mathf.Max(-1, chosen - 1);
so.ApplyModifiedProperties();
});
@@ -208,17 +231,25 @@ class IngameGalgameGraphView : GraphView
private string[] BuildSpeakerNames(SerializedObject so)
{
var speakersDataSOProp = so.FindProperty("speakersDataSO");
if (speakersDataSOProp == null || speakersDataSOProp.objectReferenceValue == null) return null;
if (speakersDataSOProp == null || speakersDataSOProp.objectReferenceValue == null)
{
return null;
}
var speakersSO = new SerializedObject(speakersDataSOProp.objectReferenceValue);
var speakersList = speakersSO.FindProperty("speakers");
if (speakersList == null || !speakersList.isArray) return null;
int c = speakersList.arraySize;
var names = new string[c + 1];
names[0] = "(无)";
for (int i = 0; i < c; i++)
if (speakersList == null || !speakersList.isArray)
{
var e = speakersList.GetArrayElementAtIndex(i);
var nameProp = e.FindPropertyRelative("spkrName");
return null;
}
int count = speakersList.arraySize;
string[] names = new string[count + 1];
names[0] = "(无)";
for (int i = 0; i < count; i++)
{
var element = speakersList.GetArrayElementAtIndex(i);
var nameProp = element.FindPropertyRelative("spkrName");
names[i + 1] = nameProp != null ? nameProp.stringValue : $"Speaker {i}";
}
return names;
@@ -232,11 +263,19 @@ class IngameGalgameGraphView : GraphView
public void CreateNodeAt(Vector2 position)
{
if (currentSo == null) return;
if (currentSo == null)
{
return;
}
Undo.RecordObject(currentSo, "Create Dialogue Node");
var so = new SerializedObject(currentSo);
var dialogueList = so.FindProperty("dialogueList");
if (dialogueList == null) return;
if (dialogueList == null)
{
return;
}
int elemIndex = dialogueList.arraySize;
dialogueList.arraySize++;
var elemProp = dialogueList.GetArrayElementAtIndex(elemIndex);
@@ -252,6 +291,7 @@ class IngameGalgameGraphView : GraphView
ApplyTextDefaults(textProp);
}
}
so.ApplyModifiedProperties();
EditorUtility.SetDirty(currentSo);
Build(currentSo);
@@ -259,38 +299,69 @@ class IngameGalgameGraphView : GraphView
public void DeleteSelected()
{
if (currentSo == null) return;
var selectedNodes = new List<DialogueTextNode>();
foreach (var e in selection)
if (currentSo == null)
{
if (e is DialogueTextNode n) selectedNodes.Add(n);
return;
}
if (selectedNodes.Count == 0) return;
var selectedNodes = new List<DialogueTextNode>();
foreach (var element in selection)
{
if (element is DialogueTextNode node)
{
selectedNodes.Add(node);
}
}
if (selectedNodes.Count == 0)
{
return;
}
selectedNodes.Sort((a, b) =>
{
int c = b.elementIndex.CompareTo(a.elementIndex);
return c != 0 ? c : b.textIndex.CompareTo(a.textIndex);
int compare = b.elementIndex.CompareTo(a.elementIndex);
return compare != 0 ? compare : b.textIndex.CompareTo(a.textIndex);
});
Undo.RecordObject(currentSo, "Delete Dialogue Node");
var so = new SerializedObject(currentSo);
var dialogueList = so.FindProperty("dialogueList");
if (dialogueList == null) return;
if (dialogueList == null)
{
return;
}
foreach (var node in selectedNodes)
{
if (node.elementIndex < 0 || node.elementIndex >= dialogueList.arraySize) continue;
if (node.elementIndex < 0 || node.elementIndex >= dialogueList.arraySize)
{
continue;
}
var elemProp = dialogueList.GetArrayElementAtIndex(node.elementIndex);
if (elemProp == null) continue;
if (elemProp == null)
{
continue;
}
var textListProp = elemProp.FindPropertyRelative("textList");
if (textListProp == null) continue;
if (textListProp == null)
{
continue;
}
if (node.textIndex >= 0 && node.textIndex < textListProp.arraySize)
{
textListProp.DeleteArrayElementAtIndex(node.textIndex);
}
if (textListProp.arraySize <= 0)
{
dialogueList.DeleteArrayElementAtIndex(node.elementIndex);
}
}
so.ApplyModifiedProperties();
EditorUtility.SetDirty(currentSo);
Build(currentSo);
@@ -299,12 +370,18 @@ class IngameGalgameGraphView : GraphView
public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
evt.menu.AppendAction("新建节点", _ => CreateNodeAt(evt.localMousePosition));
bool hasSelection = false;
foreach (var e in selection)
foreach (var element in selection)
{
if (e is DialogueTextNode) { hasSelection = true; break; }
if (element is DialogueTextNode)
{
hasSelection = true;
break;
}
}
evt.menu.AppendAction("删除所选", _ => DeleteSelected(), hasSelection ? DropdownMenuAction.Status.Normal : DropdownMenuAction.Status.Disabled);
evt.menu.AppendAction("删除选中", _ => DeleteSelected(), hasSelection ? DropdownMenuAction.Status.Normal : DropdownMenuAction.Status.Disabled);
}
private void OnKeyDown(KeyDownEvent evt)
@@ -328,65 +405,108 @@ class IngameGalgameGraphView : GraphView
private string SerializeSelection()
{
if (currentSo == null) return string.Empty;
if (currentSo == null)
{
return string.Empty;
}
var payload = new DialogueCopyPayload();
var so = new SerializedObject(currentSo);
var dialogueList = so.FindProperty("dialogueList");
foreach (var e in selection)
foreach (var element in selection)
{
if (e is not DialogueTextNode node) continue;
if (dialogueList == null) continue;
if (node.elementIndex < 0 || node.elementIndex >= dialogueList.arraySize) continue;
if (element is not DialogueTextNode node || dialogueList == null)
{
continue;
}
if (node.elementIndex < 0 || node.elementIndex >= dialogueList.arraySize)
{
continue;
}
var elemProp = dialogueList.GetArrayElementAtIndex(node.elementIndex);
if (elemProp == null) continue;
var positionTagProp = elemProp.FindPropertyRelative("positionTag");
var textListProp = elemProp.FindPropertyRelative("textList");
if (textListProp == null) continue;
if (node.textIndex < 0 || node.textIndex >= textListProp.arraySize) continue;
var positionTagProp = elemProp?.FindPropertyRelative("positionTag");
var textListProp = elemProp?.FindPropertyRelative("textList");
if (textListProp == null || node.textIndex < 0 || node.textIndex >= textListProp.arraySize)
{
continue;
}
var textProp = textListProp.GetArrayElementAtIndex(node.textIndex);
if (textProp == null) continue;
var data = new DialogueTextCopyData();
data.positionTag = positionTagProp != null ? positionTagProp.enumValueIndex : 0;
data.content = textProp.FindPropertyRelative("content")?.stringValue;
data.textNumber = textProp.FindPropertyRelative("textNumber")?.intValue ?? 0;
data.senderIndex = textProp.FindPropertyRelative("senderIndex")?.intValue ?? -1;
data.imagePos = textProp.FindPropertyRelative("imagePos")?.enumValueIndex ?? 0;
data.flipX = textProp.FindPropertyRelative("flipX")?.boolValue ?? false;
data.appearTime = textProp.FindPropertyRelative("appearTime")?.floatValue ?? 0f;
data.duration = textProp.FindPropertyRelative("duration")?.floatValue ?? 0f;
data.stepMethod = textProp.FindPropertyRelative("stepMethod")?.enumValueIndex ?? 0;
data.timeAction = textProp.FindPropertyRelative("timeAction")?.enumValueIndex ?? 0;
data.slowMotionScale = textProp.FindPropertyRelative("slowMotionScale")?.floatValue ?? 0f;
data.endAction = textProp.FindPropertyRelative("endAction")?.enumValueIndex ?? 0;
payload.items.Add(data);
if (textProp == null)
{
continue;
}
payload.items.Add(new DialogueTextCopyData
{
positionTag = positionTagProp != null ? positionTagProp.enumValueIndex : 0,
content = textProp.FindPropertyRelative("content")?.stringValue,
textNumber = textProp.FindPropertyRelative("textNumber")?.intValue ?? 0,
senderIndex = textProp.FindPropertyRelative("senderIndex")?.intValue ?? -1,
imagePos = textProp.FindPropertyRelative("imagePos")?.enumValueIndex ?? 0,
flipX = textProp.FindPropertyRelative("flipX")?.boolValue ?? false,
appearTime = textProp.FindPropertyRelative("appearTime")?.floatValue ?? 0f,
duration = textProp.FindPropertyRelative("duration")?.floatValue ?? 0f,
stepMethod = textProp.FindPropertyRelative("stepMethod")?.enumValueIndex ?? 0,
timeAction = textProp.FindPropertyRelative("timeAction")?.enumValueIndex ?? 0,
slowMotionScale = textProp.FindPropertyRelative("slowMotionScale")?.floatValue ?? 0f,
endAction = textProp.FindPropertyRelative("endAction")?.enumValueIndex ?? 0
});
}
return payload.items.Count == 0 ? string.Empty : JsonUtility.ToJson(payload);
}
private bool CanPasteDataInternal(string serializedData)
private static bool CanPasteDataInternal(string serializedData)
{
if (string.IsNullOrEmpty(serializedData)) return false;
if (string.IsNullOrEmpty(serializedData))
{
return false;
}
var payload = JsonUtility.FromJson<DialogueCopyPayload>(serializedData);
return payload != null && payload.items != null && payload.items.Count > 0;
}
private void PasteFromClipboard()
{
if (currentSo == null) return;
if (currentSo == null)
{
return;
}
var serializedData = EditorGUIUtility.systemCopyBuffer;
if (!CanPasteDataInternal(serializedData)) return;
if (!CanPasteDataInternal(serializedData))
{
return;
}
var payload = JsonUtility.FromJson<DialogueCopyPayload>(serializedData);
if (payload == null || payload.items == null || payload.items.Count == 0) return;
if (payload == null || payload.items == null || payload.items.Count == 0)
{
return;
}
Undo.RecordObject(currentSo, "Paste Dialogue Node");
var so = new SerializedObject(currentSo);
var dialogueList = so.FindProperty("dialogueList");
if (dialogueList == null) return;
if (dialogueList == null)
{
return;
}
foreach (var item in payload.items)
{
int elemIndex = dialogueList.arraySize;
dialogueList.arraySize++;
var elemProp = dialogueList.GetArrayElementAtIndex(elemIndex);
if (elemProp == null) continue;
if (elemProp == null)
{
continue;
}
var positionTagProp = elemProp.FindPropertyRelative("positionTag");
if (positionTagProp != null) positionTagProp.enumValueIndex = item.positionTag;
var textListProp = elemProp.FindPropertyRelative("textList");
@@ -397,14 +517,19 @@ class IngameGalgameGraphView : GraphView
ApplyTextData(textProp, item);
}
}
so.ApplyModifiedProperties();
EditorUtility.SetDirty(currentSo);
Build(currentSo);
}
private void ApplyTextDefaults(SerializedProperty textProp)
private static void ApplyTextDefaults(SerializedProperty textProp)
{
if (textProp == null) return;
if (textProp == null)
{
return;
}
textProp.FindPropertyRelative("content").stringValue = string.Empty;
textProp.FindPropertyRelative("textNumber").intValue = 1;
textProp.FindPropertyRelative("senderIndex").intValue = -1;
@@ -412,16 +537,20 @@ class IngameGalgameGraphView : GraphView
var flipXProp = textProp.FindPropertyRelative("flipX");
if (flipXProp != null) flipXProp.boolValue = false;
textProp.FindPropertyRelative("appearTime").floatValue = 0f;
textProp.FindPropertyRelative("duration").floatValue = 0f;
textProp.FindPropertyRelative("duration").floatValue = 1f;
textProp.FindPropertyRelative("stepMethod").enumValueIndex = 0;
textProp.FindPropertyRelative("timeAction").enumValueIndex = 0;
textProp.FindPropertyRelative("slowMotionScale").floatValue = 0f;
textProp.FindPropertyRelative("slowMotionScale").floatValue = 0.5f;
textProp.FindPropertyRelative("endAction").enumValueIndex = 0;
}
private void ApplyTextData(SerializedProperty textProp, DialogueTextCopyData data)
private static void ApplyTextData(SerializedProperty textProp, DialogueTextCopyData data)
{
if (textProp == null || data == null) return;
if (textProp == null || data == null)
{
return;
}
textProp.FindPropertyRelative("content").stringValue = data.content ?? string.Empty;
textProp.FindPropertyRelative("textNumber").intValue = data.textNumber;
textProp.FindPropertyRelative("senderIndex").intValue = data.senderIndex;
@@ -460,7 +589,7 @@ class IngameGalgameGraphView : GraphView
}
}
class DialogueTextNode : Node
internal class DialogueTextNode : Node
{
public Port input;
public Port output;