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 type conflicts where the
DialogueNodeDataclass is being recognized from two different assemblies. This typically happens when a class is defined in both runtime and editor code or placed incorrectly within the project structure. To resolve these issues, follow the steps below:-
Ensure Proper Placement of Scripts:
- Runtime Scripts: These scripts should not be inside any
Editorfolders. They are compiled into theAssembly-CSharp.dll. - Editor Scripts: These should be inside an
Editorfolder and are compiled into a separate assembly.
Your
DialogueNodeData.csshould be placed in a runtime folder, such asAssets/MeetAndTalk/Scripts/, and not inside anyEditorfolder. - Runtime Scripts: These scripts should not be inside any
-
Avoid Duplicate Definitions:
- Ensure that
DialogueNodeDatais only defined once in your project. - If it's mistakenly placed in both runtime and editor folders, Unity will compile them into separate assemblies, causing type conflicts.
- Ensure that
-
Ensure Correct Namespaces and References:
- Your editor scripts should reference runtime types correctly.
- Avoid redefining runtime classes within editor scripts.
-
Review and Correct Your Scripts:
Below are the corrected versions of your scripts ensuring that
DialogueNodeDatais only defined once and properly referenced in the editor scripts.
1. DialogueSaveAndLoad.cs (Editor Script)
Path:
Assets/Meet and Talk/Editor/DialogueSaveAndLoad.csusing 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) { if (dialogueContainerSO == null) { Debug.LogError("DialogueContainerSO is null. Cannot save data."); return; } // Clear existing data dialogueContainerSO.NodeLinkDatas.Clear(); dialogueContainerSO.DialogueNodeDatas.Clear(); dialogueContainerSO.EndNodeDatas.Clear(); dialogueContainerSO.StartNodeDatas.Clear(); // Gather 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)); } } // Mark as dirty and save assets EditorUtility.SetDirty(dialogueContainerSO); AssetDatabase.SaveAssets(); } public void Load(DialogueContainerSO dialogueContainerSO) { if (dialogueContainerSO == null) { Debug.LogError("DialogueContainerSO is null. Cannot load data."); return; } ClearGraph(); // Create dialogue nodes foreach (var nodeData in dialogueContainerSO.DialogueNodeDatas) { DialogueNode node = graphView.CreateDialogueNode(nodeData.Position); node.nodeGuid = nodeData.NodeGuid; LoadNodeData(node, nodeData); graphView.AddElement(node); } // Create start nodes foreach (var nodeData in dialogueContainerSO.StartNodeDatas) { StartNode node = graphView.CreateStartNode(nodeData.Position); node.nodeGuid = nodeData.NodeGuid; graphView.AddElement(node); } // Create end nodes 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 }; newEdge.input.Connect(newEdge); newEdge.output.Connect(newEdge); graphView.Add(newEdge); } } } private void ClearGraph() { var elements = graphView.graphElements.ToList(); foreach (var element in elements) { graphView.RemoveElement(element); } } // Methods to create data for each node type private DialogueNodeData CreateDialogueNodeData(DialogueNode node) { return new DialogueNodeData { NodeGuid = node.nodeGuid, Position = node.GetPosition().position, TextType = new List<LanguageGeneric<string>>(node.Texts), AudioClips = new List<LanguageGeneric<AudioClip>>(node.AudioClips), Duration = node.DurationShow, TextEffects = new List<TextEffectData>(node.TextEffects) }; } 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 }; } // Method to load data into a DialogueNode 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 = new List<TextEffectData>(nodeData.TextEffects); node.LoadValueInToField(); } #endif } }
2. DialogueNodeData.cs (Runtime Script)
Path:
Assets/Meet and Talk/Scripts/DialogueNodeData.csusing System.Collections.Generic; using UnityEngine; namespace MeetAndTalk { [System.Serializable] public class DialogueNodeData : BaseNodeData { public List<LanguageGeneric<AudioClip>> AudioClips = new List<LanguageGeneric<AudioClip>>(); public List<LanguageGeneric<string>> TextType = new List<LanguageGeneric<string>>(); public float Duration; public List<TextEffectData> TextEffects = new List<TextEffectData>(); } [System.Serializable] public class StartNodeData : BaseNodeData { // Add any additional fields if necessary } [System.Serializable] public class EndNodeData : BaseNodeData { public EndNodeType EndNodeType; } [System.Serializable] public class NodeLinkData { public string BaseNodeGuid; public string TargetNodeGuid; } [CreateAssetMenu(fileName = "DialogueContainer", menuName = "MeetAndTalk/DialogueContainerSO")] public class DialogueContainerSO : ScriptableObject { public List<DialogueNodeData> DialogueNodeDatas = new List<DialogueNodeData>(); public List<StartNodeData> StartNodeDatas = new List<StartNodeData>(); public List<EndNodeData> EndNodeDatas = new List<EndNodeData>(); public List<NodeLinkData> NodeLinkDatas = new List<NodeLinkData>(); } // Assuming BaseNodeData is defined elsewhere in your project [System.Serializable] public class BaseNodeData { public string NodeGuid; public Vector2 Position; } [System.Serializable] public class LanguageGeneric<T> { // Define your language-specific implementation here // Example: public string Language; public T Data; } [System.Serializable] public class TextEffectData { // Define your text effect properties here } public enum EndNodeType { Type1, Type2, // Add other types as needed } }
3. Additional Considerations
-
BaseNode and Derived Classes: Ensure that your
BaseNode,DialogueNode,StartNode, andEndNodeclasses are correctly defined and inherit from appropriate Unity GraphView classes. They should be placed in runtime folders unless they are strictly editor-only. -
LanguageGeneric Class: The
LanguageGeneric<T>class should be defined to handle language-specific data. Ensure it's correctly implemented to manage different languages if your dialogue system supports localization. -
ScriptableObject (
DialogueContainerSO): TheDialogueContainerSOScriptableObject holds all the data for your dialogue system. Make sure to create instances of this asset via the Unity Editor using the providedCreateAssetMenuattribute. -
Handling Ports and Edges: When connecting ports and creating edges in the
Loadmethod, ensure that port queries (outputContainer.Query<Port>().First()) correctly identify the output and input ports. Depending on your node setup, you might need to refine the queries to target specific ports. -
Error Checking: Add additional error checking as needed to handle cases where data might be missing or corrupted during the load process.
4. Folder Structure Recommendation
To prevent such conflicts in the future, it's essential to organize your project folders correctly. Here's a recommended structure:
Assets/ ├── MeetAndTalk/ │ ├── Scripts/ │ │ ├── DialogueNodeData.cs │ │ ├── DialogueGraphView.cs │ │ └── ... other runtime scripts │ └── Editor/ │ ├── DialogueSaveAndLoad.cs │ └── ... other editor scripts- Scripts Folder: Contains all runtime scripts accessible during gameplay.
- Editor Folder: Contains all editor-specific scripts. These scripts can reference runtime scripts but not the other way around.
By following the above steps and ensuring that your scripts are correctly organized and referenced, the type conflicts should be resolved, and your dialogue save and load functionality should work as expected.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.