90 lines
2.5 KiB
C#
90 lines
2.5 KiB
C#
using System.Text;
|
|
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using System.IO;
|
|
|
|
[InitializeOnLoad]
|
|
public static class DiscordSceneNotifier
|
|
{
|
|
private const string WebhookUrl = "https://discord.com/api/webhooks/1486718530765328424/42mP_zYNTEwwBbc_lcuasPbpZppXoC4U14-Pde1YatdxatT5s9vml8lgQXTwAmm0PNe5";
|
|
|
|
static DiscordSceneNotifier()
|
|
{
|
|
EditorSceneManager.sceneOpened += OnSceneOpened;
|
|
EditorSceneManager.sceneClosed += OnSceneClosed;
|
|
EditorApplication.quitting += OnEditorQuitting;
|
|
}
|
|
|
|
private static string GetUserName()
|
|
{
|
|
return System.Environment.UserName;
|
|
}
|
|
|
|
private static string FormatPath(string path)
|
|
{
|
|
return path.Replace("Assets/", "");
|
|
}
|
|
|
|
private static void OnSceneOpened(UnityEngine.SceneManagement.Scene scene, OpenSceneMode mode)
|
|
{
|
|
string user = GetUserName();
|
|
string path = FormatPath(scene.path);
|
|
|
|
string message =
|
|
$"🚧 **Scene Locked**\n" +
|
|
$"👤 **{user}**\n" +
|
|
$"📁 `{path}`";
|
|
|
|
SendDiscordMessage(message);
|
|
}
|
|
|
|
private static void OnSceneClosed(UnityEngine.SceneManagement.Scene scene)
|
|
{
|
|
string user = GetUserName();
|
|
string path = FormatPath(scene.path);
|
|
|
|
string message =
|
|
$"🔓 **Scene Released**\n" +
|
|
$"👤 **{user}**\n" +
|
|
$"📁 `{path}`";
|
|
|
|
SendDiscordMessage(message);
|
|
}
|
|
|
|
private static void OnEditorQuitting()
|
|
{
|
|
string user = GetUserName();
|
|
|
|
string message =
|
|
$"❌ **Unity Closed**\n" +
|
|
$"👤 **{user}**\n" +
|
|
$"All scenes released";
|
|
|
|
SendDiscordMessage(message);
|
|
}
|
|
|
|
private static async void SendDiscordMessage(string content)
|
|
{
|
|
string json = JsonUtility.ToJson(new DiscordMessage { content = content });
|
|
|
|
using var request = new UnityWebRequest(WebhookUrl, "POST");
|
|
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
|
|
|
|
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
|
|
await request.SendWebRequest();
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
Debug.LogWarning("Discord webhook failed: " + request.error);
|
|
}
|
|
|
|
[System.Serializable]
|
|
private class DiscordMessage
|
|
{
|
|
public string content;
|
|
}
|
|
} |