- Home /
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.
You can always overwrite it in code, but what's the point of serializing your field if u can't change it.
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.
I would go like
[SerializeField]
public float InspectorVariable { get { return _hiddenVariable } set {} }
private float _hiddenVariable;
You'll still have to make _hiddenVariable private otherwise you can change that one too.
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.
I didn't know properties weren't serialized. oh well at least I learned something :)
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.
$$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.
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
Answer by unityBerserker · Jul 28, 2017 at 10:26 AM
Today I find this attribute: https://gist.github.com/LotteMakesStuff/c0a3b404524be57574ffa5f8270268ea#file-readonlypropertydrawer-cs
Your answer
