Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by OniJames · Sep 28, 2016 at 09:10 PM · editorinspectortypedrawerfieldinfo

[Editor]Get FieldInfo by using an instance of that field

Hi, I'm from a small team that are developing a Reverse Tower Defense / RTS.

This is going to be hard to explain... So I'll use an example to help:

Let's say we have the following class:

   public class AClass
   {
         public float fExample1;
   }

Somewhere along editor scripts, we will do something like this (the GetName() instruction is my goal what I'd like, it's just to help understanding):

   AClass a = (AClass) target;
   FieldInfo field = GetType().GetField(**a.fExample1.GetName()**);
   
   SerializedProperty serProp = serializedObject.FindProperty(field.name);

We want to get a FieldInfo by using an instance of the same field in order to avoid hardcoding.

This way if we rename the field per say to fExample2 my code will refactor to this:

   AClass a = (AClass) target;
   FieldInfo field = GetType().GetField( a.fExample2.GetName() );
   
   SerializedProperty serProp = serializedObject.FindProperty(field.name);


Why avoid the hardcoding if this is already kinda hardcoded?

In my development team we mainly use this for selective attributes. We have a structure for all units and as such, our Towers are also units.

We have several fields in [HideInInspector] and in the Inspector/Drawer, fields like the units they will spawn or the positions from which each tower throws bombs/arrows to you are only shown if the Unit represents a tower.

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 Adam-Mechtley · Sep 29, 2016 at 07:11 AM 1
Share

@OniJames: So, put concisely: You want to be able to create an Editor class that finds a SerializedProperty, but want some mechanism for making sure it still works if you happen to refactor and change the field's name at some point?

avatar image OniJames Adam-Mechtley · Sep 29, 2016 at 09:31 AM 0
Share

Yes, that's a much better way to put it.

Thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Adam-Mechtley · Sep 29, 2016 at 09:44 AM

So unfortunately that's not strictly possible.

The best alternative I have come up with, which I use in my projects, is that I have an attribute I use to decorate SerializedProperty member variables on Editor classes that inherit from a base editor. At compile time, it scans the assemblies for all Editor classes, checks their fields for SerializedProperty members decorated with this attribute, and confirms that the path specified in the attribute argument is valid for the type being inspected. If there's a problem, you get an error in the console right away so you can fix it. (All decorated fields are also automatically assigned when the Editor is enabled.)

I'm hoping to actually get something like this into a future release of Unity as I think it could be useful. In the meantime, I have some packages on the AssetStore that include this BaseEditor class if you are curious and want to replicate this sort of thing (e.g., https://www.assetstore.unity3d.com/en/#!/content/161)

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

83 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 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

How to create a drop down menu in editor inspector 0 Answers

Expose protected variable in inspector 1 Answer

,Unity Custom Inspector CreateInspectorGUI redraw on change 1 Answer

The variables modified in the inspector are not kept on build 0 Answers

Can't restore camera preview 0 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