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
0
Question by Xarbrough · Jul 25, 2017 at 12:47 PM · c#inspectordebugserializedproperty

Make serialized field not editable in debug inspector

I'd like to prevent any changes to a serialized field on my custom component, even when in debug mode of the inspector. I have a feeling that this is not supported, but Unity internally has a way of hacking it, so I might be able as well.

In debug mode the inspector also shows fields like "Instance ID" or "Local Identfier In File", but of which should never change, but the fields are editable. When I change one of the numbers, however, it gets reset to the original. That's basically what I'd like to do for my fields as well.

In the custom property drawer I handle my custom behaviour, but in debug mode, I have no callbacks whatsoever. I was trying to check for a change by storing the last known number, but that would have to be static and seems very brittle.

Any ideas how to do something like this?

My main reason is: The field represents a fixed ID, which I assign on creation, it should never change, or else references all across the project would break. My designers still use debug mode sometimes, and I also want to support checking the underlying data and seeing the number in case anything goes wrong, so HideIninspector is not really an option.

Comment
Add comment · Show 7
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 ShadyProductions · Jul 25, 2017 at 12:58 PM 0
Share

You can always overwrite it in code, but what's the point of serializing your field if u can't change it.

avatar image Xarbrough ShadyProductions · Jul 25, 2017 at 01:06 PM 0
Share

Yes, I'd like to override it in code...but in the inspector, when in debug mode, which I apparently can't, because Unity handles the properties for me without giving me callbacks. The point is, that at edit time, I serialize data when the component is created. I use that data at runtime to match up IDs and find custom references, so between object creation and runtime use, nobody should accidentally be able to change the value manually. This can be prevented easily on the "front-end" in my custom property drawer, but not in the inspector debug mode, which I'd like to handle somehow.

avatar image taxvi · Jul 25, 2017 at 01:03 PM 0
Share

I would go like

 [SerializeField]
 public float InspectorVariable { get { return _hiddenVariable } set {} }
 private float _hiddenVariable;
avatar image ShadyProductions taxvi · Jul 25, 2017 at 01:06 PM 0
Share

You'll still have to make _hiddenVariable private otherwise you can change that one too.

avatar image taxvi ShadyProductions · Jul 25, 2017 at 01:13 PM 0
Share

ouch, good catch, fixed :D

avatar image Xarbrough taxvi · Jul 25, 2017 at 01:14 PM 0
Share

Hm, this doesn't really help, I'm afraid. $$anonymous$$aking the backing variable private serialized and exposing it via a public property only means, that nobody can edit the variable through regular C# code, but Unity doesn't use properties in the inspector, it sets the fields directly via the SerializedObject. I know there is no official support or feature for what I'm trying to do, but I thought that somebody would know about some hacky trick. Again, I'm trying to make variables show up in the debug mode of the inspector, but not be editable even there, like the Instande ID field.

avatar image taxvi Xarbrough · Jul 25, 2017 at 01:23 PM 0
Share

I didn't know properties weren't serialized. oh well at least I learned something :)

2 Replies

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

Answer by Bunny83 · Jul 25, 2017 at 01:44 PM

That's simply not possible. The Debug mode of the inspector bypasses all possible customisations. It just shows the raw serialized data. The internal UnityEngine.Object fields are simply hardcoded to not be editable. The SerializedProperty class has an "editable" property which directly calls into native code. So if you have a SerializedProperty of an internal field it is automatically marked as not editable.

Your best approach to avoid any editing from the GUI of the UnityEditor is to completely hide that component. As far as i know a hidden component won't show up in the debug mode, though i've never really tested this yet.

Anyways the question is how far it is necessary to go here. Keep in mind that when using the asset serialization mode "text" everybody can open assets like prefabs or scenes in a text editor and change literally any value, even internal ones which might completely break the scene / prefab.

Even with binary serialziation it's possible to alter values given enough time and patience.

ps: Keep in mind that if you don't want to hide your complete component because it has values that need to be edited, just create a seperate component (like UNets NetworkIdentity) which only holds that ID for you and is completely hidden. You could even "bind" it to another component by automatically adding that ID component (RequireComponent) and destroying it when the main component is destroyed.

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 Xarbrough · Jul 25, 2017 at 02:03 PM 0
Share

$$anonymous$$akes sense. I've settled on using HideInInspector for the ID field and drawing it in the regular inspector as a label for debug purposes, but true, trying to make something fool-proof and flexible for me as a developer contradicts itself pretty much. Also a good idea to look at the Unity internal implementation, maybe there there even is some hidden way to mark a property as not editable. For the entire components we can still use HideFlags.NotEditable.

avatar image unityBerserker · Jul 27, 2017 at 11:54 AM 0
Share

In rigidbody2D is property Info. We can't change that values.

In decompiled version we can see how it was created: https://github.com/$$anonymous$$attRix/UnityDecompiled/blob/master/UnityEditor/UnityEditor/Rigidbody2DEditor.cs

We can do that with: https://docs.unity3d.com/ScriptReference/EditorGUI.BeginDisabledGroup.html

avatar image
-1

Answer by unityBerserker · Jul 28, 2017 at 10:26 AM

Today I find this attribute: https://gist.github.com/LotteMakesStuff/c0a3b404524be57574ffa5f8270268ea#file-readonlypropertydrawer-cs

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

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

Creating a custom inspector utilizing a list of class instances with serialization? 1 Answer

Property Drawer SerializedProperty is null 2 Answers

Distribute terrain in zones 3 Answers

How to change inspector with non-Monobehaviour objects ? 1 Answer

Multiple Cars not working 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