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

创建保存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();
    }

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Unity中绘制矩形并保存图片形状,我们可以通过以下步骤实现: 1. 创建一个空白的2D场景,并在场景中创建一个Canvas对象。 2. 在Canvas下创建一个RawImage对象,用于显示矩形。 3. 在脚本代码中,使用Texture2D类创建一个空白的纹理对象,并设置其宽度和高度,这里我们可以设置为矩形的尺寸。 4. 使用Graphics类的DrawTexture方法,在纹理上绘制矩形。可以使用Rect结构体来定义矩形的位置和大小。 5. 使用纹理的EncodeToPNG方法将纹理编码为PNG格式的字节数组。 6. 将字节数组保存为本地图片文件,可以使用File.WriteAllBytes方法。 7. 将保存图片路径赋值给RawImage的texture属性,以在游戏中显示保存的矩形图片。 具体的代码实现如下: ```csharp using UnityEngine; using UnityEngine.UI; using System; using System.IO; public class DrawRectAndSaveImage : MonoBehaviour { public int width = 200; // 矩形的宽度 public int height = 100; // 矩形的高度 public string savePath = "SavedImages/Rectangle.png"; // 保存图片路径 private RawImage rawImage; void Start() { rawImage = GetComponent<RawImage>(); // 创建一个空白的纹理对象 Texture2D texture = new Texture2D(width, height); // 在纹理上绘制矩形 Rect rect = new Rect(0, 0, width, height); for (int y = (int)rect.yMin; y < rect.yMax; y++) { for (int x = (int)rect.xMin; x < rect.xMax; x++) { texture.SetPixel(x, y, Color.red); } } // 保存纹理为PNG图片 byte[] bytes = texture.EncodeToPNG(); string fullPath = Path.Combine(Application.dataPath, savePath); File.WriteAllBytes(fullPath, bytes); // 在游戏中显示保存图片 rawImage.texture = texture; } } ``` 以上就是使用Unity绘制矩形并保存图片形状的方法,首先在脚本中绘制矩形并保存图片,然后在游戏中显示保存图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

萧然CS

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

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

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

打赏作者

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

抵扣说明:

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

余额充值