⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)

1.这里我代码没啥问题~~~编辑器里也没毛病

 void Start()
    {
        // 加载底图和上层图片
        string backgroundImagePath = Application.streamingAssetsPath + "/background.jpg";
        Texture2D backgroundTexture = new Texture2D(2, 2);
        byte[] backgroundImageData = System.IO.File.ReadAllBytes(backgroundImagePath);
        backgroundTexture.LoadImage(backgroundImageData);

        string overlayImagePath = Application.streamingAssetsPath + "/1.png";
        Texture2D overlayTexture = new Texture2D(2, 2);
        byte[] overlayImageData = System.IO.File.ReadAllBytes(overlayImagePath);
        overlayTexture.LoadImage(overlayImageData);


        // 创建合成材质
        Material imageProcessingMaterial = new Material(Shader.Find("Custom/ImageProcessingShader"));
        imageProcessingMaterial.SetTexture("_MainTex", backgroundTexture);
        imageProcessingMaterial.SetTexture("_OverlayTex", overlayTexture);

        // 创建RenderTexture,并渲染到其中
        RenderTexture renderTexture = new RenderTexture(backgroundTexture.width, backgroundTexture.height, 0);
        Graphics.Blit(null, renderTexture, imageProcessingMaterial);

        // 将RenderTexture转换为Texture2D
        Texture2D outputTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGBA32, false);
        RenderTexture.active = renderTexture;
        outputTexture.ReadPixels(new UnityEngine.Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
        outputTexture.Apply();
        RenderTexture.active = null;

        // 显示处理后的图像
        raw.texture = outputTexture;
    }

2.但是打包完之后,原本要合并的图片却没有反应

3.解决方法如下:

选择Edit->ProjectSettings->Graphics:

在ProjectSettings里面的Graphics,Always Included Shaders数组更改size,并将所需用到的Shader拖入其中

  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值