- Home /
Serializable class doesn't store list<>
Hi everyone, I have a problem, that I can't store the list "distances" inside my serializable class. The UnityEditor "forgets" it after a cycle. The class RAYpoint on the other hand is stored ,therefore I can get x,y,z. Guess its a problem with list in general. I tried some solutions offered by other users, sadly so far, I didn't get it to work. Hope someone can point me in the right direction. Cheers
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class RAYpoint{
public RAYpoint(Vector3 vec) { vector = vec; }
public Vector3 vector;
public bool updated = false;
[SerializeField]
public List<_DistancePoint> distances = new List<_DistancePoint>();
public RAYpoint[] neighbors = new RAYpoint[0];
public void reset(){
distances = new List<_DistancePoint>();
neighbors = new RAYpoint[0];
}
public RAYpoint[] GetNeighbors(){
if (!updated) {
SortDistance ();
neighbors = new RAYpoint[distances.Count];
for(int i = 0; i < distances.Count; i++){
neighbors[i] = distances[i].point;
}
}
return neighbors;
}
public void AddDistance(float distance, RAYpoint point){
updated = false;
if (distances == null) {
distances = new List<_DistancePoint>();
}
distances.Add (new _DistancePoint (distance, point));
}
public void SortDistance(){
if (!updated) {
distances.Sort(
delegate(_DistancePoint p1, _DistancePoint p2) {
return p1.distance.CompareTo (p2.distance);
});
}
updated = true;
}
public float x {
get {
return vector.x;
}
set
{
vector = new Vector3(value, vector.y, vector.z);
}
}
public float y {
get {
return vector.y;
}
set
{
vector = new Vector3(vector.x, value , vector.z);
}
}
public float z {
get {
return vector.z;
}
set
{
vector = new Vector3(vector.x, vector.y, value);
}
}
public static implicit operator Vector3(RAYpoint obj){
return obj.vector;
}
// User-defined conversion from double to Digit
public static implicit operator RAYpoint(Vector3 vector){
return new RAYpoint(vector);
}
public override string ToString(){
return vector.ToString();
}
/**************************************/
[System.Serializable]
public class _DistancePoint{
public float distance { get; set; }
public RAYpoint point { get; set; }
public _DistancePoint(float distance, RAYpoint point){
this.distance = distance;
this.point = point;
}
public override string ToString(){
return "Distance: " + distance + " Point: " + point;
}
}
}
Answer by frarees · Oct 27, 2014 at 02:39 PM
Make sure the _DistancePoint
class is marked with System.Serializable
, and the fields are serializable by Unity as well.
Your answer
Follow this Question
Related Questions
Assets Returning Null 0 Answers
ScriptableObject with Custom Editor resetting data in inspector 1 Answer
How to Serialize and Deserialize a Dictionary Containing Gameobject and a Vector 0 Answers
Error when trying to Serialize a field that is in a class 0 Answers
PropertyDrawer and EditorWindow 1 Answer