Skip to content

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 via Update(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);
}
Great for checking if your API is online, testing response formats, or debugging header issues.

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);
}
Useful when testing backend endpoints or validating serialization output.

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}");
}
Great for prototyping UI or validating remote data formats without writing mock handlers.

Inspect Serialized Object State

Use case: Serialize a live component’s data and preview its JSON output.

public void Start()
{
    var json = JsonUtility.ToJson(target, true);
    Debug.Log(json);
}
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);
}
Excellent for testing save systems, caching, and replay logic.

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);
    }
}
No need to leave Play Mode or hook into your regular save/load logic.

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);
    }
}
Great for debugging edge cases like latency, 404s, or error flows.

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.");
}
Test socket availability, handshake logic, or simulate clients.

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);
    }
}
Perfect for hybrid live/local workflows or live A/B testing setup.

Polling or Delayed Sync

Use case: Pull updates from server periodically.

private float timer;

public async void Update(float dt)
{
    timer += dt;
    if (timer > 5f)
    {
        timer = 0f;
        var result = await new System.Net.Http.HttpClient().GetStringAsync("https://your-api.com/status");
        Debug.Log("Polled: " + result);
    }
}
Easily simulate sync loops, cloud save polling, or match status checking.