I would not recommend using your scene objects or domain objects for serialization as well. You will always be battling between the two different serialization requirements.
I would recommend separating your models between scene model and persistence/serialization model.
public class SerializableItemModel
{
public Vector3 position;
public Quaternion rotation;
public float _value;
}
Then have a mapper than can map between your objects.
public class ItemToModelMapper
{
public SerializableItemModel MapToModel(Item item)
{
var model = new SerializableItemModel
{
position = item.transform.position;
rotation = item.transform.rotation;
_value = item._value;
};
return model
}
public Item MapFromModel(SerializableItemModel model)
{
return new Item(model.position, model.rotation, model._value);
}
}
If you separate your objects like this you will never have to worry about cross cutting concerns like serializing your object for persistence and also somehow display it in the inspector etc. If you want to serialize only 10% of your scene object then you map it into a small model and serialize that instead.
Of course this is a lot more code than you probably were hoping to write. If you wanted to you could just provide the Item to the SerializableItemModel constructor and skip the mapping layer.
public class SerializableItemModel
{
public Vector3 position;
public Quaternion rotation;
public float _value;
public SerializableItemModel(Item item)
{
this.position = item.transform.position;
this.rotation = item.transform.rotation;
this._value = item.transform._value;
}
}
Now your code will look like this when serializing:
private ItemToModelMapper mapper;
void Start()
{
Item item = new Item();
...
...
// Serialize
var serializationModel = this.mapper.MapToModel(item);
string json = JsonUtility.ToJson(serializationModel );
// Deserialize
SerializableItemModel deserializedModel = JsonUtility.FromJson<SerializableItemModel>(json);
Item loadedItem = this.mapper.MapFromModel(deserializedModel);
}
EDIT: As pointed out by Programmer I never addressed the inspector view for Item. Because the Item class now only has one responsibility you are welcome to put [SerializeField] on any field because this object will not be used for any other serialization. You can now also use any serializer you want (JsonUtility, BinaryFormatter, JsonConvert, XmlSerializer, etc).