Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by DaveA · Jul 26, 2010 at 07:43 PM · gameobjectcomponentreflectionapi

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Mike 3 · Jul 26, 2010 at 08:01 PM

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jul 26, 2010 at 08:05 PM 0
Share

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

avatar image Mike 3 · Jul 26, 2010 at 08:10 PM 0
Share

Hehe, nice work on finding it so fast

avatar image Vitro · May 24, 2013 at 10:50 AM 0
Share

The SetField does not work with monobehaviour components

avatar image Bunny83 · May 24, 2013 at 10:55 AM 0
Share

Sure, why not? What's your code? if it's not directly related to this question / answer you should post your own question and include your code. What variable type are you trying to set?

avatar image DrDecipher · Aug 18, 2014 at 12:14 AM 0
Share

This script is a gem! Nice work guys.

Show more comments
avatar image
3

Answer by DaveA · Jul 26, 2010 at 08:04 PM

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&lt;Component&gt;();
         for (int i = 0; i &lt; 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);
             }
         }
     }
 }

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jul 26, 2010 at 08:09 PM 0
Share

sorry for bad formatting

avatar image Mike 3 · Jul 26, 2010 at 08:16 PM 0
Share

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

avatar image Aggressor · Jul 19, 2015 at 08:32 PM 0
Share

Great help thank you.

avatar image
0

Answer by Cherno · Jul 19, 2015 at 08:35 PM

SerializeHelper can do what you ask for, at least for Transform and MonoBehavior components; Other types can be supported with some minor additions.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

FieldInfo.GetValue with GameObject field 0 Answers

Get Component on Instantiated Object 1 Answer

way to get all object with a certain component/script attached 3 Answers

How to get all scripts attached to a gameojbect? 1 Answer

Script Component unchecks itself prior to build. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges