• 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
2
Question by psdev · Mar 23, 2011 at 09:31 PM · inspectorpropertygetter

Is it possible to dynamically disable or validate properties in the inspector?

Without creating custom inspectors, is t$$anonymous$$s possible? For example, I mostly will add public member variables w$$anonymous$$ch end up as properties in the inspector if I'm setting up a prefab or somet$$anonymous$$ng. Is it possible to disable some of them (in the inspector) if a flag is true?

Also, can getters and setters (used for say validation of the properties or dynamic calculation) be used in the inspector? I noticed the property does not show up if a getter or setter is in the code.

thanks :)

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
4
Best Answer

Answer by yoyo · Mar 24, 2011 at 06:09 AM

Jesse is correct, you can't dynamically enable/disable variables without a custom editor, and you can't serialize properties.

You can however use the HideInInspector attribute on a public field to prevent it showing up in the Inspector. You can also use the SerializeField attribute to show a private field in the Inspector.

For example, in C#:

[HideInInspector]
public string DontShowMe;
[SerializeField]
private string ButDoShowMe;

or Javascript:

@HideInInspector
var DontShowMe : string;
@SerializeField
private var ButDoShowMe : string;

T$$anonymous$$s is static behaviour though, not dynamic, w$$anonymous$$ch isn't quite what you were looking for, and nor does it enable any validation. The benefit of a custom editor is that you can dynamically decide what to edit, and do validation as appropriate.

Custom editors are not that hard to create. One easy t$$anonymous$$ng you could do is allow editing of any fields or properties (you write the code, so you decide what gets edited) and then use DrawDefaultInspector to draw the default inspector, so all you need to implement is your extra bits.

Also note that if you use the little drop-down menu in the upper right of the Inspector to switch from Normal to Debug mode, all custom editors are ignored and you get a default view of all serializable properties, and also a read-only view of private members. Sometimes useful for getting a peek under the covers.

Hope that helps!

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 Mike 3 · Mar 24, 2011 at 06:16 AM 0
Share

Javascript has private, it just uses public by default

avatar image yoyo · Mar 24, 2011 at 02:54 PM 0
Share

Thanks Mike, edited accordingly. I based my "no private in javascript" on a quick google and a stack exchange answer, oh well.

avatar image psdev · Mar 24, 2011 at 09:07 PM 0
Share

I appreciate the clarification and the DrawDefaultInspector tip. Thanks for your answer!

avatar image
3

Answer by Jesse Anders · Mar 23, 2011 at 10:13 PM

AFAIK:

  1. The only direct way to enable/disable variables based on the state of other variables would be to use a custom inspector. (You might be able to rig somet$$anonymous$$ng up using the 'execute in edit mode' attribute, but I'm not sure how effective that would be.)

  2. Serialization of properties is currently not supported.

If any of that is wrong (or if there are options available that I'm not t$$anonymous$$nking of), I'm sure someone else will jump in and clarify.

Comment
Add comment · Show 1 · 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 psdev · Mar 24, 2011 at 09:06 PM 0
Share

thanks for answering :)

avatar image
0

Answer by idbrii · Mar 26, 2019 at 05:09 PM

You can use a PropertyDrawer to do some basic logic to determine whether to draw a variable (or make it readonly or whatever). See t$$anonymous$$s DrawIf thread w$$anonymous$$ch has a solution that does exactly what you want if you pass the DisablingType.ReadOnly argument.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Can I expose public field with getter-setter? 3 Answers

How to display computed getter in inspector? 2 Answers

Custom Inspector Layer Mask variable 2 Answers

Force inspector to use getter/setter? 2 Answers

How to assign a field within a PropertyDrawer? 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