【UnityEditor】创建并保存 Texture图片、Mesh网格

这篇博客介绍了如何在Unity中创建并保存Texture2D和Mesh。首先,通过设定像素颜色和应用更改来创建Texture2D,并将其编码为PNG文件保存。接着,详细说明了如何构建Mesh的顶点和三角面数据,然后将数据赋值给Mesh对象,并保存为asset文件。这两个过程都是Unity中常见的3D资源创建步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建保存Texture

  • new Texture2D(spriteSize,spriteSize):生成一张Texture2D纹理;
  • Texture2D.SetPixel(pixelX,pixelY,Color):按像素设置Texture2D纹理的色值;
  • Texture2D.Apply():应用任何先前的SetPixel和SetPixels更改;
  • Texture2D.EncodeToPNG():将Texture2D纹理转为二进制数据,用于保存到本地;
  • 使用FileStream读写文件流,将Texture2D的二进制数据保存为png。

 创建保存Texture:

    private void CreateSampleSprite()
    {
        int minRadius = 64;
        int maxRadius = 128;

        //图片尺寸
        int spriteSize = maxRadius * 2;
        //创建Texture2D
        Texture2D texture2D = new Texture2D(spriteSize,spriteSize);
        //图片中心像素点坐标
        Vector2 centerPixel = new Vector2(maxRadius,maxRadius);
        //遍历像素点
        Vector2 tempPixel;
        float tempDis;
        for(int x = 0; x < spriteSize; x++)
        {
            for(int y = 0; y < spriteSize; y++)
            {
                //以中心作为起点,获取像素点向量
                tempPixel.x = x - centerPixel.x;
                tempPixel.y = y - centerPixel.y;
                //是否在半径范围内
                tempDis = tempPixel.magnitude;
                if(tempDis >= minRadius && tempDis <= maxRadius)
                    texture2D.SetPixel(x,y,Color.red);
                else
                    texture2D.SetPixel(x,y,Color.white);
            }
        }
        texture2D.Apply();
        //保存图片
        byte[] dataBytes = texture2D.EncodeToPNG();
        string savePath = Application.dataPath + "/SampleCircle.png";
        FileStream fileStream = File.Open(savePath,FileMode.OpenOrCreate);
        fileStream.Write(dataBytes,0,dataBytes.Length);
        fileStream.Close();
        UnityEditor.AssetDatabase.SaveAssets();
        UnityEditor.AssetDatabase.Refresh();
    }

创建保存Mesh

  • 创建网格数据:顶点和三角面,赋值到Mesh;
  • AssetDatabase.CreateAsset(),将Mesh文件保存到本地,存为asset格式。

关于创建网格:

  • Vector3[] vertices:用于存放顶点数据,元素是三维坐标,固定顺序;
  • int[] triangles:用于存放三角面数据,元素是顶点下标(vertices中坐标的下标),固定顺序,每三个下标一组,逆时针方向围成一个三角面;

伪代码:
vertices = new Vector3[3];
vertices[0] = 坐标0;
vertices[1] = 坐标1;
vertices[2] = 坐标2;
triangles = new int[3];
triangles[0] = 0;
triangles[1] = 1;
triangles[2] = 3;

创建保存Mesh:

    private void CreateSampleMesh()
    {
        float halfWidth = 50;
        float halfHeight = 50;

        Vector3[] vertices = new Vector3[4];
        vertices[0] = new Vector3(-halfWidth,0,-halfHeight);
        vertices[1] = new Vector3(-halfWidth,0,halfHeight);
        vertices[2] = new Vector3(halfWidth,0,halfHeight);
        vertices[3] = new Vector3(halfWidth,0,-halfHeight);

        int[] triangles = new int[6];
        triangles[0] = 0;
        triangles[1] = 1;
        triangles[2] = 3;
        triangles[3] = 3;
        triangles[4] = 1;
        triangles[5] = 2;

        Mesh newMesh = new Mesh();
        newMesh.vertices = vertices;
        newMesh.triangles = triangles;
        newMesh.name = "SampleMesh";

        string savePath = "Assets/SampleMesh.asset";
        UnityEditor.AssetDatabase.CreateAsset(newMesh,savePath);
        UnityEditor.AssetDatabase.SaveAssets();
        UnityEditor.AssetDatabase.Refresh();
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧然CS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值