Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

PrefabUtility.RevertPropertyOverride not working

Discussion in 'Prefabs' started by Gydiah, Nov 3, 2021.

  1. Gydiah

    Gydiah

    Joined:
    Feb 15, 2020
    Posts:
    5
    Hello !
    On a big project, I'm trying to create a tool to replace a specified font by another one (in editor), and in the same time revert the overrides that shouldn't be here.
    I'm able to replace the font, but I've been searching for hours and I can't find why my
    PrefabUtility.RevertPropertyOverride()
    doesn't work.

    How my code works :
    - I iterate a first time over each prefabs to replace the font
    - I iterate a seconde time over the same prefabs
    - I get every TextMeshProUGUI in a prefab
    - I iterate through every of these
    - I revert the property override like this :


    Code (CSharp):
    1.  
    2. public static void RevertOverrides(IEnumerable<TextMeshProUGUI> texts)
    3. {
    4.     foreach(TextMeshProUGUI text in texts)
    5.     {
    6.           SerializedObject serializedText = new UnityEditor.SerializedObject(text);
    7.  
    8.           SerializedProperty textFont = serializedText.FindProperty("m_fontAsset");
    9.           SerializedProperty textFontMaterial = serializedText.FindProperty("m_fontMaterial");
    10.  
    11.           PrefabUtility.RevertPropertyOverride(textFont, InteractionMode.AutomatedAction);
    12.           PrefabUtility.RevertPropertyOverride(textFontMaterial, InteractionMode.AutomatedAction);
    13.     }  
    14. }
    My
    textFont
    and
    textFontMaterial
    aren't null but their
    objectOverride 
    property is to false. Am I doing something wrong ?

    Unity version : 2020.3.11f1
     
  2. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    221
    Hard to tell what the issue is without having a repro project to see how you collect the TextMeshProUGUI objects. If you have nested prefabs or variants you might need to edit the contents of the Prefabs and not the scene instances to remove the overrides. (See https://docs.unity3d.com/2020.1/Doc...ce/PrefabUtility.EditPrefabContentsScope.html)

    You can also try checking if they are overridden first before reverting (to make sure they are actually overriden):
    if (textFont.prefabOverride)
    PrefabUtility.RevertPropertyOverride(textFont, InteractionMode.AutomatedAction);
     
  3. Gydiah

    Gydiah

    Joined:
    Feb 15, 2020
    Posts:
    5