A minimal Unity project that reads player data from a JSON web API into UI text with a GET request and sends a health update with a POST request.
On play, the scene requests a player profile (name, health, personality, charm, behavior, management and an image URL) from a mock REST endpoint, shows each value in a UI Text, and downloads the image into a RawImage. At the same time it POSTs a new Health value and shows the server's reply.
The endpoint is served by My JSON Server from the db.json in unity-mock-player-data. The same scripts are reused in a 3D pickup demo, unity-fps-json-sync.
- Demo.cs defines a
Datastruct whose fields match the JSON keys (Name,Health,Personality,Charm,Behavior,managment,ImageURL). InStartit runs a coroutine that callsUnityWebRequest.Get(jsonURL), parses the body withJsonUtility.FromJson<Data>, writes the values into sixTextfields, then callsUnityWebRequestTexture.GetTexture(data.ImageURL)and assigns the result touiRawImage.texture. - PostMethod.cs builds a
WWWFormwithHealth=110, sends it withUnityWebRequest.Postto the same URL, showsLoading...while waiting, and writes theHealthfrom the response into itsText. It runs fromStart. - Both scripts use
https://my-json-server.typicode.com/MemeGamer/MockData/UnityDataas the endpoint. Errors are swallowed (GET) or logged asError(POST).
- Unity 2020.3.25f1
- UGUI (
com.unity.ugui1.0.0), TextMeshPro 3.0.6 (in the manifest, not used by the scripts) UnityWebRequest,UnityWebRequestTexture,JsonUtility
- Open the folder in Unity Hub with Unity 2020.3.25f1 (newer 2020.3 or later versions should upgrade it).
- Open
Assets/Scene.unityand press Play. The[ Demo.cs ]object already has both scripts with their UI fields assigned. - To use your own data, change
jsonURLinDemo.csanduriinPostMethod.cs, and keep the JSON keys matching theDatastruct.
Any URL that returns a raw JSON object with the same keys works for the GET request:
- My JSON Server: put a
db.jsonwith a top-levelUnityDataobject in a public GitHub repo, then usehttps://my-json-server.typicode.com/<user>/<repo>/UnityData. - Google Drive: upload a
data.json, share it as "Anyone with the link", convert the share link to a direct download link (for example with this generator), and set it asjsonURL. - Local server:
http://localhost:PORT/file.json.
Example data:
{
"Name": "Player1",
"Health": "100",
"Personality": "2",
"Charm": "2",
"Behavior": "2",
"managment": "3",
"ImageURL": "https://drive.google.com/uc?export=download&id=YOUR_IMAGE_ID"
}| Script | Purpose |
|---|---|
Assets/Demo.cs |
GET request, fills UI text and downloads the profile image |
Assets/PostMethod.cs |
POST request that sends a new Health value |
- The URL in both scripts still uses the old mock data repo name
MockData. The repo is nowunity-mock-player-data; update the URL tohttps://my-json-server.typicode.com/MemeGamer/unity-mock-player-data/UnityDataif the old one stops resolving. At the time of writing, My JSON Server returned errors for both URLs, so the demo may need a different host. - My JSON Server is a fake REST API: POST returns a response but does not save the change, so a later GET still returns the original data.
isNetworkErrorandisHttpErrorare deprecated in Unity 2020.2 and later; they still compile but show warnings.request.resultis the modern replacement.UserSettings/is committed; it holds per-user editor preferences and is usually gitignored.
