How to run a local AI model inside Unity
You can chat with an AI model from inside the Unity Editor without an account, an API key or sending your code anywhere. The model runs on your own computer through Ollama, and Unity talks to it over a local connection.
Why run it locally?
- Private: your scripts and questions never leave your machine.
- Free to use: no subscription or per-message cost.
- Works offline once the model is downloaded.
The trade-off is speed and size: local models are smaller than the big cloud ones, and they run best on a computer with a graphics card that has room for the model.
1. Install Ollama and a model
Download Ollama, a free app, from ollama.com and install it. Then open a terminal and download a model made for code:
ollama pull qwen2.5-coder:7b
That's a 4.7 GB download that suits most PCs with 16 GB of memory. On a smaller machine use qwen2.5-coder:3b (1.9 GB), and with 32 GB or more try qwen2.5-coder:14b (9 GB).
Ollama runs in the background and answers on http://localhost:11434. To check it's working, run ollama run qwen2.5-coder:7b and ask it something.
2. Add an editor window
Create a folder called Editor inside Assets and add LocalAIWindow.cs:
using System;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
public class LocalAIWindow : EditorWindow {
[Serializable] class Message { public string role; public string content; }
[Serializable] class ChatRequest { public string model; public Message[] messages; public bool stream; }
[Serializable] class ChatResponse { public Message message; }
const string Model = "qwen2.5-coder:7b";
string prompt = "Explain what a ScriptableObject is in two sentences.";
string reply = "";
bool waiting;
Vector2 scroll;
[MenuItem("Tools/Local AI")]
static void Open() {
GetWindow<LocalAIWindow>("Local AI");
}
void OnGUI() {
prompt = EditorGUILayout.TextArea(prompt, GUILayout.Height(60));
using (new EditorGUI.DisabledScope(waiting)) {
if (GUILayout.Button(waiting ? "Thinking..." : "Ask")) {
Ask();
}
}
scroll = EditorGUILayout.BeginScrollView(scroll);
EditorGUILayout.TextArea(reply, EditorStyles.wordWrappedLabel);
EditorGUILayout.EndScrollView();
}
void Ask() {
var body = new ChatRequest {
model = Model,
messages = new[] { new Message { role = "user", content = prompt } },
stream = false
};
var request = new UnityWebRequest("http://localhost:11434/api/chat", "POST");
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonUtility.ToJson(body)));
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.timeout = 300;
waiting = true;
request.SendWebRequest().completed += _ => {
if (request.result == UnityWebRequest.Result.Success) {
reply = JsonUtility.FromJson<ChatResponse>(request.downloadHandler.text).message.content;
} else {
reply = $"Error: {request.error}. Is Ollama running?";
}
waiting = false;
request.Dispose();
Repaint();
};
}
}
3. Ask it something
Open Tools > Local AI, type a question and press Ask. The first reply takes longer while Ollama loads the model into memory; after that, answers come back much faster.
If Unity refuses the request with an "insecure connection" error, go to Project Settings > Player > Other Settings and set Allow downloads over HTTP to allow it in development.
Where to take it next
This window answers one question at a time and knows nothing about your project. To make it genuinely useful you'd add:
- Chat history: send the earlier messages in the
messagesarray, so it remembers the conversation. - Streaming: set
streamto true and read the reply as it's written, instead of waiting for the whole answer. - Project context: paste the script you're asking about into the message, or let the model ask to read files.
- Safe edits: show proposed changes before writing anything to disk, with a way to undo.