What is a byte array?

Asked

Viewed 228 times

0

What is the byte array in Unity? I know we store data that can be converted into bytes.

1 answer

1


It is not exclusive to Unity, although it has some specific utilities, I could not even speak in Unity and yes in each case that Unity uses.

As a whole array, it is a sequence of data of a certain type, in which case it is of a byte simple. You can put whatever you want there. Virtually you can put any information in a byte[] because any data is a sequence of bytes. It can be a text, an executable, an image, a video, a sound, any pattern, something with a specific format, it doesn’t matter.

Some objects can be generated or stored in a generic way as a array of bytes. Example:

using UnityEngine;

public class ExampleScript : MonoBehaviour {
    public void Start() {
        // Create a 16x16 texture with PVRTC RGBA4 format
        // and fill it with raw PVRTC bytes.
        Texture2D tex = new Texture2D(16, 16, TextureFormat.PVRTC_RGBA4, false);
        // Raw PVRTC4 data for a 16x16 texture. This format is four bits
        // per pixel, so data should be 16*16/2=128 bytes in size.
        // Texture that is encoded here is mostly green with some angular
        // blue and red lines.
        byte[] pvrtcBytes = new byte[] {
            0x30, 0x32, 0x32, 0x32, 0xe7, 0x30, 0xaa, 0x7f, 0x32, 0x32, 0x32, 0x32, 0xf9, 0x40, 0xbc, 0x7f,
            0x03, 0x03, 0x03, 0x03, 0xf6, 0x30, 0x02, 0x05, 0x03, 0x03, 0x03, 0x03, 0xf4, 0x30, 0x03, 0x06,
            0x32, 0x32, 0x32, 0x32, 0xf7, 0x40, 0xaa, 0x7f, 0x32, 0xf2, 0x02, 0xa8, 0xe7, 0x30, 0xff, 0xff,
            0x03, 0x03, 0x03, 0xff, 0xe6, 0x40, 0x00, 0x0f, 0x00, 0xff, 0x00, 0xaa, 0xe9, 0x40, 0x9f, 0xff,
            0x5b, 0x03, 0x03, 0x03, 0xca, 0x6a, 0x0f, 0x30, 0x03, 0x03, 0x03, 0xff, 0xca, 0x68, 0x0f, 0x30,
            0xaa, 0x94, 0x90, 0x40, 0xba, 0x5b, 0xaf, 0x68, 0x40, 0x00, 0x00, 0xff, 0xca, 0x58, 0x0f, 0x20,
            0x00, 0x00, 0x00, 0xff, 0xe6, 0x40, 0x01, 0x2c, 0x00, 0xff, 0x00, 0xaa, 0xdb, 0x41, 0xff, 0xff,
            0x00, 0x00, 0x00, 0xff, 0xe8, 0x40, 0x01, 0x1c, 0x00, 0xff, 0x00, 0xaa, 0xbb, 0x40, 0xff, 0xff,
        };
        // Load data into the texture and upload it to the GPU.
        tex.LoadRawTextureData(pvrtcBytes);
        tex.Apply();
        // Assign texture to renderer's material.
        GetComponent<Renderer>().material.mainTexture = tex;
    }
}

I put in the Github for future reference.

Source.

This example mounts an image pattern that will create a texture object to use on in-game scene objects. These bytes end up forming a form of drawing somehow, they indicate where must have a certain shade of color to simulate a hypothetical surface. In a real code could go changing these numbers, which were written in hexadecimal notation, but are number like any other can go from 0 to 255, and after changing can see changes in the texture that is applied to the game. A texture is a sequence of bytes.

  • Thanks friend! But in case, any data placed in it ends up converting to its respective byte sequence?

  • Nothing is converted, at least without command. He is a sequence of bytes as a whole array It’s a sequence of something. What you can do is take this sequence and convert it to something with a special meaning like this code that converts it to a texture, or you can do the opposite somewhere and take a specific object and treat it like a byte sequence.

  • Thank you so much again! It helped a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.