Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

using System.Linq; using System.Collections.Generic; using UnityEngine; using…

  1. 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)]'

    Исрпавь данные ошибки, напиши полный код

  2. Assistant

    The errors you're encountering are due to the DialogueNodeData class being defined in both the runtime assembly and the editor assembly. In Unity, scripts inside an Editor folder are compiled into a separate assembly (Assembly-CSharp-Editor), while scripts outside are compiled into Assembly-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 DialogueNodeData class is only defined once and is accessible to both the runtime and editor code. The best practice is to place data classes like DialogueNodeData outside of the Editor folder 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:

    1. Move DialogueNodeData.cs Out of the Editor Folder:

      Move the DialogueNodeData.cs file from Assets\Meet and Talk\Editor\Node\ to a folder outside of Editor, such as Assets\Meet and Talk\Scripts\NodeData\.

    2. Update Namespaces:

      Ensure that the namespace of DialogueNodeData matches across all files and that it's accessible from both editor and runtime scripts.

    3. 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.

    4. Adjust Using Directives:

      In your editor scripts, update the using directives to correctly reference the DialogueNodeData class.

    After making these changes, the compiler should recognize that there's only one DialogueNodeData class, 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 .asmdef files, make sure that your editor assembly has a reference to the runtime assembly where DialogueNodeData is defined. Do not have the runtime assembly reference the editor assembly.

    • Using Directives: Double-check your using statements. Make sure you're using the correct namespaces and that there are no namespace conflicts.

    • Editor Folder: Remember that any script inside an Editor folder is excluded from builds and compiled into a separate assembly. Data scripts needed at runtime should not be placed inside an Editor folder.


    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)
        • Resources (Assets like AudioClips, etc.)

    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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.