- Home /
Serialization of GameObject
I found pretty much nothing from search results because I believe this never really happened to anyone. But the thing is I have precompiled dll that contains game data and therefore I cannot change the design of serialization. So, I have access to PlayerData that has all info, even ActiveBuilding (the problem), but on Serialization of PlayerData this list just doesn't produce anything...
The problem:
using (FileStream fs = new FileStream(SerializationPath, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, PlayerData.PDataRef);
}
Produces
"Buildings": [
{
"instanceID": 10784
}
The array that I need to be serialized (in PlayerData).
[SerializeField] public List<ActiveBuilding> Buildings = new List<ActiveBuilding>();
Active Buildings has the following structure.
public abstract class ActiveBuilding : MonoBehaviour, IUnlockable
{
[SerializeField]
private int _Free;
[SerializeField]
private int _Owned;
[SerializeField]
private double CostBase = 10;
[SerializeField]
[Tooltip("Set in editor! How much user needs to pay for building with scale.")]
private float CostScale = 1.1f;
}
Specifically referring to class inheriting ActiveBuilding solves the issue and produces...
{
"_Free": 1,
"_Owned": 1,
"CostBase": 10.0,
"CostScale": 1.100000023841858
}
I cannot refer to objects individually, because I only have 1 stream, but also because I have not access to PlayerData source. How to get around this?
Answer by kilanth13 · Aug 09, 2019 at 02:27 PM
For anyone who is still interested. The only proper way to address it would be to manually use reflection, collecting that data that is required. A fair and Object-Oriented way would be the method below. As there was no direct solution to this but serialization, an intermediate step needed to be done. A ShadowBuilding class was created that contains data of all relevant fields that needed to be serialized.
[Serializable]
public class BuildingShadow
{
public int Free;
public int Owned;
public double CostBase;
private float CostScale;
}
This class is constructed every time before serialization and deconstructed after deserialization.
Your answer
![](https://koobas.hobune.stream/wayback/20220612210136im_/https://answers.unity.com/themes/thub/images/avi.jpg)