Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Julien-Lynge · Dec 19, 2011 at 10:16 PM · c#linerendererrpcxmlserializerienumerable

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?

Comment
Add comment · Show 2
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
avatar image Julien-Lynge · Dec 19, 2011 at 10:20 PM 0
Share

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.

avatar image luizgpa · Dec 21, 2011 at 06:47 PM 0
Share

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.

2 Replies

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

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.

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
avatar image
1

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

network.instantiating players using RPC calls 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