# DownloadHandlerScript

> An abstract base class for custom, scriptable DownloadHandler implementations.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine.Networking](/engine/6000.7/script-reference/unityengine/networking.md)
* **Assembly:** UnityEngine.UnityWebRequestModule
* **Inherits from:** [DownloadHandler](/engine/6000.7/script-reference/unityengine/networking/downloadhandler.md)
* **Implements:** [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public class DownloadHandlerScript : DownloadHandler, IDisposable
```

## Remarks

Derive from `DownloadHandlerScript` and override some or all of its callbacks to implement custom data handling for key events in the download process.

For example, override [DownloadHandler.ReceiveData](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/receivedata.md) to implement custom handling when data arrives from the remote server and override [DownloadHandler.ReceiveContentLengthHeader](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/receivecontentlengthheader.md) to implement custom handling when a Content-Length header is received.

The `UnityWebRequest` system permits the pre-allocation of a managed-code byte array, which is used to deliver downloaded data to the [DownloadHandler.ReceiveData](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/receivedata.md) callback. Using this function eliminates managed-code memory allocation when using classes derived from `DownloadHandlerScript`-to capture downloaded data.

To make a `DownloadHandlerScript` operate with a pre-allocated managed buffer, supply a byte array to the [DownloadHandlerScript](/engine/6000.7/script-reference/unityengine/networking/downloadhandlerscript/ctor.md). The size of the byte array limits the amount of data that can be received in each `ReceiveData` call. If data arrives slowly, over many frames, the supplied byte array might be too small.

**Note**: The actual downloads occur on a worker thread, but all `DownloadHandlerScript` callbacks operate on the main thread. Avoid performing computationally heavy operations from these callbacks.

Additional Resources: [DownloadHandler.ReceiveData](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/receivedata.md), [DownloadHandler.ReceiveContentLengthHeader](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/receivecontentlengthheader.md), [DownloadHandler.CompleteContent()](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/completecontent.md)

## Examples

```csharp
using UnityEngine;
using UnityEngine.Networking;

public class LoggingDownloadHandler : DownloadHandlerScript {

    // Standard scripted download handler - allocates memory on each ReceiveData callback

    public LoggingDownloadHandler(): base() {
    }

    // Pre-allocated scripted download handler
    // reuses the supplied byte array to deliver data.
    // Eliminates memory allocation.

    public LoggingDownloadHandler(byte[] buffer): base(buffer) {
    }

    // Required by DownloadHandler base class. Called when you address the 'bytes' property.

    protected override byte[] GetData() { return null; }

    // Called once per frame when data has been received from the network.

    protected override bool ReceiveData(byte[] data, int dataLength) {
        if(data == null) {
            Debug.Log("LoggingDownloadHandler :: ReceiveData - received a null/empty buffer");
            return false;
        }

        Debug.Log(string.Format("LoggingDownloadHandler :: ReceiveData - received {0} bytes", dataLength));
        return true;
    }

    // Called when all data has been received from the server and delivered via ReceiveData.

    protected override void CompleteContent() {
        Debug.Log("LoggingDownloadHandler :: CompleteContent - DOWNLOAD COMPLETE!");
    }

    // Called when a Content-Length header is received from the server.

    protected override void ReceiveContentLengthHeader(ulong contentLength) {
        Debug.Log(string.Format("LoggingDownloadHandler :: ReceiveContentLength - length {0}", contentLength));
    }
}
```

## Constructors

| Constructor                                                                                                                                                  | Description                                                                                  |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------- |
| [DownloadHandlerScript()](/engine/6000.7/script-reference/unityengine/networking/downloadhandlerscript/ctor.md#downloadhandlerscript\(\))                    | Create a DownloadHandlerScript which allocates new buffers when passing data to callbacks.   |
| [DownloadHandlerScript(System.Byte\[\])](/engine/6000.7/script-reference/unityengine/networking/downloadhandlerscript/ctor.md#downloadhandlerscript\(byte\)) | Create a DownloadHandlerScript which reuses a preallocated buffer to pass data to callbacks. |

## Inheritance

**Inherited Members:**

* [DownloadHandler.Dispose()](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/dispose.md)
* [DownloadHandler.GetNativeData()](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/getnativedata.md)
* [DownloadHandler.GetData()](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/getdata.md)
* [DownloadHandler.GetText()](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/gettext.md)
* [DownloadHandler.ReceiveData(byte\[\], int)](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/receivedata.md)
* [DownloadHandler.ReceiveContentLengthHeader(ulong)](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/receivecontentlengthheader.md)
* [DownloadHandler.CompleteContent()](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/completecontent.md)
* [DownloadHandler.GetProgress()](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/getprogress.md)
* [DownloadHandler.isDone](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/isdone.md)
* [DownloadHandler.error](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/error.md)
* [DownloadHandler.nativeData](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/nativedata.md)
* [DownloadHandler.data](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/data.md)
* [DownloadHandler.text](/engine/6000.7/script-reference/unityengine/networking/downloadhandler/text.md)
