Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to detect if prefab instance has been reverted?

Discussion in 'Prefabs' started by Peter77, Jul 30, 2017.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    I would like to detect if a prefab instance has been reverted. Users can revert a prefab instance by selecting a GameObject in the hierarchy and pressing the "Revert" button in the Inspector for example.

    I couldn't find any callback that fires when reverting a prefab. I gave the following two callbacks a try though, but neither does the job:

    How can I detect if a prefab instance has been reverted?
     
  2. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    Would like to know as well ! Did you find something ?
     
  3. klhurley

    klhurley

    Joined:
    Dec 11, 2014
    Posts:
    2
    If you revert all, you get a OnDisable() -> OnEnable().

    If you revert individual member variables, I haven't found anything yet.
     
  4. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    302
    Anyone from Unity can tell us if there is such a callback now? I'm trying to prevent/protect some variables from "reverting" when using "Revert all". Would be nice if there was an attribute that could protect some variables (very useful in my case when having auto generated ids for all objects).
     
  5. thomasa1972

    thomasa1972

    Unity Technologies

    Joined:
    Oct 11, 2016
    Posts:
    10
    Last edited: Mar 9, 2022
  6. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    302
    Hey @thomasa1972 thanks for answering.

    The callback does currently not trigger when using "Revert All" (I'm guessing you tested this quickly and is why you created a bug report on this).

    Also, it would be nice to have a callback that indicates that the object is about to change/be reverted. Or at the very least - know what action that caused the callback to trigger. :)

    Thanks!
     
  7. devorenge

    devorenge

    Joined:
    Jan 26, 2020
    Posts:
    6
    The callback still doesn't trigger for me when using any of the revert buttons (Revert All, Revert Component, Revert Property). I'm using 2020 LTS and the latest 2022.2. Until this is resolved, is there a different way to detect when the Revert buttons are pressed?
     
  8. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    217
    In 2023.1 you have:
    Code (CSharp):
    1. public static event System.Action<UnityEngine.GameObject> prefabInstanceReverted;
    2. public static event System.Action<UnityEngine.GameObject> prefabInstanceReverting;
    These callbacks have not been backported yet. Can you check that a 2023.1 build does solve your use case?