Hi everyone,
it’s been a while since I used Unity.
I recently wanted to continue my Quiz-App.
However, I was wondering how it would be possible to connect to a web server to get data and stuff.
Features that I want to implement:
- check if net connection is available
- new questions should be retrievable from the net, instead of upgrading the app. (or at least an info that there is a new version)
- pass data to the net, like which questions were answered correct or wrong, so I can calculate a “correct rate”
I suppose that there are more than one way to do it, however I have never worked with net features before,
so I would appreciate it if someone has an easy tutorial.
Thanks
This is basically a topic that has nothing to do with Unity itself. To get started with making simple web requests over HTTP in C#, you can start here: https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient
There is one very important caveat when writing this kind of code in the context of Unity though: you have to make sure that if you are making any requests, you are not doing so in the main thread! If you do, the game will appear to freeze for the entire duration of the network request. You have to push networking code into a background thread. A simple way to do that (in .NET 4+) is this:
Task.Run(() => {
// Your networking code here
});
Note that any code running inside that block cannot access any Unity resources such as GameObjects, Textures, etc…! You will need a thread-safe way to transfer the response data of your requests back into the main thread. One simple option is https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1, which you can populate from your background task and read from in an Update() method on a Monobehaviour safely.
Thank you for your answer.
I will read the two links that you’ve send.
However, if there is an example for unity I would appreciate it.
BTW, I was just searching for unity examples and I found this documentaion:
Is it better to use the “Unity-version” or the “Microsoft-verion” ?
Ah wow, I feel silly as I didn’t even know UnityWebRequest existed. I would suggest using UnityWebRequest because it looks like it already has built-in handling for doing the request in the background and getting the data back to your MonoBehaviour scripts. It’s likely that UnityWebRequest uses the “microsoft” code (or mono’s implementation at least) under the hood.
It looks like you may have to write three pieces depending on the features you need:
UploadHandler
DownloadHandler
UnityWebRequest
OK, retrieving Text is no problem.
Even though I don’t know if this is a good way, since I will start to iterate through the whole text looking for keywords and then my script will do something.
However, I have no idea what POST or PUT does.
POST: Unity - Manual: Sending a form to an HTTP server (POST)
PUT: Unity - Manual: Uploading raw data to an HTTP server (PUT)
When I use the post code, it says that it has been successfully posted, but I don’t know what it posted.
Nothing in server manager changed.
When using the PUT code, it gives me
“Curl error 52: Empty reply from Server”
and
“Received no data in response”
in the console
I registered to 000webhost in order to test it.
POST and PUT (and GET) are HTTP methods. See HTTP - Methods
Ultimately the HTTP method you should use will depend heavily on what the server you are working with supports. Are you working with an existing web server of some kind, or building your own?
I’m using the “File Manager” that is provided by 000webhost
OK, I’ve been searching for the net for hours and it seems to be either more difficult than I thought or I just can’t find it.
So I’m making a simple question (whereas the answer might be complicated):
But is it possible to send a text via unity to my HTTP and modify it?
Meaning that I will be able to see the text on my webhost?
If someone has an example code (including the php if there is) I’d appreciate it.
Thank you