Skip to content

About

Unity 2020.3 demo: GET player data from a mock JSON API into UI text and POST a health update

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

Illustration of a game scene and a character stats card connected by download and upload arrows to a cloud holding a JSON file

Unity JSON GET and POST

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.

What it does

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.

How it works

  • Demo.cs defines a Data struct whose fields match the JSON keys (Name, Health, Personality, Charm, Behavior, managment, ImageURL). In Start it runs a coroutine that calls UnityWebRequest.Get(jsonURL), parses the body with JsonUtility.FromJson<Data>, writes the values into six Text fields, then calls UnityWebRequestTexture.GetTexture(data.ImageURL) and assigns the result to uiRawImage.texture.
  • PostMethod.cs builds a WWWForm with Health=110, sends it with UnityWebRequest.Post to the same URL, shows Loading... while waiting, and writes the Health from the response into its Text. It runs from Start.
  • Both scripts use https://my-json-server.typicode.com/MemeGamer/MockData/UnityData as the endpoint. Errors are swallowed (GET) or logged as Error (POST).

Tech

  • Unity 2020.3.25f1
  • UGUI (com.unity.ugui 1.0.0), TextMeshPro 3.0.6 (in the manifest, not used by the scripts)
  • UnityWebRequest, UnityWebRequestTexture, JsonUtility

Use it

  1. Open the folder in Unity Hub with Unity 2020.3.25f1 (newer 2020.3 or later versions should upgrade it).
  2. Open Assets/Scene.unity and press Play. The [ Demo.cs ] object already has both scripts with their UI fields assigned.
  3. To use your own data, change jsonURL in Demo.cs and uri in PostMethod.cs, and keep the JSON keys matching the Data struct.

Using your own JSON file

Any URL that returns a raw JSON object with the same keys works for the GET request:

  • My JSON Server: put a db.json with a top-level UnityData object in a public GitHub repo, then use https://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 as jsonURL.
  • 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"
}

Scripts

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

Notes

  • The URL in both scripts still uses the old mock data repo name MockData. The repo is now unity-mock-player-data; update the URL to https://my-json-server.typicode.com/MemeGamer/unity-mock-player-data/UnityData if 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.
  • isNetworkError and isHttpError are deprecated in Unity 2020.2 and later; they still compile but show warnings. request.result is the modern replacement.
  • UserSettings/ is committed; it holds per-user editor preferences and is usually gitignored.

About

Unity 2020.3 demo: GET player data from a mock JSON API into UI text and POST a health update

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages