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".
Sounds interesting, it will be cool output all public data values of an object onto your screen GUI. I'm a 3D artist, not really a programmer. Your examples only shows the fragment of the code, Do u have a just simple completed example project that included it to an unityPackage (or, rar, zip)? so we can download it and try it out. Thanks.
Michael, currently I am a bit short of time. I toying around with thought to release a package with maxscripts and Unity scripts but it's nothing that will happen within the next 6 to 8 weeks or so.
Pingback: pligg.com