Wednesday, 27th May, 2009

Simple DebugData output for the lazy Unity3D coder

Filed under: C# and Dot.Net, Unity3D — dominique @ 01:55

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".

Bookmark and Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Technorati
  • Digg
  • Reddit
  • Google Bookmarks
  • YahooMyWeb
  • Live-MSN
  • Facebook

3 Comments »

  1. 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.

    Comment by Michael — Wednesday, 27th May, 2009 @ 06:11

  2. 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.

    Comment by dominique — Wednesday, 27th May, 2009 @ 04:14

  3. 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

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

:D :) ^_^ :( :o 8O :shock: 8) ;-( :lol: xD :wink: :evil: :p :whistle: :woot: :sleep: =] :sick: :straight: :ninja: :love: :kiss: :angel: :bandit: :alien:
Bookmark and Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Technorati
  • Digg
  • Reddit
  • Google Bookmarks
  • YahooMyWeb
  • Live-MSN
  • Facebook