Displaying a Game Object Using JSON File in Unity: A Step-by-Step Guide

Introduction:

Gsuryateja
3 min readJun 12, 2023

In Unity, JSON (JavaScript Object Notation) files are commonly used for storing and exchanging data. They provide a flexible way to represent complex objects, making them an excellent choice for saving and loading Game Objects. In this article, we will explore how to display a Game Object using a JSON file in Unity. We will cover the process step-by-step and provide example code to help you understand the implementation.

Step 1: Set up the Project:

Before we begin, ensure you have a Unity project set up and ready to go. Create a new scene or open an existing one where you want to display the Game Object from the JSON file.

Step 2: Create a JSON File:

To start, we need a JSON file that contains the data of the Game Object you want to display. You can either create a JSON file manually or use a tool/library to generate it programmatically. For simplicity, let’s assume we have a JSON file called “gameObjectData.json” with the following structure:

{
"name": "Cube",
"position": {
"x": 0,
"y": 0,
"z": 0
},
"rotation": {
"x": 0,
"y": 0,
"z": 0
},
"scale": {
"x": 1,
"y": 1,
"z": 1
}
}

Step 3: Parsing the JSON Data:

In Unity, you can utilize the built-in JsonUtility class to parse JSON data into C# objects. Create a C# script, e.g., “GameObjectLoader.cs,” and attach it to an empty Game Object in your scene. In this script, add the following code:

using UnityEngine;

public class GameObjectLoader : MonoBehaviour
{
public string jsonFilePath;

private void Start()
{
string jsonContent = System.IO.File.ReadAllText(jsonFilePath);
GameObjectData gameObjectData = JsonUtility.FromJson<GameObjectData>(jsonContent);

// Display the GameObject using the parsed data
DisplayGameObject(gameObjectData);
}

private void DisplayGameObject(GameObjectData data)
{
GameObject gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
gameObject.name = data.name;
gameObject.transform.position = new Vector3(data.position.x, data.position.y, data.position.z);
gameObject.transform.rotation = Quaternion.Euler(data.rotation.x, data.rotation.y, data.rotation.z);
gameObject.transform.localScale = new Vector3(data.scale.x, data.scale.y, data.scale.z);
}
}

[System.Serializable]
public class GameObjectData
{
public string name;
public Vector3Data position;
public Vector3Data rotation;
public Vector3Data scale;
}

[System.Serializable]
public class Vector3Data
{
public float x;
public float y;
public float z;
}

Step 4: Set the JSON File Path:

In the Unity Editor, select the Game Object with the “GameObjectLoader” script attached, and in the Inspector, provide the file path to the JSON file using the “jsonFilePath” field.

Step 5: Run the Scene:

Press the Play button in the Unity Editor, and the Game Object specified in the JSON file should be displayed in the scene. You can modify the JSON file to update the position, rotation, scale, or any other property of the Game Object.

Conclusion:

Using JSON files to store and load Game Object data in Unity provides a flexible and convenient approach. By leveraging the JsonUtility class, you can easily parse JSON data into C# objects and utilize the extracted information to display GameObjects dynamically. This article has guided you through the necessary steps, along with example code, to help you display a GameObject using a JSON file in Unity. With this knowledge, you can now incorporate JSON files into your Unity projects for more efficient data management and GameObject representation.

--

--

No responses yet