x


Generic way to get properties on Component?

I need to dump selected GameObjects to a file, with all their components and their components variable names and values.

Is there a generic 'getAttribute' kind of API I can call if I have a Component and I don't know the type?

more ▼

asked Jul 26 '10 at 07:43 PM

DaveA gravatar image

DaveA
26.4k 151 171 256

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You could always abuse reflection and generics for it.

Something like:

using System.Reflection;

//blah blah

public static T GetReference<T>(object inObj, string fieldName) where T : class
{
    return GetField(inObj, fieldName) as T;
}

public static T GetValue<T>(object inObj, string fieldName) where T : struct
{
    return (T)GetField(inObj, fieldName);
}

public static void SetField(object inObj, string fieldName, object newValue)
{
    FieldInfo info = inObj.GetType().GetField(fieldName);
    if (info != null)
        info.SetValue(inObj, newValue);
}

private static object GetField(object inObj, string fieldName)
{
    object ret = null;
    FieldInfo info = inObj.GetType().GetField(fieldName);
    if (info != null)
        ret = info.GetValue(inObj);
    return ret;
}

This has the benefit of working on any object, not just components. Use GetValue when you know it'll be a value type, GetReference when it'll be a reference type. Added SetField just for fun too (which works without generics for obvious reasons)

Hopefully pretty self explanatory

more ▼

answered Jul 26 '10 at 08:01 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

Thanks Mike. Had dug up an answer myself, posted before I saw your answer, but you've given me some good stuff to work with there.

Jul 26 '10 at 08:05 PM DaveA

Hehe, nice work on finding it so fast

Jul 26 '10 at 08:10 PM Mike 3
(comments are locked)
10|3000 characters needed characters left

This should get me started:

using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;

class Introspect
{

    [MenuItem("Window/Introspect")]
    static void IntrospectNow()
    {
        bool found1;
        GameObject[] go = Selection.gameObjects;
        Transform[] trs = Selection.GetTransforms (SelectionMode.Deep | SelectionMode.DeepAssets);
        found1 = false;
        foreach (Transform tr in trs)
        {
            Component[] components = tr.GetComponents<Component>();
            for (int i = 0; i < components.Length; i++)
            {
                Component c = components[i];
                if (c == null)
                {
                    Debug.Log(tr.name + " has an empty script attached in position: " + i);
                    found1 = true;
                }
                else
                {
                    Type t = c.GetType();
                    Debug.Log("Type "+t);
                    Debug.Log("Type information for:" + t.FullName);
                    Debug.Log("\tBase class = " + t.BaseType.FullName);
                    Debug.Log("\tIs Class = " + t.IsClass);
                    Debug.Log("\tIs Enum = " + t.IsEnum);
                    Debug.Log("\tAttributes = " + t.Attributes);                            


                    System.Reflection.FieldInfo[] fieldInfo = t.GetFields();
                    foreach (System.Reflection.FieldInfo info in fieldInfo)
                    Debug.Log("Field:" +info.Name);

                    System.Reflection.PropertyInfo[] propertyInfo = t.GetProperties();
                    foreach (System.Reflection.PropertyInfo info in propertyInfo)
                    Debug.Log("Prop:"+info.Name);

                    Debug.Log("Found component "+c);
                }
            }
        }
    }
}
more ▼

answered Jul 26 '10 at 08:04 PM

DaveA gravatar image

DaveA
26.4k 151 171 256

sorry for bad formatting

Jul 26 '10 at 08:09 PM DaveA

for the code blocks, just paste the code in, select all, then click the code button (button with 0s and 1s) - works a heck of a lot better than either pre or code tags

Jul 26 '10 at 08:16 PM Mike 3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3319
x2075
x302
x138
x84

asked: Jul 26 '10 at 07:43 PM

Seen: 3432 times

Last Updated: Jul 26 '10 at 07:43 PM