greyBow
greyBow

Reputation: 1348

Unity Texture2D.Resize() yielding empty texture

I'm trying to resize (scale down) a texture2d before compressing it and sending over the network but the resulting texture2d is empty (it just loads as solid gray). am I using tex.Resize(800, 600, TextureFormat.RGB24, true); incorrectly?

[Client]
    public void PrepareServerData(Texture2D texToSend, string typeToSend)
    {
        StartCoroutine(DoResizeTex(texToSend));
        StartCoroutine(DoGetTexToBytes(texToSend));

        playerObj.texWidth = texToSend.width;
        playerObj.texHeight = texToSend.height;
        playerObj.texFormat = texToSend.format;
        playerObj.tranX = tran.x;
        playerObj.tranY = tran.y;
        playerObj.tranZ = tran.z;
        playerObj.type = typeToSend;
        Player_ID id = GetComponent<Player_ID>();
        playerObj.id = id.MakeUniqueIdentity();
        playerObj.strength = strengthToSend;
        playerObj.hitpoints = hitPointsToSend;

        Network_Serializer serialize = GetComponent<Network_Serializer>();
        byte[] bytes = serialize.ObjectToByteArray(playerObj);

        StartCoroutine(Network_Transmitter.instance.DoSendBytes(0, bytes));
    }

    IEnumerator DoGetTexToBytes(Texture2D tex)
    {
        byte[] texBytes = tex.GetRawTextureData();                      // convert texture to raw bytes
        byte[] compressedTexBytes = lzip.compressBuffer(texBytes, 9);   // compress texture byte array
        playerObj.texBytes = compressedTexBytes;                        // set compressed bytes to player object

        yield return new WaitForEndOfFrame();

        GameObject infoDisplayText = GameObject.Find("InfoDisplay");
        infoDisplayText.GetComponent<Text>().text += "Bytes to send : " + playerObj.texBytes.Length + "\n";
    }

    IEnumerator DoResizeTex(Texture2D tex)
    {
        tex.Resize(800, 600, TextureFormat.RGB24, true);
        tex.Apply();

        yield return new WaitForEndOfFrame();
    }

Upvotes: 1

Views: 6337

Answers (2)

Memphis
Memphis

Reputation: 327

As mentioned, that only resizes the container. To scale the texture you can use Graphics.ConvertTexture(source, destination), where resolution of destination is your target resolution.

For example:

IEnumerator DoScaleTex(Texture2D tex)
{
    Texture2D scaled = new Texture2D(800, 600, TextureFormat.RGB24, true);
    Graphics.ConvertTexture(tex, scaled);
    tex = scaled;
    yield return new WaitForEndOfFrame();
}

Upvotes: 1

Oliver Wuensch
Oliver Wuensch

Reputation: 31

I am currently also digging through texture resize in Unity and Texture.Resize I found only resizes the container (the array of data) of the texture, it does not use content but has to be filled-- in my opinion it suggests something else, just like you expected. Looks like texture resizing is not a feature implemented in C# in Unity and has to be done per pixel by iterating and processing data.

Not comfortable and unfortunately very slow. There is an asset available that fills the gap (Texture Adjustments).

Upvotes: 3

Related Questions