Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get public variables of an unknown component

Discussion in 'Scripting' started by djweinbaum, Jun 21, 2014.

  1. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    533
    Hey everyone,

    I trying to make a custom inspector with an object field, and then some dropdowns allowing you to dig in to select a component of that object, and then select a variable. I've gathered I'll probably need to use system.reflection, and I have everything working accept the dropdown for choosing a variable. I know the string name of the component through the component dropdown.

    What I want is anything the inspector would show, but I don't want to have that inspector showing. GetFields seems to return only if that particular inspector is showing, unless I'm doing something wrong. I can't seem to understand what the heck getproperties is getting but I don't see the public vars defined in the component script.

    Any ideas?

    Thanks in advance,
    Danny
     

    Attached Files:

  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    I... huh... what? Sorry, but I re-read this a few times and I still don't understand. You want what the inspector would show, but not the inspector showing.

    I will assume you want all the fields Unity's inspector are known to show;
    - Public fields without [HideInInspector]
    - Private/Protected fields with [SerializedField] and without [HideInInspector]

    Should give;
    Code (csharp):
    1.  
    2. public List<FieldInfo> GetInspectorVisibleFields(Type type)
    3. {
    4.   List<FieldInfo> fields = new List<FieldInfo>();
    5.   foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public))
    6.   {
    7.     if (field.GetCustomAttributes(typeof(HideInInspector), true).Length != 0)
    8.       continue;
    9.  
    10.     if (field.IsPrivate && field.GetCustomAttributes(typeof(SerializeField), true).Length == 0)
    11.       continue;
    12.  
    13.     fields.Add(field);
    14.   }
    15.  
    16.   return fields;
    17. }
    18.  
     
  3. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    533
    LightStriker thank you so much! I really appreciate your help. This gets me really close. What I meant by "not have the inspector showing" was irrelevant because I falsely thought getfields doesn't work on a component if the inspector for that component isn't showing unity, which I see now is incorrect. The code you put there gets almost exactly what I want. There are some fields that I can see in the inspector that this isn't getting me, like transformation fields of a transform component. Would you have any idea how to get those to show up as well?
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Most of Unity's type has no fields, as the data is stored in the unmanaged space. Instead, they expose properties that act as bridge.

    So for Unity's type, you'll need to do GetProperties() instead of GetFields().
     
  5. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    533
    ah okay. Sounds like I'm in over my head with this reflection stuff. I think I'm just going to make a special case for if the component of type transform.