Question by
MouseInBox · Sep 09, 2019 at 08:23 AM ·
initializeprefabutilityapply
How to record all the components of a prefab when click the button "Apply"?
There is a script:
class MyClass : MonoBehaviour
{
public Image image;
}
I want to assign "image"'s value automatically when I click the "prefab"'s button "Apply" in Inspector window.
class MyClass : MonoBehaviour
{
public Image image;
public void OnApply()
{
image = GetComponent<Image>("image");
}
}
My goal is to call method "OnApply" automatically when I click Apply button.
[InitializeOnLoad]
public class InitializeOnLoadTest
{
static InitializeOnLoadTest()
{
PrefabUtility.prefabInstanceUpdated += OnPrefabInstanceUpdated;
}
static void OnPrefabInstanceUpdated(GameObject instance)
{
instance.SendMessage("OnApply");
}
}
It seems OK, but I have to click "Apply" button twice. It means that method "OnPrefabInstanceUpdated" is called after the modify of the prefab, so the first click will not work, and the second click solve the problem. How can I make the first click work?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612211912im_/https://answers.unity.com/themes/thub/images/avi.jpg)