Unity Texture2D.Resize()产生空纹理

时间:2017-11-05 08:08:29

标签: c# unity3d resize texture2d

我正在尝试调整(缩小)一个texture2d,然后压缩它并通过网络发送,但生成的texture2d为空(它只是加载为纯灰色)。我错误地使用tex.Resize(800, 600, TextureFormat.RGB24, true);吗?

[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();
    }

2 个答案:

答案 0 :(得分:1)

我目前也正在挖掘Unity和Texture.Resize中的纹理调整大小我发现只调整了纹理的容器(数据数组),它不使用内容但必须填充 - 在我看来它建议别的,就像你期望的那样。 看起来纹理大小调整不是Unity中C#中实现的功能,必须通过迭代和处理数据来按像素完成。

不舒服,不幸的是很慢。 有一个可用的资产填补了空白(纹理调整)。

答案 1 :(得分:0)

如上所述,这只会调整容器的大小。要缩放纹理,可以使用 const rightnow = () => new Date().toLocaleTimeString() <rect id="grafic" height={5} width={5}> <animate id="animation" attributeName="height" from={5} to={10} dur="2s" begin={start ? `wallclock(${rightnow()})` : "indefinite"/> </rect> ,其中Graphics.ConvertTexture(source, destination)的分辨率是您的目标分辨率。

例如:

destination