- Home /
Serialize a LineRenderer
I'm trying to send a LineRenderer over the network with RPC calls. Since there is no way to get information out of a line renderer after it is created, I can't just get the points and do something clever like converting them to a byte array and sending that. I thought about using an XmlSerializer to convert the data to a string and send that, but I'm getting an error when attempting to serialize:
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(LineRenderer));
The above throws an error:
InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. UnityEngine.Transform does not implement Add(System.Object).
What I don't understand is: at no point does LineRenderer inherit from UnityEngine.Transform. If you follow the inheritance it goes LineRenderer->Renderer->Component->UnityEngine.Object. None of these appear to implement the IEnumerable interface.
Does anyone wiser than I in the ways of C# know what's going on here?
Also, FYI: there is no straightforward way within the framework of our project to simply save the point values of the line renderers as we're creating them. If there is no way to send LineRenderers or LR info through RPCs I will figure out how to save the point data separately, but I'd prefer not to.
The compiler is complaining about the transform property from LineRenderer (inherited from Component). To use XmlSerializer all public properties and fields must be serializable, so I think you will need to do it by hand.
Answer by Julien-Lynge · Dec 21, 2011 at 09:13 PM
As @Luiz said:
The compiler is complaining about the transform property from LineRenderer (inherited from Component). To use XmlSerializer all public properties and fields must be serializable, so I think you will need to do it by hand.
That solves it - I wasn't understanding what Unity was trying to tell me. It was having an issue with the inheritance of a variable within the class it was trying to serialize, not the class itself.
Unfortunately, there is no way to serialize a LineRenderer by hand, since you can't get at any of the LineRenderer data (there are no public variables or methods that return variables). I was hoping that I could get around that issue by serializing, but it appears not.
Answer by luizgpa · Dec 24, 2011 at 03:49 AM
There no private members to read the data either. But you could create a class that holds the data needed to recreate the LineRenderer: a list of points, two colors and two width values. Something like:
[Serializable]
public class LineRendererData
{
public List<Vector3> points;
public float startWidth, endWidth;
public Color startColor, endColor;
public void UpdateLineRenderer(LineRenderer renderer)
{
renderer.SetVertexCount(points.Count);
for(int i = 0; i < points.Count; ++i)
renderer.SetPosition(i, points[i]);
renderer.SetColors(startColor, endColor);
renderer.SetWidth(startWidth, endWidth);
}
}
If you need to create the points on the Editor you could write a script that has a public LineRendererData field, executes on edit mode and calls UpdateLineRenderer when the data was changed.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Line renderer corner issue (Gui Shader) 0 Answers
unity c# RPC 0 Answers