DataEditor & StorySystem Graph

This commit is contained in:
SoulliesOfficial
2025-11-10 11:18:19 -05:00
parent 1bca620966
commit ea75bd5225
76 changed files with 2340 additions and 90 deletions

View File

@@ -0,0 +1,15 @@
using System.Collections.Generic;
using UnityEngine;
namespace SLSFramework.StorySystem
{
public abstract class GraphBase : ScriptableObject
{
// 使用 [SerializeReference] 来支持多态性,存储不同类型的节点数据
[SerializeReference]
public List<BaseNodeData> nodes = new List<BaseNodeData>();
// 存储节点之间的连接
public List<EdgeData> edges = new List<EdgeData>();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 88df947440cfd3841b8e5a545437b253

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1ad08c73d10275f498f9643e948dade4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,36 @@
using SLSFramework.StorySystem.Dialog;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
namespace SLSFramework.StorySystem
{
// 自定义节点的基础类,用于存储对应的数据
public abstract class BaseGraphNode : Node
{
public BaseNodeData NodeData { get; private set; }
protected BaseGraphNode(BaseNodeData data)
{
NodeData = data;
title = data.GetType().Name.Replace("Data", ""); // 自动设置标题, e.g. "StartNodeData" -> "StartNode"
SetPosition(new Rect(data.position, Vector2.zero)); // Vector2.zero size 会被自动计算
viewDataKey = data.guid; // 确保GUID一致
}
// 辅助方法:创建端口
protected Port CreatePort(Direction direction, Port.Capacity capacity = Port.Capacity.Single, string portName = "")
{
// 如果未指定端口名,则使用方向的默认值
if (string.IsNullOrEmpty(portName))
{
portName = direction == Direction.Input ? "Input" : "Output";
}
var port = InstantiatePort(Orientation.Horizontal, direction, capacity, typeof(bool));
port.portName = portName;
port.name = portName;
return port;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 89062ba4c7f90314c86ed9cedb4053cf

View File

@@ -0,0 +1,47 @@
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace SLSFramework.StorySystem
{
public class ConditionGraphNode : BaseGraphNode
{
private ConditionNodeData _data;
public ConditionGraphNode(ConditionNodeData data) : base(data)
{
_data = data;
var titleContainer = this.Q("title");
titleContainer.style.backgroundColor = new StyleColor(new Color(0.4f, 0.2f, 0.6f)); // 紫色
// 1. 添加端口
var inputPort = CreatePort(Direction.Input, Port.Capacity.Multi, "Previous");
// Condition 节点有两个输出
var truePort = CreatePort(Direction.Output, Port.Capacity.Single, "True");
var falsePort = CreatePort(Direction.Output, Port.Capacity.Single, "False");
inputContainer.Add(inputPort);
outputContainer.Add(truePort);
outputContainer.Add(falsePort);
// 2. 添加自定义UI字段 (条件语句)
var conditionField = new TextField("Condition") { multiline = true };
conditionField.SetValueWithoutNotify(_data.conditionString);
conditionField.RegisterValueChangedCallback(evt => {
_data.conditionString = evt.newValue;
});
// 调整TextArea的样式
conditionField.Q("unity-text-input").style.minWidth = 150;
extensionContainer.Add(conditionField);
// 刷新布局
RefreshExpandedState();
RefreshPorts();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 95a4ba99bc298c74283b008b8b25b8bd

View File

@@ -0,0 +1,23 @@
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace SLSFramework.StorySystem
{
public class EndGraphNode : BaseGraphNode
{
public EndGraphNode(EndNodeData data) : base(data)
{
var titleContainer = this.Q("title");
titleContainer.style.backgroundColor = new StyleColor(new Color(0.4f, 0.4f, 0.2f)); // 红色
// End 节点只有一个输入端口
var inputPort = CreatePort(Direction.Input, Port.Capacity.Multi, "Previous");
inputContainer.Add(inputPort);
// 刷新端口和布局
RefreshExpandedState();
RefreshPorts();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e82960b057418454eab7e7eefcc71a14

View File

@@ -0,0 +1,43 @@
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace SLSFramework.StorySystem.Dialog
{
public class EventGraphNode : BaseGraphNode
{
private EventNodeData _data;
public EventGraphNode(EventNodeData data) : base(data)
{
_data = data;
var titleContainer = this.Q("title");
titleContainer.style.backgroundColor = new StyleColor(new Color(0.1f, 0.5f, 0.5f)); // 青色
// 1. 添加端口
var inputPort = CreatePort(Direction.Input, Port.Capacity.Multi, "Previous");
var outputPort = CreatePort(Direction.Output, Port.Capacity.Single, "Next");
inputContainer.Add(inputPort);
outputContainer.Add(outputPort);
// 2. 添加自定义UI字段 (事件语句)
var eventField = new TextField("Event") { multiline = true };
eventField.SetValueWithoutNotify(_data.eventString);
eventField.RegisterValueChangedCallback(evt => {
_data.eventString = evt.newValue;
});
// 调整TextArea的样式
eventField.Q("unity-text-input").style.minWidth = 150;
extensionContainer.Add(eventField);
// 刷新布局
RefreshExpandedState();
RefreshPorts();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: baf926c674d6ea842b99cda704461ee2

View File

@@ -0,0 +1,23 @@
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace SLSFramework.StorySystem
{
public class StartGraphNode : BaseGraphNode
{
public StartGraphNode(StartNodeData data) : base(data)
{
VisualElement titleContainer = this.Q("title");
titleContainer.style.backgroundColor = new StyleColor(new Color(0.2f, 0.5f, 0.2f)); // 绿色
// Start 节点只有一个输出端口
var outputPort = CreatePort(Direction.Output, Port.Capacity.Single, "Next");
outputContainer.Add(outputPort);
// 刷新端口和布局
RefreshExpandedState();
RefreshPorts();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d3bf9fbf0029def4bbb8ce1e1ed0628c

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4019e9906ff3c1f46b7db8bbb8604ef3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,27 @@
using UnityEditor;
using UnityEngine;
namespace SLSFramework.StorySystem
{
public class EditorWindowBase : EditorWindow
{
protected GraphViewBase graphView;
protected GraphBase currentGraph;
protected string windowTitle = "Story System Graph";
protected virtual void CreateGraphView()
{
}
public virtual void SetGraph(GraphBase graph)
{
}
public virtual void OnGraphUpdated()
{
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2ada5daa7b9068f4c98c9af0655aef6d

View File

@@ -0,0 +1,296 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace SLSFramework.StorySystem
{
public partial class GraphViewBase : GraphView
{
protected GraphBase graph;
protected EditorWindowBase editorWindow;
// 加载图表
public void LoadGraph(GraphBase graph)
{
this.graph = graph;
if (graph == null)
{
Debug.LogError("Graph is null, cannot load.");
return;
}
// 清除当前视图
DeleteElements(graphElements);
// 1. 创建所有节点
var nodeViewMap = new Dictionary<string, BaseGraphNode>();
foreach (var nodeData in this.graph.nodes)
{
BaseGraphNode nodeView = CreateNodeView(nodeData);
AddElement(nodeView);
nodeViewMap[nodeData.guid] = nodeView;
}
// 2. 创建所有连接
foreach (var edgeData in this.graph.edges)
{
if (!nodeViewMap.TryGetValue(edgeData.outputNodeGuid, out var outputNode) ||
!nodeViewMap.TryGetValue(edgeData.inputNodeGuid, out var inputNode))
{
Debug.LogWarning($"Failed to find nodes for edge: {edgeData.outputNodeGuid} -> {edgeData.inputNodeGuid}");
continue;
}
Port outputPort = outputNode.outputContainer.Q<Port>(name: edgeData.outputPortName);
Port inputPort = inputNode.inputContainer.Q<Port>(name: edgeData.inputPortName);
if (outputPort == null || inputPort == null)
{
Debug.LogWarning(
$"Failed to find ports for edge: {outputNode.title} (Port: {edgeData.outputPortName}) -> {inputNode.title} (Port: {edgeData.inputPortName})");
continue;
}
Edge edge = outputPort.ConnectTo(inputPort);
AddElement(edge);
}
}
public void SaveGraph()
{
if (graph == null) return;
graph.nodes.Clear();
graph.edges.Clear();
// 1. 保存所有节点
foreach (var nodeView in nodes.OfType<BaseGraphNode>())
{
// 更新节点数据的位置
nodeView.NodeData.position = nodeView.GetPosition().position;
graph.nodes.Add(nodeView.NodeData);
}
// 2. 保存所有连接
foreach (var edge in edges)
{
var outputNode = (BaseGraphNode)edge.output.node;
var inputNode = (BaseGraphNode)edge.input.node;
graph.edges.Add(new EdgeData
{
outputNodeGuid = outputNode.NodeData.guid,
// --- 关键修复 ---
// 我们必须保存 port.name (内部ID/GUID),
// 而不是 port.portName (可视标签, 可能是"")
outputPortName = edge.output.name, // <-- 旧代码是: edge.output.portName
inputNodeGuid = inputNode.NodeData.guid,
inputPortName = edge.input.name // <-- 旧代码是: edge.input.portName
// --- 修复结束 ---
});
}
EditorUtility.SetDirty(graph);
AssetDatabase.SaveAssets();
editorWindow.OnGraphUpdated();
Debug.Log($"Graph '{graph.name}' saved successfully!");
}
// 泛型创建节点方法
protected void CreateNode(BaseNodeData data, Vector2 position)
{
if (graph == null)
{
EditorUtility.DisplayDialog("No Graph", "Please select a Dialogue Graph asset first.", "OK");
return;
}
// 初始化数据
data.guid = Guid.NewGuid().ToString();
data.position = position;
// 创建节点视图
var nodeView = CreateNodeView(data);
// 将节点添加到图表数据中 (保存时会再次保存,但在此处添加以便新建的节点可以立即连接)
// _graph.nodes.Add(data); // 暂时不加等SaveGraph统一处理
// 将节点视图添加到GraphView
AddElement(nodeView);
}
protected virtual BaseGraphNode CreateNodeView(BaseNodeData data)
{
return null; // 由子类实现
}
protected virtual GraphViewChange OnGraphViewChanged(GraphViewChange graphViewChange)
{
if (graph != null)
{
EditorUtility.SetDirty(graph);
editorWindow.OnGraphUpdated();
}
return graphViewChange;
}
}
public partial class GraphViewBase
{
[Serializable]
protected class CopyPasteData
{
[SerializeReference] // <-- 关键:确保多态性被正确序列化
public List<BaseNodeData> nodes = new List<BaseNodeData>();
public List<EdgeData> edges = new List<EdgeData>();
}
protected string OnSerializeGraphElements(IEnumerable<GraphElement> elements)
{
var nodesToCopy = new List<BaseNodeData>();
var edgesToCopy = new List<EdgeData>();
var selectedNodeGuids = new HashSet<string>(
elements.OfType<BaseGraphNode>().Select(n => n.NodeData.guid)
);
// 遍历所有选中的节点
foreach (var nodeView in elements.OfType<BaseGraphNode>())
{
// 使用 JsonUtility 创建一个深拷贝 (注意:这对于[SerializeReference]可能不完美,但对简单字段有效)
// 一个更健壮的方法是使用System.Text.Json或Newtonsoft.Json但我们先用Unity内置的
string nodeJson = JsonUtility.ToJson(nodeView.NodeData);
// 注意JsonUtility 不支持直接反序列化到基类,我们需要知道具体类型
// 这是一个简化,它可能无法正确深拷贝 [SerializeReference] 列表
// 让我们改变策略:我们只复制数据,在粘贴时创建新实例。
nodesToCopy.Add(nodeView.NodeData); // <-- 简化:直接添加引用
}
// 遍历所有选中的边
foreach (var edge in elements.OfType<Edge>())
{
var outputNode = (BaseGraphNode)edge.output.node;
var inputNode = (BaseGraphNode)edge.input.node;
if (selectedNodeGuids.Contains(outputNode.NodeData.guid) &&
selectedNodeGuids.Contains(inputNode.NodeData.guid))
{
edgesToCopy.Add(new EdgeData
{
outputNodeGuid = outputNode.NodeData.guid,
outputPortName = edge.output.name,
inputNodeGuid = inputNode.NodeData.guid,
inputPortName = edge.input.name
});
}
}
var copyData = new CopyPasteData
{
nodes = nodesToCopy,
edges = edgesToCopy
};
// 使用 JsonUtility 序列化
return JsonUtility.ToJson(copyData, true); // 'true' for pretty print
}
protected void OnUnserializeAndPaste(string operationName, string data)
{
if (string.IsNullOrEmpty(data))
{
return;
}
CopyPasteData pastedData;
try
{
pastedData = JsonUtility.FromJson<CopyPasteData>(data);
}
catch (Exception e)
{
Debug.LogError($"Failed to deserialize pasted data: {e.Message}");
return;
}
if (pastedData == null)
{
Debug.LogError("Pasted data is null.");
return;
}
var guidMap = new Dictionary<string, string>();
var nodeViewMap = new Dictionary<string, BaseGraphNode>();
// 1. 创建新节点 (并重新生成GUID)
foreach (var nodeData in pastedData.nodes)
{
var oldGuid = nodeData.guid;
var newGuid = Guid.NewGuid().ToString();
guidMap[oldGuid] = newGuid;
// --- 关键:创建数据的深拷贝 ---
// 我们必须创建一个新实例,否则粘贴的节点将引用与原始节点相同的数据
string nodeJson = JsonUtility.ToJson(nodeData);
BaseNodeData newData = (BaseNodeData)JsonUtility.FromJson(nodeJson, nodeData.GetType());
// --- 结束 ---
newData.guid = newGuid;
newData.position += new Vector2(20, 20);
var nodeView = CreateNodeView(newData);
AddElement(nodeView);
nodeViewMap[newGuid] = nodeView;
}
// 2. 创建新边
foreach (var edgeData in pastedData.edges)
{
// 检查旧GUID是否存在于映射中 (防止只复制一个节点和它的边)
if (!guidMap.ContainsKey(edgeData.outputNodeGuid) || !guidMap.ContainsKey(edgeData.inputNodeGuid))
{
continue;
}
string newOutputGuid = guidMap[edgeData.outputNodeGuid];
string newInputGuid = guidMap[edgeData.inputNodeGuid];
var outputNode = nodeViewMap[newOutputGuid];
var inputNode = nodeViewMap[newInputGuid];
Port outputPort = outputNode.outputContainer.Q<Port>(name: edgeData.outputPortName);
Port inputPort = inputNode.inputContainer.Q<Port>(name: edgeData.inputPortName);
if (outputPort == null || inputPort == null)
{
Debug.LogWarning($"Failed to find ports for pasted edge: {edgeData.outputPortName} -> {edgeData.inputPortName}");
continue;
}
Edge edge = outputPort.ConnectTo(inputPort);
AddElement(edge);
}
// OnGraphViewChanged 会自动处理 'dirty' 标记
}
protected bool OnCanPasteSerializedData(string data)
{
// (简单的检查)
return !string.IsNullOrEmpty(data) && data.Contains("nodes") && data.Contains("edges");
}
// --- 修复结束 ---
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 57749720ca7b80e479d90181d6499476