Skip to content

Conversation

@mkc1370
Copy link

@mkc1370 mkc1370 commented Jul 11, 2025

Summary
Subscriptions created in OnEnable or Start are cleared when Unity re-enters Edit Mode because UnityFrameProvider.Clear() is called during PlayModeStateChange.EnteredEditModeand.
As a result, setting up new subscriptions when Edit Mode is entered again becomes difficult.

Observed order (Unity 6000.1.10f1, 2022.3.9f1)

ExitingPlayMode   ← Clear() [to-be]
OnEnable
Start
EnteredEditMode   ← Clear() [as-is]

Minimal repro

using R3;
using UnityEditor;
using UnityEngine;

[ExecuteAlways]
public class R3Test : MonoBehaviour
{
    [InitializeOnLoadMethod]
    private static void Init()
    {
        EditorApplication.playModeStateChanged += state =>
        {
            Debug.Log($"Play mode state changed: {state}");
        };
    }

    private void Start()
    {
        Debug.Log(nameof(Start));
        Observable.EveryUpdate(UnityFrameProvider.PreLateUpdate, destroyCancellationToken)
                  .Take(5)
                  .Subscribe(_ => Debug.Log($"Subscribe in Start: {Time.frameCount}"));
    }

    private void OnEnable()
    {
        Debug.Log(nameof(OnEnable));
        Observable.EveryUpdate(UnityFrameProvider.PreLateUpdate, destroyCancellationToken)
                  .Take(5)
                  .Subscribe(_ => Debug.Log($"Subscribe in OnEnable: {Time.frameCount}"));
    }

    private void OnDestroy() => Debug.Log(nameof(OnDestroy));
}

@mkc1370 mkc1370 marked this pull request as ready for review July 11, 2025 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant