using System.Collections; using System.Text; using UnityEngine; using UnityEngine.Networking; // EmailSender.cs // Attache ce script à un GameObject. Branche SendDesign(email) sur le bouton // "Envoyer" de ton formulaire, en lui passant le texte du champ d'adresse. // // Le script ne fait QUE transmettre l'adresse à ton backend. Aucune clé API, // aucun PDF côté Unity — tout reste sur le serveur. public class EmailSender : MonoBehaviour { [Tooltip("URL de ta fonction serverless déployée")] public string endpoint = "https://ton-app.vercel.app/api/send-design"; [System.Serializable] private class Payload { public string email; } /// /// À appeler depuis l'UI (ex : onClick du bouton), avec l'adresse saisie. /// public void SendDesign(string email) { StartCoroutine(SendDesignCoroutine(email)); } private IEnumerator SendDesignCoroutine(string email) { string json = JsonUtility.ToJson(new Payload { email = email }); byte[] body = Encoding.UTF8.GetBytes(json); using (UnityWebRequest req = new UnityWebRequest(endpoint, "POST")) { req.uploadHandler = new UploadHandlerRaw(body); req.downloadHandler = new DownloadHandlerBuffer(); req.SetRequestHeader("Content-Type", "application/json"); yield return req.SendWebRequest(); if (req.result == UnityWebRequest.Result.Success) { Debug.Log("Email envoyé ! Réponse : " + req.downloadHandler.text); // TODO : afficher un message de succès dans ton UI } else { Debug.LogError($"Erreur {req.responseCode} : {req.error}\n{req.downloadHandler.text}"); // TODO : afficher un message d'erreur dans ton UI } } } }