1
\$\begingroup\$

I am creating a 2D MMORPG, I created a forum to store player information( username, password etc.). Could someone please help me understand the new Unity Web System so i can change my code for optimal functionality.

IEnumerator ProcessRequest(string username, string password, string url, string age = null, string email = null)
{

    WWWForm form = new WWWForm();

    form.AddField("username", username);
    form.AddField("password", password);
    form.AddField("age", age);
    form.AddField("email", email);

    WWW request = new WWW(url, form);

    yield return request;

    if (string.IsNullOrEmpty(request.error))
    {
        Debug.Log(request.text);
    }

This results in:

'WWW' is obsolete: 'Use UnityWebRequest, a fully featured replacement which is more efficient and has additional features'

I read over the official documentation UnityWebRequest , however in my contents I honestly did not really understand how I can implement it...

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

There are three primary methods for sending data to a server, formatted as an HTML Form using unity web request.

1st : Using WWWForm (which is a legacy method, that UnityWebRequest still supports)

IEnumerator ProcessRequest(string username, string password, string url, string age = null, string email = null)
{
    WWWForm form = new WWWForm();
    
    form.AddField("username", username);
    form.AddField("password", password);
    form.AddField("age", age);
    form.AddField("email", email);


    UnityWebRequest www = UnityWebRequest.Post(url, form);
    yield return www.SendWebRequest();

    if (www.result != UnityWebRequest.Result.Success)
    {
        Debug.Log(www.error);
    }
    else 
    {
        Debug.Log("Form upload complete!");
    }
}

2nd: Using IMultipartFormSection

List<IMultipartFormSection> form = new List<IMultipartFormSection>();

form.Add(new MultipartFormDataSection("username", username));
form.Add(new MultipartFormDataSection("password", password));
form.Add(new MultipartFormDataSection("age", age != null ? age : "0"));
form.Add(new MultipartFormDataSection("email", email));

UnityWebRequest www = UnityWebRequest.Post(url, form);
yield return www.SendWebRequest();

if (www.result != UnityWebRequest.Result.Success)
{
    Debug.Log(www.error);
}
else 
{
    Debug.Log("Form upload complete!");
}

3rd: JSON Serialization Use the JsonUtility class to convert Unity objects to and from the JSON format. - this would require some additional code

You would begin by creating a new class that would be used to serialize your form:

[Serializable] 
public class myForm
{
    public string username;
    public string password;
    public string age;
    public string email;
}

The class is using the [Serializable] attribute, in order to work with the JSON serializer. Then create an instance of your class, for example:

myForm form = new myForm();
form.username = "UsrNamesss";
form.password = "11111111";
form.age = "23";
form.email = "[email protected]";

And to wrap it up - Serialize it using JsonUtility and Post it using UnityWebRequest

IEnumerator ProcessRequest(string url, myForm form)

Convert it (which can be done on the other direction like this: newForm = JsonUtility.FromJson<myForm>(json); )

string json = JsonUtility.ToJson(myForm);

and just post it using UnityWebRequest:

UnityWebRequest www = UnityWebRequest.Post(url, form);
yield return www.SendWebRequest();

if (www.result != UnityWebRequest.Result.Success)
{
    Debug.Log(www.error);
}
else 
{
    Debug.Log("Form upload complete!");
}

I find the 3rd solution better in most cases as it allows me to better manage data and models within my apps.

\$\endgroup\$
3
  • \$\begingroup\$ After using method 2, I ran into this. My understanding that its trying to convert a int into a string, however im not entirely sure where or what... . Cannot implicitly convert type 'System.Collections.Generic.List<UnityEngine.Networking.MultipartFormDataSection>' to 'System.Collections.Generic.List<UnityEngine.Networking.IMultipartFormSection>' \$\endgroup\$ Aug 13, 2021 at 18:07
  • 1
    \$\begingroup\$ @IyaadSaley I've edited my answer above. Didn't notice these when writing :). \$\endgroup\$
    – RoiXR
    Aug 13, 2021 at 19:28
  • \$\begingroup\$ I am really struggling with this web request. I created a php script to handle the player signup, it works correct however i added in a function to check if the username is already taken, and when i input a username thats already taken, it doesnt add it into the database however it does not show me a error message. It simply says "Form upload complete!". Id be more than happy to post my php script if needed. \$\endgroup\$ Aug 14, 2021 at 16:13
0
\$\begingroup\$

Using the 2nd Method

List<IMultipartFormSection> form = new List<MultipartFormDataSection>();

Unity Error :

Assets\NetworkManager.cs(96,48): error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<UnityEngine.Networking.MultipartFormDataSection>' to 'System.Collections.Generic.List<UnityEngine.Networking.IMultipartFormSection>'

While a MultipartFormDataSection can be used as a IMultipartFormSection (which it implements), the same does not apply to collections. So List is not compatible with new List, for C# those are completely separate types even if they are seemingly compatible.

You can just turn the list into List, adding the individual data sections will still work.

 form.Add(new MultipartFormDataSection("age", age != null ? age : 0));

Unity Error :

Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'int'

age is a string but the 0 in your conditional expression is an int. Both sides of a conditional expression need to return the same type, so you have to use "0" instead.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .