Unity and ChatGPT Integration Demo: How to Use OpenAI API

HwanUk Song
4 min readJul 16, 2024

--

Photo by Emiliano Vittoriosi on Unsplash

This article describes the process of using the ChatGPT model with features within the Unity environment using the OpenAI API.

Project Repository

Other Langs Tutorials

Table of Contents

  1. 🔐 Obtaining an OpenAI API Key
  2. ⚙️ Installing Packages and Preparing for Communication
  3. 📜 Script Creation
  4. 📗 Example Usage
  5. 🤔 Expected Features

🔐 Obtaining an OpenAI API Key

Create an OpenAI account and obtain an API key. You can get the key issued from the OpenAI API docs.

1.Login to OpenAI

2.Since registering a card is mandatory, go to the Billing tab and register your card.

To prevent excessive charges, go to the Limits tab and set Usage limits.

3.Go to View API Keys and create a New Secret Key.

Once generated, copy the key value (note that the generated key cannot be re-displayed).

4.Verify that the key has been generated correctly.

  • Check the saved key.
  • Verify the supported model (this document uses the “gpt-3.5-turbo” model).

Windows

1.Verification of cURL Installation

curl --version

2.Creating cURL Command

curl https://api.openai.com/v1/chat/completions ^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer YOUR_API_KEY" ^
-d "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"Say this is a test\"}], \"max_tokens\": 5}"
Enter the key in the underline

MacOS

curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Say this is a test"
}
],
"max_tokens": 5
}'

⚙️ Installing Packages and Preparing for Communication

Use the Package Manager to install NuGetForUnity from the URL. Then, search for and install Newtonsoft.Json in the NuGet Packages tab.

📜 Script Creation

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json; // Use Newtonsoft.Json

public class OpenAIChatGPT : MonoBehaviour
{
private string apiKey = "YOUR_API_KEY"; // Replace with an actual API key
private string apiUrl = "https://api.openai.com/v1/chat/completions";

public IEnumerator GetChatGPTResponse(string prompt, System.Action<string> callback)
{
// Setting OpenAI API Request Data
var jsonData = new
{
model = "gpt-3.5-turbo",
messages = new[]
{
new { role = "user", content = prompt }
},
max_tokens = 20
};

string jsonString = JsonConvert.SerializeObject(jsonData);

// HTTP request settings
UnityWebRequest request = new UnityWebRequest(apiUrl, "POST");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonString);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "Bearer " + apiKey);

yield return request.SendWebRequest();

if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("Error: " + request.error);
}
else
{
var responseText = request.downloadHandler.text;
Debug.Log("Response: " + responseText);
// Parse the JSON response to extract the required parts
var response = JsonConvert.DeserializeObject<OpenAIResponse>(responseText);
callback(response.choices[0].message.content.Trim());
}
}

public class OpenAIResponse
{
public Choice[] choices { get; set; }
}

public class Choice
{
public Message message { get; set; }
}

public class Message
{
public string role { get; set; }
public string content { get; set; }
}
}
using UnityEngine;

public class ChatGPTExample : MonoBehaviour
{
[SerializeField, TextArea(3, 5)] private string prompt;

void Start()
{
OpenAIChatGPT chatGPT = gameObject.AddComponent<OpenAIChatGPT>();
StartCoroutine(chatGPT.GetChatGPTResponse(prompt, OnResponseReceived));
}

void OnResponseReceived(string response)
{
Debug.Log("ChatGPT Response: " + response);
}
}

📗 Example Usage

Prompt question : In the extended virtual world, what is the newly coined word that combines “meta,” which means virtual and transcendent, and “universe,” which means world and universe?

🤔 Expected Features

  • Generation Assistance Feature
  • Educational Quiz Game
  • User-Customizable Story Generator
  • AR-Based Educational Platform
    - Display 3D models in an AR environment to visualize learning themes
    - Provide real-time Q&A and explanations using ChatGPT
  • AR-Based Tour Guide
    - Visually provide information about tourist spots through AR
    -Use ChatGPT to provide additional information about tourist spots and answer questions
  • AR-Based Shopping Helper
    - Visually provide product information through AR
    - Use ChatGPT to provide product descriptions and answer questions

--

--

HwanUk Song

I am a Unity software engineer residing in Seoul. I like to keep all possibilities open with flexibility