Unity中动态生成贴图并保存成png图片实现

实现原理:

要生成长x宽y的贴图,就是生成x*y个像素填充到贴图中,如下图:

如果要改变局部颜色,就是从x1到x2(x1<x2),y1到y2(y1<y2)这个范围做处理,

或者要想做圆形就是计算距某个点(x1,y1)在一定长度d的范围内做处理

代码:

cs 复制代码
    Vector3 point;
    float d;
    Texture2D t = new Texture2D(width,height);
    Color[] pixelColors = new Color[width * height];
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            if(Mathf.Abs(i-width/2 - point.x)<d && Mathf.Abs(j-height/2 - point.z)<d){
             pixelColors[i * this.height + j] = new Color(0,0,0,0);
            }else{
             pixelColors[i * height + j] = Color.black;
            }
        }
    }

    t.SetPixels(pixelColors);
    t.Apply();
    byte[] b = t.EncodeToPNG();
    System.IO.File.WriteAllBytes(Application.dataPath+"/t.png",b);

最终就会在文件夹中保存了图片

相关推荐
可别39013 小时前
cesium实现网格化
游戏引擎·cocos2d
游乐码1 天前
Unity基础(十二)资源异步加载
unity·游戏引擎
weixin_424294671 天前
程序不知道写在了什么位置???
unity
weixin_441940011 天前
vuforia ar unity实验教程
unity·游戏引擎·ar
妙为1 天前
unreal engine5(UE5)中使用Rider
ue5·游戏引擎·虚幻·rider
WarPigs1 天前
Unity AB包资源加载管理器
unity
程序员正茂1 天前
EasyAR使用OpenCV下USB摄像头作为自定义相机
opencv·unity·easyar
诙_1 天前
unity——C#
unity·c#·游戏引擎
晓13131 天前
【Cocos Creator 3.x】篇——第一章 简介
前端·javascript·游戏引擎
GTA村长团队MOD1 天前
村长团队GTA5模组开发Blender 4.2 + Sollumz 多张贴图烘焙成单张贴图教程
人工智能·blender·贴图