What are Unity WWW and WWWForm?
What are Unity WWW and WWWForm?
There are those among us who wonder how things like high score boards and user login are done in Unity. The modules we actually use to achieve these are the WWW and WWWForm modules. Let's take a closer look at these modules.
What is WWW?
WWW is the module that allows us to connect to websites from Unity. It allows you to access and use various data from internet sites within the game, such as user data, textures, high score data, and more.
It allows you to read and edit JSON files over the internet, create MYSQL connections, and communicate with websites in many other fields.
You can also send "GET" and "POST" data to forms/catchers found on websites using WWW.
In fact, I can say that I have seen people pull even audio data from the internet with this method while doing research online.
With WWW, you can control the "http://", "https://", and "file://" protocols; in addition, on IPhone devices, the "ftp://" protocol is also partially supported, but this applies only to anonymous connections.
Let's examine the example given by Unity about WWW.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg"; // The internet address where the texture file is located
IEnumerator Start()
{
using (WWW www = new WWW(url)) // The WWW class is called.
{
yield return www; // Waits for the site to be downloaded.
Renderer renderer = GetComponent<Renderer>(); // Selects the object's renderer component.
renderer.material.mainTexture = www.texture; // The texture in the object's Renderer is replaced with the texture downloaded from the internet.
}
}
} I think I have provided enough information about WWW, so I'll move on to our other module, WWWForm.
What is WWWForm?
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class WWWFormImage : MonoBehaviour
{
public string screenShotURL= "http://www.my-server.com/cgi-bin/screenshot.pl";
// Use this for initialization
void Start()
{
StartCoroutine(UploadPNG()); // Calls the UploadPNG function.
}
IEnumerator UploadPNG()
{
// Waits for the active frame to pass.
yield return new WaitForEndOfFrame();
//Creates a new texture in RGB 24 format.
int width = Screen.width; // Set the width
int height = Screen.height; // Set the height
var tex = new Texture2D( width, height, TextureFormat.RGB24, false ); // Created our texture file
// The image on the screen is cropped and assigned to the texture.
tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
// The created texture is converted to PNG.
byte[] bytes = tex.EncodeToPNG();
Destroy( tex );
// Create a WWWForm
WWWForm form = new WWWForm();
// Note: The "frameCount" here is the name of the catcher in the site's POST method, for example, on the site it is retrieved as $_POST["frameCount"].
form.AddField("frameCount", Time.frameCount.ToString()); // Here, the frame count is taken as text and added as a parameter into the form
form.AddBinaryData("fileUpload", bytes, "screenShot.png", "image/png"); // The created and converted texture is added into the form as a parameter.
// The created form is sent to the website.
using (var w = UnityWebRequest.Post(screenShotURL, form)) // Sent the data to the website with WebRequest.Post()
{
yield return w.SendWebRequest(); // Requested completion of reaching the site and returning the results
if (w.isNetworkError || w.isHttpError) { // If there is an error, error commands were printed
print(w.error);
}
else {
print("Finished Uploading Screenshot"); // If there were no errors, "Finished Uploading SS" was printed.
}
}
}
}

Yorum Gönder