0

I would like to see the object structure as a string that can be printed to the console but cannot seem to achieve this. Im using the following code:

Debug.Log(character.Serialize());

where character is the serializable class

tommo
  • 1,350
  • 1
  • 10
  • 25

2 Answers2

1

I think there's no immediate way. You have some solutions, either you override the ToString and do this for every class you want to print:

public override string ToString(){
    return string.Format("{0}:{1}",pippo,pluto);
}

where pippo and pluto either are primitive that can be printed or a complex Class with an overridden ToString().

Or you make a Json out of it, like the following: http://blog.codingoutloud.com/2014/01/29/dumping-objects-one-property-at-a-time-with-console-writeline-a-pretty-printer-for-c-objects-thats-good-enough/

or

http://weblogs.asp.net/scottgu/tip-trick-building-a-tojson-extension-method-using-net-3-5

But it would require an external library.

It's probably the same question posted here, though: C#: Printing all properties of an object

Community
  • 1
  • 1
Massimo
  • 692
  • 2
  • 11
  • 29
0

The easiest way to do this would be to use json.net unity plugin and call:

string s = JsonConvert.SerializeObject(yourObject);

works like a charm.

tommo
  • 1,350
  • 1
  • 10
  • 25