47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
[InitializeOnLoad]
|
|
public class SceneNotifier
|
|
{
|
|
private static readonly string WebhookUrl = "http://n8n.homelab/webhook/unity-scene";
|
|
private static readonly string UserName = System.Environment.UserName;
|
|
|
|
private static readonly HttpClient Http = new HttpClient();
|
|
|
|
static SceneNotifier()
|
|
{
|
|
EditorSceneManager.sceneOpened += OnSceneOpened;
|
|
EditorSceneManager.sceneClosed += OnSceneClosed;
|
|
}
|
|
|
|
private static void OnSceneOpened(UnityEngine.SceneManagement.Scene scene, OpenSceneMode mode)
|
|
{
|
|
_ = SendNotification(scene.name, "opened");
|
|
}
|
|
|
|
private static void OnSceneClosed(UnityEngine.SceneManagement.Scene scene)
|
|
{
|
|
_ = SendNotification(scene.name, "closed");
|
|
}
|
|
|
|
private static async Task SendNotification(string sceneName, string action)
|
|
{
|
|
string json = $"{{\"scene\":\"{sceneName}\",\"user\":\"{UserName}\",\"action\":\"{action}\"}}";
|
|
try
|
|
{
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
var response = await Http.PostAsync(WebhookUrl, content);
|
|
Debug.Log($"[SceneNotifier] Sent: {json} — Status: {response.StatusCode}");
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogWarning($"[SceneNotifier] Failed to notify: {e.Message}");
|
|
}
|
|
}
|
|
}
|