• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Custom Inspector changes not updating target in Edit Mode 1 Answer

Custom Editor List with child classes 1 Answer

How to draw a model in the preview window of my custom editor using OnPreviewGUI? 0 Answers

Custom enumeration class accessible through inspector? 1 Answer

Is there a way to live-update script-controlled UI formatting in the editor? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges