Jump to content
  • Advertisement
Sign in to follow this  
linternet

Unity C# question

This topic is 3605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm a C++ programmer using C#/XNA for the indie game I'm developing because Microsoft's Community Games is too good of an opportunity to pass up. I'm trying to take advantage of XML Serialization and Property Grids for use in my data format/toolset, but putting get/set properties on every memeber is not sitting well with my tendency to publically expose as little of my objects as possible. Is there a way to avoid doint that or do I just need to suck it up so that I don't have to write my own serialization routines and property dialog boxes?

Share this post


Link to post
Share on other sites
Advertisement
So you want serialization without publicly exposing all members you want to serialize? From what I remember, binary serialization can do that.

Share this post


Link to post
Share on other sites
you can implement the ISerializable interface directly, instead of just trusting serialization to do the right thing automagically. Also, you can mark individual items with NonSerialized as appropriate.

Share this post


Link to post
Share on other sites
All members of the class not marked with [NonSerialzable] will be serialized with a BinaryFormmater. Unfortunately XML serialization requires public properties. So if you don't care about the format of the saved data, use Binary (it will also do nested classes but they must be marked [Serializable] or you will get exceptions. The majority of framework classes are serializable). Otherwise you have the other option of implementing ISerializable as Xai said. I find BinaryFormatter suits most of my needs.

You will also find that after you serialize binary that the names of classes etc are plainly visible in your file if you hex edit or view in notepad. You can nest the stream inside a zip stream and / or encryption before you send it to disc as well.

Share this post


Link to post
Share on other sites
The properties need to be public on the concrete class, they do not need to be part of the interface at all or they can be read-only in the interface.

You can tag public properties with [Browseable(false)] so they do not show up on the property grid and add more property-grid-friendly properties to replace them.
e.g. One that returns "127.0.0.1" instead of 0100007F.

Share this post


Link to post
Share on other sites
Quote:
Original post by Shannon Barber
The properties need to be public on the concrete class, they do not need to be part of the interface at all or they can be read-only in the interface.

You can tag public properties with [Browseable(false)] so they do not show up on the property grid and add more property-grid-friendly properties to replace them.
e.g. One that returns "127.0.0.1" instead of 0100007F.


you can also tackle more complex versions of this desire by writing custom Designer classes, instead of hiding properties.

Share this post


Link to post
Share on other sites
I usually create dedicated data classes for information that needs to be serialized that are held within classes responsible for logic to reduce the problems of making all the methods public.

Share this post


Link to post
Share on other sites
Early warning, though I'm not sure what you're planning: At least in XNA 2.0, the 360's framework did not include the BinaryFormatter class or the FormatterServices class necessary to write a good serializer of your own. I haven't re-checked in XNA 3.0 yet, though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nypyren
Early warning, though I'm not sure what you're planning: At least in XNA 2.0, the 360's framework did not include the BinaryFormatter class or the FormatterServices class necessary to write a good serializer of your own. I haven't re-checked in XNA 3.0 yet, though.


It won't work. The BinaryFormatter is not part of the XNA Framework, but rather part of the main class library. The XBox 360 uses the .NET Compact Framework, which does not have the BinaryFormatter class.

You probably want to use the IntermediateSerializer class to create the XML files to be processed by the Content Processor for your game. Shawn Hargreaves, one of the XNA developers, has several blog postings going into great detail on how to use this class. This one in particular goes into details on how to serialize private members, collections, and controlling the XML output among other things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!