how to use a script to import terrain .raw?

I want players to be able to add the game the maps . I want that added .raw files . (via script)

So you want to load a terrain heightmap at runtime from a file? “.raw” is not a specific format. It simply contains raw data. A quite common format for a height map is 16 grayscale, however in theory it could contains anything.

There is no built-in method to load raw terrain data. You have to load the file yourself and convert it into a float array that can be used with TerrainData.SetHeights.

If you want to support the same options Unity has for the terrain heightmap importer, you have to provide a similar GUI with options for bit depth and resolution. Without those you can’t load the data. raw files don’t have any header. It literally only contains the raw data. So when reading the file you either only support one specific format (for example 513 x 513 16bit) or you have to provide options for the user to choose from.

Here’s an example how to read a 16 bit raw image with “windows” format (little endian):

void LoadTerrain(string aFileName, TerrainData aTerrain)
{
    int h = aTerrain.heightmapHeight;
    int w = aTerrain.heightmapWidth;
    float[,] data = new float[h, w];
    using (var file = System.IO.File.OpenRead(aFileName))
    using (var reader = new System.IO.BinaryReader(file))
    {
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                float v = (float)reader.ReadUInt16() / 0xFFFF;
                data[y, x] = v;
            }
        }
    }
    aTerrain.SetHeights(0, 0, data);
}

There are no error checks. So if the file doesn’t contain enough data it will fail with an exception. If the resolution doesn’t match the terrain size the image won’t appear right.