- Home /
[Possible Bug] Unity3d XmlSerializer ShouldSerialize not working?
Summary: ShouldSerialize not working with unity but works in normal c# app
Basically i tried serializing my class into xml for save file. i tried using ShouldSerialize*() so that it only serialize when it's true or something. But the xml file result ignore the ShouldSerialize and write it anyway. I tried making simple test using both Unity3d and Visual Studio c# console app. Unity failed.
Unity3d 5.6.0f3
Visual Studio 2017
Unity Project totally new with no other class beside the one attached
I Literally Copy-Paste the code and both succesfully made the file (except unity one still serialize the variable)
Made using Visual Studio 2017 console app (ShouldSerializeLever works)
using System;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
ExampleA test = new ExampleA();
test.Save(Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"WithoutUnity.xml"));
}
}
public class ExampleA
{
public bool Lever = false;
public bool ShouldSerializeLever()
{
return false;
}
public void Save(string path)
{
var serializer = new XmlSerializer(typeof(ExampleA));
using (var stream = new FileStream(path, FileMode.Create))
{
serializer.Serialize(stream, this);
}
}
}
}
Made using Unity3d (ShouldSerializeLever doesn't works)
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
[System.Serializable]
public class TestScript : MonoBehaviour
{
void Start()
{
ExampleA test = new ExampleA();
test.Save(Path.Combine(Application.dataPath, "WithUnity.xml"));
}
public class ExampleA
{
public bool Lever = false;
public bool ShouldSerializeLever()
{
return false;
}
public void Save(string path)
{
var serializer = new XmlSerializer(typeof(ExampleA));
using (var stream = new FileStream(path, FileMode.Create))
{
serializer.Serialize(stream, this);
}
}
}
}
Answer by RalphWeis · Jul 28, 2017 at 12:05 AM
After upgrading my Unity to 2017.1.0f3 and changing the Scripting Runtime version to experimental (.NET 4.6 equivalent) the ShouldSerialize works.
After few digging apparently Mono's System.Xml.Serialization add the ShouldSerialize*() option at Sep 5 2014 (around mono 3.x) while Unity use Mono2x (Mono 2.6)
Source: (github history and the commit)
I guessing since Unity still use older version of mono the ShouldSerialize hasn't been implemented yet
Answer by FortisVenaliter · Jul 27, 2017 at 09:32 PM
Visual Studio compiles the code with the .NET compiler. Unity uses a modified version of the Mono compiler, so they are not 1-to-1 with functionality.
Also, I've never seen that ShouldSerialize*() syntax before... Especially without some kind of generics or interface... but I'm not very well versed with serialization.
Anyway, look here for how Unity determines what gets serialized and not.
I use ShouldSerialize*() so that if the variable value is not what i want it wont be written my my game save file.
Look at the contents bar to the left of that article. The whole article only applies to WinForm controls or windows, so it has nothing to do with Unity and won't work here, because Unity doesn't even recognize WinForms as far as I know.
$$anonymous$$y code on top doesn't use System.Windows.Forms but still works Another example via google https://msdn.microsoft.com/en-us/library/dd585968(v=office.11).aspx
Your answer
![](https://koobas.hobune.stream/wayback/20220612131336im_/https://answers.unity.com/themes/thub/images/avi.jpg)