Search Unity

Disable recording PropertyModification to PrefabInstance for certain values

Discussion in 'Prefabs' started by sewy, Aug 5, 2019.

  1. sewy

    sewy

    Joined:
    Oct 11, 2015
    Posts:
    46
    Is there a way for custom component script value not to be recorded as modified in the prefab (because it changes on camera move)?
    They are private with [SerializeField].

    According to this
    SerializedProperty and SerializedObject. This enables instances to record changes and automatically includes changes in the undo system.

    I need to see them in the inspector (debugging purposes), but don't want to record changes as modified in the prefab.
    Could it be done with some special InspectorAttribute?

    Thanks.
    upload_2019-8-5_15-27-42.png
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,123
    If you want them visible, but not a part of the saved data, you need to write a custom editor for the script:

    Code (csharp):
    1. public class MyScript : MonoBehaviour {
    2.     [NonSerialized]
    3.     public int debugValue;
    4. }
    5.  
    6. // In a different file, in an Editor folder (or an assembly marked as Editor if you're using asmdef files)
    7. [CustomEditor(typeof(MyScript))]
    8. public class MyScriptEditor : Editor {
    9.     private MyScript script;
    10.  
    11.     void OnEnable() {
    12.         script = (MyScript) target;
    13.     }
    14.  
    15.     public override void OnInspectorGUI() {
    16.         base.OnInspectorGUI();
    17.  
    18.         script.debugValue = EditorGUILayout.IntField("Debug Value", script.debugValue);
    19.     }
    20. }