ZenCoder Tips & Tricks: Networking & Client-Side¶
ZenCoder gives you the power to simulate, inspect, and debug client-side behaviors like networking, data serialization, JSON parsing, and local state - directly from the Inspector without recompilation.
Whether you're working on REST APIs, local save systems, or JSON-driven UIs, ZenCoder helps you test, tweak, and iterate faster than ever.
💡 ZenCoder scripts run in real-time in both Edit and Play Mode (via
Start()), and continuously in Play Mode viaUpdate(float deltaTime).
No recompilation. No reloads. Instant feedback.
Quick API Call & Response Check¶
Use case: Trigger an external GET request and inspect the result.
public async void Start()
{
using var client = new System.Net.Http.HttpClient();
var result = await client.GetStringAsync("https://jsonplaceholder.typicode.com/posts/1");
Debug.Log("Response: " + result);
}
Send Data to an API (POST)¶
Use case: Simulate a client POST request from within the Editor.
public async void Start()
{
var json = "{\"username\": \"ZenUser\", \"score\": 99}";
using var content = new System.Net.Http.StringContent(json, System.Text.Encoding.UTF8, "application/json");
using var client = new System.Net.Http.HttpClient();
var response = await client.PostAsync("https://your-api.com/submit", content);
Debug.Log("POST status: " + response.StatusCode);
}
Test JSON Parsing and Mapping¶
Use case: Parse external JSON into a local data structure.
[System.Serializable]
public class PlayerData
{
public string name;
public int score;
}
public void Start()
{
var json = "{\"name\": \"Atef\", \"score\": 420}";
var player = JsonUtility.FromJson<PlayerData>(json);
Debug.Log($"Name: {player.name}, Score: {player.score}");
}
Inspect Serialized Object State¶
Use case: Serialize a live component’s data and preview its JSON output.
target can be any MonoBehaviour or serializable component. Helps visualize current state or cache it for backups.
Local Data File Writing (Persistent Path)¶
Use case: Save data as a file locally on device.
public void Start()
{
var path = Application.persistentDataPath + "/test.json";
System.IO.File.WriteAllText(path, "{\"saved\":true}");
Debug.Log("Saved at: " + path);
}
Local File Reading & Parsing¶
Use case: Load and parse JSON from persistent storage.
public void Start()
{
var path = Application.persistentDataPath + "/test.json";
if (System.IO.File.Exists(path))
{
var content = System.IO.File.ReadAllText(path);
Debug.Log("Loaded: " + content);
}
else
{
Debug.Log("No file found at: " + path);
}
}
Simulate Failed Request¶
Use case: Test timeout or error states live.
public async void Start()
{
var client = new System.Net.Http.HttpClient();
client.Timeout = System.TimeSpan.FromMilliseconds(200);
try
{
await client.GetStringAsync("https://httpstat.us/200?sleep=500");
}
catch (System.Exception ex)
{
Debug.Log("Timeout caught: " + ex.Message);
}
}
Manual Socket Send/Receive (TCP)¶
Use case: Connect to a TCP server and exchange data.
public async void Start()
{
using var client = new System.Net.Sockets.TcpClient("127.0.0.1", 8080);
var stream = client.GetStream();
var data = System.Text.Encoding.UTF8.GetBytes("Hello Server");
await stream.WriteAsync(data, 0, data.Length);
Debug.Log("Message sent.");
}
Simulate Cloud Settings or Config Fetch¶
Use case: Fake cloud configuration with fallback to local.
public async void Start()
{
string fallback = "{\"theme\":\"dark\"}";
try
{
var client = new System.Net.Http.HttpClient();
var remote = await client.GetStringAsync("https://api.mygame.com/config");
Debug.Log("Cloud config: " + remote);
}
catch
{
Debug.Log("Using local config: " + fallback);
}
}
Polling or Delayed Sync¶
Use case: Pull updates from server periodically.
Easily simulate sync loops, cloud save polling, or match status checking.