Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AntistraX · Apr 15, 2017 at 12:01 PM · c#listcustom editoredit mode

Why would pressing the play button delete or recreate a list elements?

I have MonoBehaviour with two lists for nodes and edges. Edge has links to nodes witch is ends of this. When i am creating edge it works currectly, but when i am pressing play button edge already has links to another copies of nodes... and it's all. So how can i fix it?

Edge class:

 using System;
 using UnityEngine;
 
 [Serializable]
 public class Edge {
 
     public Node[] nodes = new Node[2];
 }


Node class:

 using System;
 using UnityEngine;
 
 [Serializable]
 public class Node
 {
     public Vector3 position;
 }

Graph class:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class Graph : MonoBehaviour {
 
     public List<Node> nodes;
     public List<Edge> edges;
 }
 

GraphEditor class:

 using UnityEditor;
 using UnityEngine;
 
 [CustomEditor(typeof(Graph))]
 public class GraphEditor : Editor {
 
     Graph graph;
 
     private Node selectedNode;
     private Node connectedNode;
 
     private float handleSize = 0.1f;
     private float pickSize = 0.1f;
 
     private void OnEnable()
     {
         selectedNode = null;
         graph = (Graph)target;
     }
 
     private void OnSceneGUI()
     {
         var transform = graph.transform;
 
         for (int i = 0; i < graph.edges.Count; i++)
         {
             var edge = graph.edges[i];
             Handles.DrawLine(transform.TransformPoint(edge.nodes[0].position), transform.TransformPoint(edge.nodes[1].position));
         }
 
         for (int i = 0; i < graph.nodes.Count; i++)
         {
             var node = graph.nodes[i];
 
             var point = transform.TransformPoint(node.position);
             if (Handles.Button(point, Quaternion.identity, handleSize, pickSize, Handles.DotHandleCap))
             {
                 selectedNode = node;
 
                 if (connectedNode != null)
                 {
                     var edge = new Edge { nodes = new Node[] { selectedNode, connectedNode } };
                     graph.edges.Add(edge);
 
                     connectedNode = null;
                 }
             }
 
             if (selectedNode == node)
             {
                 EditorGUI.BeginChangeCheck();
                 point = Handles.DoPositionHandle(point, Quaternion.identity);
                 if (EditorGUI.EndChangeCheck())
                 {
                     Undo.RecordObject(graph, "Move Node");
                     node.position = transform.InverseTransformPoint(point);
                     EditorUtility.SetDirty(graph);
                 }
             }
         }
     }
 
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
         if (GUILayout.Button("Connect Nodes"))
         {
             Undo.RecordObject(graph, "Connect Nodes");
             EditorUtility.SetDirty(graph);
             connectedNode = selectedNode;
         }
     }
 }

Before play: alt text After: alt text

2.png (43.0 kB)
снимок.png (37.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Baroque · Apr 15, 2017 at 06:21 PM

It's important to realize that the serializer does not preserve references, except to subclasses of UnityEngine.Object. When it serializes out a list of Node references it will save a copy of those nodes. You should read the serialization documentation carefully when trying to save complex data structures. This blog post is also extremely useful.

You have a couple of options:

  1. You could change your system to be implemented in terms of ScriptableObject. You would have to manually register the newly constructed Nodes and Edges with the AssetDatabase but then you can store references to those objects from anywhere, including MonoBehaviours on scene objects.

  2. Store the indices of the vertices in your Edge structure and use those to store a naturally serializable version of the data structure. You can always reconstruct the references as a post-serialization step for optimization purposes.

How complex you plan on making your system will dictate which choice makes most sense. If this is intended to be lightweight and simple then I would use the second approach. If you want more advanced features--for example, being able to store subclasses of your Node type--then I would go with the first.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to find the element index from a list in Unity Inspector 0 Answers

A node in a childnode? 1 Answer

Custom Editor DragAndDrop for List 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges