I am becoming a .Net/C# fan
With it's reflection ability, you can do some nice things. For example output all public data values of an object onto your screen. A little minimal example for Unity debugging/tweaking purposes …
Let's say we have two classes with public data we are interested in
class MyDataOne : Object
{
public float AirDensityRo = 1.225f;
public float LengthInMeters = 2156.33f;
public float MetersPerSecond = 2.0f;
}class MyDataTwo : Object
{
public int Iteration = 0;
public float Offset = 13.0f;
public float Affinity = 2.0f;
}
We then save both instances into a list
List<Object> DisplayedDataObjects = new List<Object>();
void Start()
{DisplayedDataObjects.AddRange(new Object[] { new MyDataOne() , new MyDataTwo () });
(…)
And in the OnGUI context you can draw the values for example like this
(…)
float yOffset = 15;
foreach (Object obj in myDisplayedObjects)
{
yOffset += 5;
GUI.Label(new Rect(5, yOffset, 240, 20), "== " + obj.ToString() + " ==");
yOffset += 15;
FieldInfo[] fields = obj.GetType().GetFields();
foreach (FieldInfo field in fields)
{
if (field.GetValue(obj) != null)
GUI.Label(new Rect(1, yOffset, 240, 20), field.Name + ": " + field.GetValue(obj).ToString());
yOffset += 15;
}
}
The nice thing is that you can rename (refactor) member names and classes without worries as changes will visible immediately. With the list you can also modify quite quickly in what objects you're actually interested in.
The reflection stuff is in "System.Reflection".








Comment by Michael — Wednesday, 27th May, 2009 @ 06:11
Comment by dominique — Wednesday, 27th May, 2009 @ 04:14
Simple DebugDAta Output for the Lazy Unity3D Coder…
I am becoming a .Net/C# fan
With it’s reflection ability, you can do some nice things. For example output all public data values of an object onto your screen. A little minimal example for Unity debugging/tweaking purposes ÔǪ…
Trackback by pligg.com — Wednesday, 29th July, 2009 @ 09:40