Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by xpavelos · Oct 02, 2016 at 10:12 AM · editor-scriptingcustom-inspectorreflectionsfieldinfo

Find out what fields the object have and let user edit them in custom editor window (Reflections)

Hey guys,

So let's say I have a few ScriptableObject items in my database and each have different set of fields like: int damage, float coolDown, SpriteRenderer sprite, etc. For example:

 public class Item1: ScriptableObject
 {
     public int damage;
     public float speed;
 }
 
 public class Item2: ScriptableObject
 {
     public bool isFunny;
     public SpriteRenderer sprite;
     public int damage;
 }

When I press a button I want to show fields of selected object in a custom inspector that user can edit. I only know how to display what fields the object have:

     var fields = items[i].GetType().GetFields();
 
     for (int j=0; j<fields.Length; j++)
     {
         GUILayout.Label(fields[j].ToString());
     }

This is the first time I use Reflections, I have no idea how to treat them. I did research yesterday but I am still having trouble.

Any suggestions?

Comment
Add comment · Show 2
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 Cherno · Oct 02, 2016 at 10:19 AM 0
Share

Take a look at the FieldInfo entry on $$anonymous$$SDN, is what I would recommend.

avatar image xpavelos Cherno · Oct 02, 2016 at 10:26 AM 0
Share

Hey, thanks for your comment. I looked at this before and I somewhat understand how it works. But the problem I am having is - how can I create a field in my custom window in order to edit values?

1 Reply

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

Answer by Bunny83 · Oct 02, 2016 at 10:38 AM

You shouldn't use reflection here since you have to take care of which variables actually can be serialized and which can't. Not all fields are serialized / serializable.

Just to answer your question:

To get the value of a field you have to call "GetValue" and pass the containing object as parameter.

 object val = fields[j].GetValue(items[i]);

This will read the value of the field into an object variable. Of course if you want to display a GUI control to edit the value you would need to analyse the type (int, float, string, ....) and cast the value to the actual type and display an appropriate GUI control.

Finally to write it back you have to use SetValue the same way:

 fields[j].SetValue(items[i], val);

Also don't forget to use Undo.RecordObject before you apply any changes, otherwise the changes won't be persistent.

What's way easier is to use the SerializedObject class. This can handle all that for you. The steps are:

  • You create a SerializedObject for each of the items you want to edit and store them in an array / list once.

  • at the beginning of each item you would call the Update method of the SerializedObject instance. This will update all the serialized values inside that SerializedObject.

  • Now you simply iterate through all SerializedProperties using GetIterator which returns the first SerializedProperty and in turn using Next each iteration.

  • Now you can simply use EditorGUILayout.PropertyField with the current SerializedProperty to show the appropriate control for this field.

  • Important: When you're done with an Item you have to call ApplyModifiedProperties on the SerializedObject instance to actually write the serialized data and to automatically create an Undo step.

The second way is the way how the normal Inspector works (in case no custom editor has been defined for this class).

Comment
Add comment · Show 2 · 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 Bunny83 · Oct 02, 2016 at 10:53 AM 0
Share

If you really want to use relfection, have a look at this list. It's a list of all the 18 different types Unity can serialize. But that's not the end. You have to check private variables as well and see if they have a SerializeField attribute attached and if so, display them as well. Further more custom Serializable class references are not serialized seperately but simply within the containing object. So you would need to do all that recursively for nested objects.

At all of these steps you have to be very careful how you treat the values (so you don't get casting exceptions) and to not run into an infinite recursion.

The SerializedObject class takes away a lot of this trouble, though sometimes it's limitations can be a bit annoying.

avatar image xpavelos · Oct 02, 2016 at 10:59 AM 0
Share

Thanks a lot for your broad answer. This looks like a perfect solution for my problem, I will start working on it right now. ;)

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

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom editor, serialize and prefab issue 0 Answers

Persistently changing variables from custom editor 2 Answers

A smarter Way to get the Type of SerializedProperty 6 Answers

Deep Copy of ScriptableObject 1 Answer

How can I recreate the Array Inspector element for a custom Inspector GUI? 7 Answers


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