using System.Linq; using System.Collections.Generic; using UnityEngine; using…
- User
using System.Linq; using System.Collections.Generic; using UnityEngine; using UnityEditor.Experimental.GraphView; using MeetAndTalk.Nodes; using UnityEngine.UIElements; #if UNITY_EDITOR using UnityEditor; #endif
namespace MeetAndTalk.Editor { public class DialogueSaveAndLoad { private DialogueGraphView graphView;
public DialogueSaveAndLoad(DialogueGraphView graphView) { this.graphView = graphView; }#if UNITY_EDITOR public void Save(DialogueContainerSO dialogueContainerSO) { // Очищаем данные в контейнере dialogueContainerSO.NodeLinkDatas.Clear(); dialogueContainerSO.DialogueNodeDatas.Clear(); dialogueContainerSO.EndNodeDatas.Clear(); dialogueContainerSO.StartNodeDatas.Clear();
// Собираем ноды и связи var nodes = graphView.nodes.ToList(); var edges = graphView.edges.ToList(); // Сохраняем связи между нодами foreach (var edge in edges) { if (edge.input.node is BaseNode inputNode && edge.output.node is BaseNode outputNode) { dialogueContainerSO.NodeLinkDatas.Add(new NodeLinkData { BaseNodeGuid = outputNode.nodeGuid, TargetNodeGuid = inputNode.nodeGuid }); } } // Сохраняем данные нод foreach (var node in nodes) { if (node is DialogueNode dialogueNode) { dialogueContainerSO.DialogueNodeDatas.Add(CreateDialogueNodeData(dialogueNode)); } else if (node is StartNode startNode) { dialogueContainerSO.StartNodeDatas.Add(CreateStartNodeData(startNode)); } else if (node is EndNode endNode) { dialogueContainerSO.EndNodeDatas.Add(CreateEndNodeData(endNode)); } } EditorUtility.SetDirty(dialogueContainerSO); AssetDatabase.SaveAssets(); } public void Load(DialogueContainerSO dialogueContainerSO) { ClearGraph(); // Создаем ноды foreach (var nodeData in dialogueContainerSO.DialogueNodeDatas) { DialogueNode node = graphView.CreateDialogueNode(nodeData.Position); node.nodeGuid = nodeData.NodeGuid; LoadNodeData(node, nodeData); graphView.AddElement(node); } foreach (var nodeData in dialogueContainerSO.StartNodeDatas) { StartNode node = graphView.CreateStartNode(nodeData.Position); node.nodeGuid = nodeData.NodeGuid; graphView.AddElement(node); } foreach (var nodeData in dialogueContainerSO.EndNodeDatas) { EndNode node = graphView.CreateEndNode(nodeData.Position); node.nodeGuid = nodeData.NodeGuid; node.EndNodeType = nodeData.EndNodeType; node.LoadValueInToField(); graphView.AddElement(node); } // Соединяем ноды var allNodes = graphView.nodes.ToList().OfType<BaseNode>().ToList(); foreach (var linkData in dialogueContainerSO.NodeLinkDatas) { BaseNode outputNode = allNodes.FirstOrDefault(n => n.nodeGuid == linkData.BaseNodeGuid); BaseNode inputNode = allNodes.FirstOrDefault(n => n.nodeGuid == linkData.TargetNodeGuid); if (outputNode != null && inputNode != null) { Port outputPort = outputNode.outputContainer.Query<Port>().First(); Port inputPort = inputNode.inputContainer.Query<Port>().First(); Edge newEdge = new Edge { output = outputPort, input = inputPort }; inputPort.Connect(newEdge); outputPort.Connect(newEdge); graphView.Add(newEdge); } } } private void ClearGraph() { graphView.nodes.ForEach(node => graphView.RemoveElement(node)); graphView.edges.ForEach(edge => graphView.RemoveElement(edge)); } // Методы создания и загрузки данных для каждого типа ноды private DialogueNodeData CreateDialogueNodeData(DialogueNode node) { return new DialogueNodeData { NodeGuid = node.nodeGuid, Position = node.GetPosition().position, TextType = node.Texts, AudioClips = node.AudioClips, Duration = node.DurationShow, TextEffects = node.TextEffects }; } private void LoadNodeData(DialogueNode node, DialogueNodeData nodeData) { node.Texts.Clear(); node.Texts.AddRange(nodeData.TextType); node.AudioClips.Clear(); node.AudioClips.AddRange(nodeData.AudioClips); node.DurationShow = nodeData.Duration; node.TextEffects = nodeData.TextEffects; node.LoadValueInToField(); } private StartNodeData CreateStartNodeData(StartNode node) { return new StartNodeData { NodeGuid = node.nodeGuid, Position = node.GetPosition().position }; } private EndNodeData CreateEndNodeData(EndNode node) { return new EndNodeData { NodeGuid = node.nodeGuid, Position = node.GetPosition().position, EndNodeType = node.EndNodeType }; }#endif } }
// DialogueNodeData.cs using System.Collections.Generic; using UnityEngine;
namespace MeetAndTalk { [System.Serializable] public class DialogueNodeData : BaseNodeData { public List<LanguageGeneric> AudioClips; public List<LanguageGeneric> TextType; public float Duration; public List TextEffects = new List(); } }
Assets\Meet and Talk\Editor\DialogueSaveAndLoad.cs(53,63): error CS1503: Argument 1: cannot convert from 'MeetAndTalk.DialogueNodeData [F:\Dowloads\TestNomer1\Assets\Meet and Talk\Editor\Node\DialogueNodeData.cs(7)]' to 'MeetAndTalk.DialogueNodeData [F:\Dowloads\TestNomer1\Library\Bee\artifacts\1900b0aEDbg.dag\Assembly-CSharp.ref.dll]'
Assets\Meet and Talk\Editor\DialogueSaveAndLoad.cs(78,36): error CS1503: Argument 2: cannot convert from 'MeetAndTalk.DialogueNodeData [F:\Dowloads\TestNomer1\Library\Bee\artifacts\1900b0aEDbg.dag\Assembly-CSharp.ref.dll]' to 'MeetAndTalk.DialogueNodeData [F:\Dowloads\TestNomer1\Assets\Meet and Talk\Editor\Node\DialogueNodeData.cs(7)]'
Исрпавь данные ошибки, напиши полный код
- Assistant
The errors you're encountering are due to the
DialogueNodeDataclass being defined in both the runtime assembly and the editor assembly. In Unity, scripts inside anEditorfolder are compiled into a separate assembly (Assembly-CSharp-Editor), while scripts outside are compiled intoAssembly-CSharp. If you define the same class in both assemblies, you'll run into type mismatch errors because the compiler treats them as different types, even though they have the same name and namespace.To fix the errors, you need to ensure that the
DialogueNodeDataclass is only defined once and is accessible to both the runtime and editor code. The best practice is to place data classes likeDialogueNodeDataoutside of theEditorfolder so that they're included in the runtime assembly. Then, the editor scripts can reference them without any issues.Here's how you can fix the errors:
-
Move
DialogueNodeData.csOut of theEditorFolder:Move the
DialogueNodeData.csfile fromAssets\Meet and Talk\Editor\Node\to a folder outside ofEditor, such asAssets\Meet and Talk\Scripts\NodeData\. -
Update Namespaces:
Ensure that the namespace of
DialogueNodeDatamatches across all files and that it's accessible from both editor and runtime scripts. -
Check Assembly Definitions (If Any):
If you're using assembly definition files (
.asmdef), make sure that the assemblies are correctly referencing each other. The runtime assembly should not reference the editor assembly, but the editor assembly can reference the runtime assembly. -
Adjust Using Directives:
In your editor scripts, update the
usingdirectives to correctly reference theDialogueNodeDataclass.
After making these changes, the compiler should recognize that there's only one
DialogueNodeDataclass, and the errors should be resolved.Below is the corrected and complete code:
DialogueNodeData.cs (Move this file to
Assets\Meet and Talk\Scripts\NodeData\)using System.Collections.Generic; using UnityEngine; namespace MeetAndTalk { [System.Serializable] public class DialogueNodeData : BaseNodeData { public List<LanguageGeneric<AudioClip>> AudioClips; public List<LanguageGeneric<string>> TextType; public float Duration; public List<TextEffectData> TextEffects = new List<TextEffectData>(); } }
DialogueSaveAndLoad.cs
using System.Linq; using System.Collections.Generic; using UnityEngine; using UnityEditor.Experimental.GraphView; using MeetAndTalk.Nodes; using UnityEngine.UIElements; #if UNITY_EDITOR using UnityEditor; #endif namespace MeetAndTalk.Editor { public class DialogueSaveAndLoad { private DialogueGraphView graphView; public DialogueSaveAndLoad(DialogueGraphView graphView) { this.graphView = graphView; } #if UNITY_EDITOR public void Save(DialogueContainerSO dialogueContainerSO) { // Clear data in the container dialogueContainerSO.NodeLinkDatas.Clear(); dialogueContainerSO.DialogueNodeDatas.Clear(); dialogueContainerSO.EndNodeDatas.Clear(); dialogueContainerSO.StartNodeDatas.Clear(); // Collect nodes and edges var nodes = graphView.nodes.ToList(); var edges = graphView.edges.ToList(); // Save node links foreach (var edge in edges) { if (edge.input.node is BaseNode inputNode && edge.output.node is BaseNode outputNode) { dialogueContainerSO.NodeLinkDatas.Add(new NodeLinkData { BaseNodeGuid = outputNode.nodeGuid, TargetNodeGuid = inputNode.nodeGuid }); } } // Save node data foreach (var node in nodes) { if (node is DialogueNode dialogueNode) { dialogueContainerSO.DialogueNodeDatas.Add(CreateDialogueNodeData(dialogueNode)); } else if (node is StartNode startNode) { dialogueContainerSO.StartNodeDatas.Add(CreateStartNodeData(startNode)); } else if (node is EndNode endNode) { dialogueContainerSO.EndNodeDatas.Add(CreateEndNodeData(endNode)); } } EditorUtility.SetDirty(dialogueContainerSO); AssetDatabase.SaveAssets(); } public void Load(DialogueContainerSO dialogueContainerSO) { ClearGraph(); // Create nodes foreach (var nodeData in dialogueContainerSO.DialogueNodeDatas) { DialogueNode node = graphView.CreateDialogueNode(nodeData.Position); node.nodeGuid = nodeData.NodeGuid; LoadNodeData(node, nodeData); graphView.AddElement(node); } foreach (var nodeData in dialogueContainerSO.StartNodeDatas) { StartNode node = graphView.CreateStartNode(nodeData.Position); node.nodeGuid = nodeData.NodeGuid; graphView.AddElement(node); } foreach (var nodeData in dialogueContainerSO.EndNodeDatas) { EndNode node = graphView.CreateEndNode(nodeData.Position); node.nodeGuid = nodeData.NodeGuid; node.EndNodeType = nodeData.EndNodeType; node.LoadValueInToField(); graphView.AddElement(node); } // Connect nodes var allNodes = graphView.nodes.ToList().OfType<BaseNode>().ToList(); foreach (var linkData in dialogueContainerSO.NodeLinkDatas) { BaseNode outputNode = allNodes.FirstOrDefault(n => n.nodeGuid == linkData.BaseNodeGuid); BaseNode inputNode = allNodes.FirstOrDefault(n => n.nodeGuid == linkData.TargetNodeGuid); if (outputNode != null && inputNode != null) { Port outputPort = outputNode.outputContainer.Query<Port>().First(); Port inputPort = inputNode.inputContainer.Query<Port>().First(); Edge newEdge = new Edge { output = outputPort, input = inputPort }; inputPort.Connect(newEdge); outputPort.Connect(newEdge); graphView.Add(newEdge); } } } private void ClearGraph() { // Remove all nodes foreach (var node in graphView.nodes.ToList()) { graphView.RemoveElement(node); } // Remove all edges foreach (var edge in graphView.edges.ToList()) { graphView.RemoveElement(edge); } } // Methods to create and load data for each node type private DialogueNodeData CreateDialogueNodeData(DialogueNode node) { return new DialogueNodeData { NodeGuid = node.nodeGuid, Position = node.GetPosition().position, TextType = node.Texts, AudioClips = node.AudioClips, Duration = node.DurationShow, TextEffects = node.TextEffects }; } private void LoadNodeData(DialogueNode node, DialogueNodeData nodeData) { node.Texts.Clear(); node.Texts.AddRange(nodeData.TextType); node.AudioClips.Clear(); node.AudioClips.AddRange(nodeData.AudioClips); node.DurationShow = nodeData.Duration; node.TextEffects = nodeData.TextEffects; node.LoadValueInToField(); } private StartNodeData CreateStartNodeData(StartNode node) { return new StartNodeData { NodeGuid = node.nodeGuid, Position = node.GetPosition().position }; } private EndNodeData CreateEndNodeData(EndNode node) { return new EndNodeData { NodeGuid = node.nodeGuid, Position = node.GetPosition().position, EndNodeType = node.EndNodeType }; } #endif } }
Additional Notes:
-
Namespaces: Ensure that all classes are under the correct namespace. For example, if you're using
namespace MeetAndTalk, both your editor and runtime scripts should reference this namespace correctly. -
Assembly Definitions: If you're using
.asmdeffiles, make sure that your editor assembly has a reference to the runtime assembly whereDialogueNodeDatais defined. Do not have the runtime assembly reference the editor assembly. -
Using Directives: Double-check your
usingstatements. Make sure you're using the correct namespaces and that there are no namespace conflicts. -
Editor Folder: Remember that any script inside an
Editorfolder is excluded from builds and compiled into a separate assembly. Data scripts needed at runtime should not be placed inside anEditorfolder.
Example Folder Structure:
- Assets
- Meet and Talk
- Editor (All editor scripts)
- DialogueGraphView.cs
- DialogueSaveAndLoad.cs
- Other editor scripts...
- Scripts
- NodeData (All data classes used at runtime)
- DialogueNodeData.cs
- StartNodeData.cs
- EndNodeData.cs
- BaseNodeData.cs
- Nodes (Runtime node classes)
- DialogueNode.cs
- StartNode.cs
- EndNode.cs
- BaseNode.cs
- Other Scripts
- LanguageGeneric.cs
- TextEffectData.cs
- ScriptableObjects
- DialogueContainerSO.cs (Your scriptable object class)
- NodeData (All data classes used at runtime)
- Resources (Assets like AudioClips, etc.)
- Editor (All editor scripts)
- Meet and Talk
By organizing your scripts this way, you ensure that both the editor and runtime code can access the necessary classes without any conflicts.
If you follow these steps, the errors should be resolved. Let me know if you need further assistance or if you encounter any other issues.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.