I am developing a graphic interface with unity and want it to interact with a Django REST API. I don't seem to have any documentation on this issue. Is this possible ? Do you have any documentation that could help ?
Asked
Active
Viewed 2,553 times
0
-
What do you really need? You have C# on Unity3D side. You can use the same methods you would use on C#. – Dragin Mar 19 '18 at 12:38
1 Answers
2
You can use any C# function to communicate to a web server. You might only have restrictions depending on the platform, for example, android will bock web access if you don't explicitly require it in the manifest.
Unity also provides helper for web communication. Have a look at the UnityWebRequest class to send data, or the WWW class to retrieve data.
This page of the manual provides a complete example
Example, how to collect data:
public string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
IEnumerator Start()
{
using (WWW www = new WWW(url))
{
yield return www;
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = www.texture;
}
}
Example, how to send data
void Start() {
StartCoroutine(Upload());
}
IEnumerator Upload() {
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add( new MultipartFormDataSection("field1=foo&field2=bar") );
formData.Add( new MultipartFormFileSection("my file data", "myfile.txt") );
UnityWebRequest www = UnityWebRequest.Post("http://www.my-server.com/myform", formData);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
}
}

Basile Perrenoud
- 4,039
- 3
- 29
- 52