Unity 动态修改图集内容

这个代码示例展示了在Unity中如何修改图集并填补指定区域,以及如何进行纹理压缩。通过`AmendAtlas`函数,可以更新图集中的指定部分,并用新的纹理数据填充。同时,`ReSetTextureSize`函数用于调整纹理尺寸。最后,`writeCaptureDataToFile`将处理后的纹理保存为JPEG文件。这是一个关于Unity图形处理和资源管理的实例。
摘要由CSDN通过智能技术生成

先上代码

using System.IO;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System;

public class Test3 : MonoBehaviour
{   

    public RawImage image;
    public Texture2D te;
    public Texture2D texture;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            image.texture = AmendAtlas(new UnityEngine.Vector2(0,1536),new UnityEngine.Vector2(512,512),te,texture);
        }
    }
    /// <summary>
    /// 修改图集
    /// </summary>
    public Texture2D AmendAtlas(UnityEngine.Vector2 _修改图片的起始点, UnityEngine.Vector2 _修改图集的范围,Texture2D _图集,Texture2D _要添加的图片)
    {
        Color[] colors = _图集.GetPixels();
        Texture2D texture2D = new Texture2D(_图集.width, _图集.height);
        texture2D.SetPixels(colors);
        texture2D.SetPixels((int)_修改图片的起始点.x, (int)_修改图片的起始点.y, (int)_修改图集的范围.x, (int)_修改图集的范围.y, _要添加的图片.GetPixels());
        texture2D.Apply();

        return texture2D;
    }
   
    /// <summary>
    /// 图片压缩
    /// </summary>
    /// <param name="tex"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    public Texture2D ReSetTextureSize(Texture2D tex, int width, int height)
    {
        var rendTex = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
        rendTex.Create();
        Graphics.SetRenderTarget(rendTex);
        GL.PushMatrix();
        GL.Clear(true, true, Color.clear);
        GL.PopMatrix();
        var mat = new Material(Shader.Find("Unlit/Transparent"));
        mat.mainTexture = tex;
        Graphics.SetRenderTarget(rendTex);
        GL.PushMatrix();
        GL.LoadOrtho();
        mat.SetPass(0);
        GL.Begin(GL.QUADS);
        GL.TexCoord2(0, 0);
        GL.Vertex3(0, 0, 0);
        GL.TexCoord2(0, 1);
        GL.Vertex3(0, 1, 0);
        GL.TexCoord2(1, 1);
        GL.Vertex3(1, 1, 0);
        GL.TexCoord2(1, 0);
        GL.Vertex3(1, 0, 0);
        GL.End();
        GL.PopMatrix();
        var finalTex = new Texture2D(rendTex.width, rendTex.height, TextureFormat.ARGB32, false);
        RenderTexture.active = rendTex;
        finalTex.ReadPixels(new Rect(0, 0, finalTex.width, finalTex.height), 0, 0);
        finalTex.Apply();
        return finalTex;
    }
   

    public void writeCaptureDataToFile(Texture2D texture, string dataPath, string filename)
    {
        string path_full =Path.Combine(dataPath,filename+".jpg");
 
        // 存入jpg文件
        StartCoroutine(saveTexture2DtoFile(texture, path_full));
    }
 
    // 保存图片到指定目录
    private IEnumerator saveTexture2DtoFile(Texture2D texture, string path, Action<object> callback=null)
    {
        //等待渲染线程结束  
        yield return new WaitForEndOfFrame();
 
        byte[] textureData = texture.EncodeToJPG();
        System.IO.File.WriteAllBytes(path, textureData);
 
        callback?.Invoke(null);
        Debug.Log("图片文件写入完毕:" + path);
    }
}

在这里插入图片描述
这图集大小为 1024 * 2048
修改红色框住的部分 并填补
该区域大小为 512 * 512
起始点为该区域的左下角 位置为 (0,1536)

填入数据
然后运行代码
得到下面的结果

在这里插入图片描述

Over

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值