Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.9 kB
5
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;

public class SendData : MonoBehaviour
{
    public GameObject panel;
    public void PostData()
    {
        //ya pas d'using a mettre a ma connaissance pour utiliser la dll

        // call http post
        Contact contact = new Contact();

        
        contact.email = GameObject.FindWithTag("inputMail").GetComponent<InputField>().text;
        contact.attributes = new Attributes();
        contact.attributes.nom = GameObject.FindWithTag("inputName").GetComponent<InputField>().text;

        //contact.Update = false;
        string json = JsonUtility.ToJson(contact);
        Debug.Log(json);
        //byte[] data = System.Text.Encoding.UTF8.GetBytes(json);

        var url = "https://api.sendinblue.com/v3/contacts";

        try
        {
            var request = UnityWebRequest.Post(url, json);
            request.SetRequestHeader("api-key", "MET TA CLE API ICI");
            request.SetRequestHeader("Content-Type", "application/json");

            byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
            request.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
            request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

            StartCoroutine(onResponse(request));
        }
        catch { Debug.Log("ERROR creating request"); }

        panel = GameObject.FindWithTag("panelTag");
        panel.gameObject.SetActive(false);

        IEnumerator naze = TakeScreenAndShare();
    }

    private IEnumerator TakeScreenAndShare()
    {
        yield return new WaitForEndOfFrame();

        Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        ss.Apply();

        string filePath = System.IO.Path.Combine(Application.temporaryCachePath, "cerealis.png");
        System.IO.File.WriteAllBytes(filePath, ss.EncodeToPNG());

        Destroy(ss);

        new NativeShare().AddFile(filePath)
                .SetSubject("").SetText("").SetUrl("")
                .SetCallback((res, target) => Debug.Log($"result {res}, target app: {target}"))
                .Share();
    }

    private IEnumerator onResponse(UnityWebRequest req)
    {

        yield return req.SendWebRequest();
        if (req.isError)
            Debug.Log("Network error has occured: " + req.GetResponseHeader(""));
        else
            Debug.Log("Success " + req.downloadHandler.text);
        byte[] results = req.downloadHandler.data;
        Debug.Log("Second Success");
    }
}

[Serializable]
internal class Contact
{
    public string email;
    public Attributes attributes;
    //public bool Update;
}

[Serializable]
internal class Attributes
{
    public string nom;
}