Search Unity

Specializing PreFabs/ Prevent properties from beeing overwritten

Discussion in 'Editor & General Support' started by langem, Aug 12, 2014.

  1. langem

    langem

    Joined:
    Mar 18, 2013
    Posts:
    73
    Hi, I've been struggling with this topic for a while now. Is there a way to prevent that certain properties of a PreFab-instance (in a scene in the editor) are "overwritten" if the according PreFab property is changed?

    In my case I use vehicles (prefabs) that should have a certain speed (that I adjust in the inspector). Everything is fine and all the vehicles keep their speed values, even if I hit "apply" on one of the instances. But here comes the problem: The velocity values of the cars that haven't been changed in the inspector are set to the new velocity value of the "new" PreFab. Is there any way of preventing this?

    Any input would be very appreciated.
    Best regards,
    Matthias
     
    Deleted User and Deadcow_ like this.
  2. langem

    langem

    Joined:
    Mar 18, 2013
    Posts:
    73
    Ok, I worked out some kind of work around... Basically the problem comes down to the point that there seems to be no way to tell a property/variable of a prefab instance to be automatically overwritten (always bold in the inspector). So I wrote an editor script that checks if the property value is preFabOverride or not. And in case it isn't the original value is tempoarily saved and the reassigned to the property an update later. In the meantime the property value is increased by 1. This way once you hit apply on an instance of a prefab the property value is automatically set to bold (override) and wouldn't change if you change the prefab value in an other place.

    Code (CSharp):
    1.  
    2. public override void OnInspectorGUI ()
    3. {
    4.     serializedObject.Update ();
    5.     DrawDefaultInspector ();
    6.  
    7.     SerializedProperty myProperty = serializedObject.FindProperty ("maxDistance");
    8.  
    9.     if (tick) {
    10.        myProperty.floatValue = tempValue;
    11.        tick = false;
    12.     }
    13. if(!Application.isPlaying)
    14. {
    15.     if (!myProperty.prefabOverride) {
    16.        GUI.color = Color.red;
    17.        tick = true;
    18.        tempValue =myProperty.floatValue;
    19.        myProperty.floatValue += 1;
    20.     }
    21.     else
    22.         GUI.color = Color.green;
    23. }
    24. EditorGUILayout.PropertyField (myProperty);
    25.  
    26.     if (GUI.changed) {
    27.         EditorUtility.SetDirty (target);
    28.     }
    29.  
    30. serializedObject.ApplyModifiedProperties ();
    31. }
    Is there a better way to do this?
     
    Last edited: Aug 13, 2014
  3. Deleted User

    Deleted User

    Guest

    Ditto, I did some horrible, horrible workarounds to achieve that. I've been unable to find an equivalent of "mark property as instance-specific", pretty much in the same way `transform.position` and `transform.rotation` work.

    I also can't believe it's such a rare use case (or is it?) — so I figured there should definitely be a "proper" way of doing this.