Skip to main content

Interface IRequest

Minimal contract for a single outbound data operation — write to disk, POST to server, or simulate locally — executed as a Unity coroutine.

public interface IRequest

Examples​

Implementing a minimal file-write transport that conforms to the coroutine contract. The synchronous File.WriteAllText call is wrapped in a coroutine so that DataSender can yield on it without needing a special code path for synchronous transports. The single yield return null ensures Unity continues rendering for at least one frame while the write is in progress.

public class DebugFileRequest : IRequest
{
private readonly string _path;
private readonly string _json;

public DebugFileRequest(string path, string json)
{
_path = path;
_json = json;
}

// HandleRequest is started as a coroutine by DataSender.SendDataCoroutine.
// Yield once after the write so the call site never has to special-case
// synchronous vs. asynchronous transports.
public IEnumerator HandleRequest()
{
System.IO.File.WriteAllText(_path, _json);
yield return null;
}
}

Remarks​

The interface decouples DataSender from any specific transport so that the send method can be swapped via project settings without touching the call site. A WebGL deployment posts to a PHP backend; a local development session writes files to disk; a standalone build may use a TCP stream. Each variant implements HandleRequest differently but is interchangeable from the sender's perspective.

Coroutine execution is intentional: HTTP requests using UnityWebRequest are inherently asynchronous and must yield across frames. Making the interface coroutine-based lets every transport, including synchronous file writes, conform to the same single-frame or multi-frame execution model without additional threading.

Methods​

HandleRequest()​

Carries out the data operation (network POST, file write, etc.) and yields until complete.

IEnumerator HandleRequest()

Returns​

IEnumerator

Remarks​

Implementations should yield at least once so that Unity can continue rendering while the operation is in progress. For synchronous operations such as local file writes, a single yield return null suffices. For UnityWebRequest operations, yield on the send operation directly.