From c58deac688dd7ff9d50ea38f225ae8b840712450 Mon Sep 17 00:00:00 2001 From: Jahnisse Date: Thu, 26 Mar 2026 16:58:18 +0100 Subject: [PATCH] Fix : No more messages after leaving play mode --- Assets/Editor/SceneNotifier.cs | 67 +++++++++++++++++----------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/Assets/Editor/SceneNotifier.cs b/Assets/Editor/SceneNotifier.cs index 2ae8af3d..766fdf6c 100644 --- a/Assets/Editor/SceneNotifier.cs +++ b/Assets/Editor/SceneNotifier.cs @@ -10,25 +10,52 @@ using UnityEngine; [InitializeOnLoad] public static class SceneNotifier { - // Replace with your Discord webhook URL - private static readonly string WebhookUrl = "https://discord.com/api/webhooks/1486718530765328424/42mP_zYNTEwwBbc_lcuasPbpZppXoC4U14-Pde1YatdxatT5s9vml8lgQXTwAmm0PNe5"; + private static readonly string WebhookUrl = "https://discord.com/api/webhooks/WEBHOOK_ID/WEBHOOK_TOKEN"; private static readonly string UserName = Environment.UserName; private static readonly string MachineName = Environment.MachineName; private static readonly HttpClient Http = new HttpClient(); - // Key = full scene path, Value = opened timestamp private static readonly Dictionary OpenScenes = new Dictionary(); + // Suppresses fake open/close events caused by Play Mode transitions + private static bool SuppressSceneEvents = false; + static SceneNotifier() { EditorSceneManager.sceneOpened += OnSceneOpened; EditorSceneManager.sceneClosed += OnSceneClosed; EditorApplication.quitting += OnEditorQuitting; + EditorApplication.playModeStateChanged += OnPlayModeStateChanged; + } + + private static void OnPlayModeStateChanged(PlayModeStateChange state) + { + switch (state) + { + case PlayModeStateChange.ExitingEditMode: + case PlayModeStateChange.EnteredPlayMode: + case PlayModeStateChange.ExitingPlayMode: + case PlayModeStateChange.EnteredEditMode: + SuppressSceneEvents = true; + break; + } + + // Re-enable notifications shortly after returning to edit mode + if (state == PlayModeStateChange.EnteredEditMode) + { + EditorApplication.delayCall += () => + { + SuppressSceneEvents = false; + }; + } } private static void OnSceneOpened(UnityEngine.SceneManagement.Scene scene, OpenSceneMode mode) { + if (SuppressSceneEvents || EditorApplication.isPlayingOrWillChangePlaymode) + return; + if (!IsTrackableScene(scene)) return; @@ -49,6 +76,9 @@ public static class SceneNotifier private static void OnSceneClosed(UnityEngine.SceneManagement.Scene scene) { + if (SuppressSceneEvents || EditorApplication.isPlayingOrWillChangePlaymode) + return; + if (!IsTrackableScene(scene)) return; @@ -71,19 +101,6 @@ public static class SceneNotifier OpenScenes.Remove(scenePath); _ = SendDiscordMessage(message); } - else - { - // Safety fallback in case Unity reports a close for a scene we did not track - string message = - $"🔓 **Scene Released**\n" + - $"👤 **{UserName}**\n" + - $"💻 **{MachineName}**\n" + - $"📁 `{FormatPath(scenePath)}`\n" + - $"🕒 Closed: **{FormatDateTime(closedAt)}**\n" + - $"⏱ Stayed on scene: **unknown**"; - - _ = SendDiscordMessage(message); - } } private static void OnEditorQuitting() @@ -91,19 +108,8 @@ public static class SceneNotifier DateTime quitAt = DateTime.Now; if (OpenScenes.Count == 0) - { - string message = - $"❌ **Unity Closed**\n" + - $"👤 **{UserName}**\n" + - $"💻 **{MachineName}**\n" + - $"🕒 Closed Unity: **{FormatDateTime(quitAt)}**\n" + - $"📂 No tracked open scenes"; - - _ = SendDiscordMessage(message); return; - } - // Copy first so we can safely clear afterward var scenesToRelease = new List>(OpenScenes); foreach (var kvp in scenesToRelease) @@ -129,7 +135,6 @@ public static class SceneNotifier private static bool IsTrackableScene(UnityEngine.SceneManagement.Scene scene) { - // Prevent false notifications on startup / editor internals / untitled scenes if (string.IsNullOrWhiteSpace(scene.path)) return false; @@ -170,11 +175,7 @@ public static class SceneNotifier using var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); using var response = await Http.PostAsync(WebhookUrl, httpContent); - if (response.IsSuccessStatusCode) - { - Debug.Log($"[SceneNotifier] Sent to Discord:\n{content}"); - } - else + if (!response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Debug.LogWarning(