[ad_1]
It is a mix of this tutorial on YouTube: How to Use UnityNetRequest – Replacement for WWW Class – Unity Tutorial and this reply on the same query: UnityNetRequest POST to PHP not work
The know-how I’m utilizing:
- Laravel Framework 7.15.0 (PHP)
- Unity: 2019.3.11f1 (script is C#)
Now I’m simply sending primary varieties not photographs and many others – so I have never tried something advanced however this sends a get to 1 URL on Laravel which returns the CSRF token.
This is then added as a further area to the submit request.
I’m clicking the two buttons fairly shortly – I might recommend (and can implement this myself later) that you do not ask for the token till your able to submit the shape – to keep away from it expiring in case your kind is sort of lengthy and many others.
The C# Script (Unity)
Both the capabilities are set as ON Click() on the buttons in a easy UI Canvas in Unity.
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.Networking;
utilizing UnityEngine.UI;
public class BasicWebCall : MonoBehaviour
{
public Text messageText;
public InputField ratingToShip;
personal string csrf_token;
readonly string getURL = "http://mazegames.check/leaderboard/1";
readonly string postURL = "http://mazegames.check/register/1";
personal void Start()
{
messageText.textual content = "Press buttons to work together with net server";
}
public void OnButtonGetRating()
{
messageText.textual content = "Getting Token";
StartCoroutine(EasyGetRequest());
}
IEnumerator EasyGetRequest()
{
UnityNetRequest www = UnityNetRequest.Get(getURL);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
messageText.textual content = "Token Received: " + www.downloadHandler.textual content;
csrf_token = www.downloadHandler.textual content;
}
}
public void OnButtonSendScore()
{
if (ratingToShip.textual content == string.Empty)
{
messageText.textual content = "Error: No excessive rating to ship.nEnter a price within the enter area.";
}
else
{
messageText.textual content = "Sending Post Request";
StartCoroutine(EasyPostRequest(ratingToShip.textual content));
}
}
IEnumerator EasyPostRequest(string curScore)
{
Dictionary<string, string> wwwForm = new Dictionary<string, string>();
wwwForm.Add("_token", csrf_token);
UnityNetRequest www = UnityNetRequest.Post(postURL, wwwForm);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
messageText.textual content = www.downloadHandler.textual content;
}
}
}
Laravel routes/net.php
Obviously it’s worthwhile to configure your finish factors to obtain your knowledge, validate and retailer it if obligatory. In this instance its simply validating the token as a part of the submit request.
Route::submit('register/{game_id}', operate($game_id) {
return "Game On!";
});
Route::get('leaderboard/{game_id}', operate($game_id) {
return csrf_token();
});
And that’s just about it – hope this helps another person.
#EDIT# – Request Token on Submission
To solely get the token when your submitting the shape, actually all it’s a must to do is put this line:
StartCoroutine(EasyGetRequest());
above the road:
StartCoroutine(EasyPostRequest(ratingToShip.textual content));
so that’s seems to be like this:
StartCoroutine(EasyGetRequest());
StartCoroutine(EasyPostRequest(ratingToShip.textual content));
Obviously you would then take away the operate EasyGetRequest altogether.
[ad_2]