Unity Network - nomrand/__basics GitHub Wiki

Rest I/F

GET

Text

In main-flow,

   StartCoroutine(GetText());

And in sub-routine

    IEnumerator GetText()
    {
        UnityWebRequest www = UnityWebRequest.Get(***URL***);
        // Send & Wait
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            // Error Route
            Debug.LogError(www.error);
        }
        else
        {
            // OK Route
            (***Some TextMesh***).text = www.downloadHandler.text;

            //  Or Bytes
            //           byte[] results = www.downloadHandler.data;
        }
    }

Image (=Texture)

In sub-routine,

    IEnumerator GetImage()
    {
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(***URL***);
        // Send & Wait
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            // Error Route
        }
        else
        {
            // OK Route
            (***Some Material***).mainTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
        }
    }

POST

Text

In sub-routine,

    IEnumerator PostData(string str)
    {
        List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
        // set parameters ...
        formData.Add(new MultipartFormDataSection("text=" + str));

        UnityWebRequest www = UnityWebRequest.Post(***URL***, formData);
        // Send & Wait
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            // Error Route
        }
        else
        {
            // OK Route
            Debug.Log("Form upload complete!");
        }
    }

Put

Raw Bytes

In sub-routine,

    IEnumerator PutData(byte[] bytes)
    {
        UnityWebRequest www = UnityWebRequest.Put(***URL***, bytes);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Upload complete!");
        }
    }

WebSocket

Client

Create WebSocket Script & Call By button or something.

(exe=OK, WebGL new app=NG(maybe security reason))

using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
   .....

    public string WebSocketServer = "ws://localhost:8765";

    public async void DoWsSend(string str)
    {
        ClientWebSocket clientWebSocket = new ClientWebSocket();
        try
        {
            Uri uri = new Uri(WebSocketServer);
            await clientWebSocket.ConnectAsync(uri, CancellationToken.None);
            if (clientWebSocket.State == WebSocketState.Open)
            {
                Debug.Log("[WS][connect]:" + "Connected");

                // Send
                ArraySegment<byte> bytesToSend = new ArraySegment<byte>(
                    Encoding.UTF8.GetBytes(str)
                );
                await clientWebSocket.SendAsync(
                    bytesToSend,
                    WebSocketMessageType.Text,
                    true,
                    CancellationToken.None
                );

                // Get Response
                ArraySegment<byte> bytesReceived = new ArraySegment<byte>(new byte[1024]);
                WebSocketReceiveResult result = await clientWebSocket.ReceiveAsync(
                    bytesReceived,
                    CancellationToken.None
                );

                // Out
                outText.text = (Encoding.UTF8.GetString(bytesReceived.Array, 0, result.Count));
            }
        }
        catch (Exception e)
        {
            Debug.LogError("[WS][exception]:" + e.Message);
            if (e.InnerException != null)
            {
                Debug.LogError("[WS][inner exception]:" + e.InnerException.Message);
            }
        }
        Debug.Log("[WS]:" + "Finnished");
    }
}
⚠️ **GitHub.com Fallback** ⚠️