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.

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.

Today I find this attribute: