question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Making UnityAtoms compatible with Enter The PlayMode feature of Unity

See original GitHub issue

Enter The PlayMode is a feature that makes entering the play mode insanely fast. image

But there are side effects:

  • Fields marked with [NonSerialized] attribute keep their values - as script serialization step is skipped completely and scripts remain the same, all data is carried to the Play Mode.
  • Static variables keep their values - because Domain Reload does not happen types stay initialized.
  • Static events keep their subscribers.
  • There is no extra OnDisable/OnEnable calls for ExecuteInEditMode scripts.
  • ScriptableObject, MonoBehaviour fields which are not serialized into the build ([NonSerialized], private or internal) keep their values - as existing objects are not recreated and constructors are not called.
  • Watch out for null private and internal fields of array/List type - they are converted to an empty array/List object during Domain Reload and stay non null for game scripts.
  • ExecuteInEditMode/ExecuteAlways scripts are not destroyed/awaken - no OnDestroy/Awake calls for those.
  • Watch out for Awake/OnEnable methods which check EditorApplication.isPlaying property - Awake is not called and OnEnable is called only when EditorApplication.isPlaying is already true on Play Mode change.

UnityAtoms gets heavily impacted by these side effects. OnEnable and OnDisable calls are not called anymore. So nothing works as expected.

I exactly don’t know how we are supposed to change the code at the moment. So I created this issue for the discussion about it.

There are some useful callbacks that we can use.

  • EditorApplication.playModeStateChanged event
  • [RuntimeInitializeOnLoadMethod] attribute

What do you think?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AdamRambergcommented, Jul 19, 2023

Thanks a bunch both of you for the explanations 🙏

I agree, makes sense what you @rhys-vdw are suggesting.

1reaction
rhys-vdwcommented, Jul 19, 2023

Just to go into more detail on the RuntimeInitializeOnLoadMethodAttribute idea:

I think you could have a single static handler that calls Resources.FindObjectsOfTypeAll like:

[RuntimeInitializeOnLoadMethodAttribute]
static void ResetAll() {
  // get all atoms currently in memory.
  var atoms = Resources.FindObjectsOfTypeAll(typeof(IAtomBaseInterface)) as IAtomBaseInterface[];
  // reset current value to initial value.
  foreach (var a in atoms) a.Reset();
}

Might be more appropriate to use EditorApplication.playModeStateChanged (as @pnarimani suggested) since exiting playmode is also important.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manual: Configurable Enter Play Mode
To configure the Enter Play Mode settings, go to Edit > Project Settings > Editor and enable Enter Play Mode Options. When you...
Read more >
[FEATURE] Use custom SearchWindow to create new atoms
By default Unity Atoms adds many new asset "categories" (Events, Variables, Constants, Actions, etc.) with even more asset types (bool, int, ...
Read more >
Configurable Enter Play Mode
Play Mode is one of the key concepts in game development with Unity. The ability to iterate fast in the Editor is a...
Read more >
Unity Atoms — Tiny modular pieces utilizing the power of ...
I've been working with Unity for several years, creating mobile games for iOS and Android as well as using it during several Ludum...
Read more >
Conditions · Unity Atoms
Now Unfiltered will simply respond to any value passed when SomeIntChanged is raised. Enter play mode and test it by clicking on "Raise"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found