untiy-新建Texture,取消引用

今天做项目时遇见一个问题,就是texture在内存中,无论怎么新建都指向同一个内存地址,只能新建一个texture来中断它,但是发现无论怎么新建  都无济于事,外网找了一圈有所收获,特意分享出来

 

    public void SaveScreenShot()
    {
        Texture2D tex = new Texture2D(1920,1080,TextureFormat.RGBA32,false);
        Texture2D texture2D = TextureToTexture2D(transform.Find("paintCanvasImg").GetComponent<RawImage>().texture);
        tex.SetPixels(texture2D.GetPixels());
        tex.Apply();
        UIController.instance.autograph.texture = tex;
    }


    private Texture2D TextureToTexture2D(Texture texture)
    {
        Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
        RenderTexture currentRT = RenderTexture.active;
        RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
        Graphics.Blit(texture, renderTexture);

        RenderTexture.active = renderTexture;
        texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
        texture2D.Apply();

        RenderTexture.active = currentRT;
        RenderTexture.ReleaseTemporary(renderTexture);

        return texture2D;
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值