2

I implemented OnEnable() and it is always called in Play mode as expected. But in Editor Mode some components call OnEnable() and other don't

I need OnEnable() or other method to be called in Editor Mode to setup component to be able to prototype it.

If I extend class from UnityEngine.UI.Image then OnEnable() is called in Editor Mode. If I extend class from MonoBehavior then it does not call OnEnable() in Editor Mode but still being called in Play Mode.

Is there any clarification why it is called or not called.

Tema
  • 1,338
  • 13
  • 27

2 Answers2

3

MonoBehaviour.OnEnable() is called when the script component is actived. UnityEngine.UI.Image inherits from MonoBehaviour, so there should be no difference. You may need to have a look at Execution Order of Event Functions.

If you want your MonoBehaviour called by the editor, you should add a ExecuteInEditMode attribute to your class.

zwcloud
  • 4,546
  • 3
  • 40
  • 69
1

Thanks. May be UI.Image has that flag

Exactly !

UI.Image inherits from MaskableGraphic. UI.Image inherits from MaskableGraphic.

MaskableGraphic inherits from Graphic. MaskableGraphic inherits from Graphic.

Graphic has the ExecuteInEditMode And Graphic has the ExecuteInEditMode flag !


ps1: @zwcloud is right about ExecuteInEditMode attribute!, this answer is my two cents.

ps2; you can always Go the declaration of the class and check the code, it could be very fun ! Go to declaration

mayo
  • 3,845
  • 1
  • 32
  • 42