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

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

cs 复制代码
 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拖入其中

相关推荐
·云扬·1 小时前
【Bug】 Elasticsearch启动失败(exit code 78):2个bootstrap检查问题排查与解决
elasticsearch·bootstrap·bug
在路上看风景13 小时前
15. 纹理尺寸是4的倍数
unity
星依网络15 小时前
使用LabelImg工具标注数据(游戏辅助脚本开发)
python·游戏引擎·图形渲染·骨骼绑定
AT~16 小时前
unity 使用Socket和protobuf实现网络连接
unity·游戏引擎
BrianGriffin21 小时前
get_download_file_path: command not found (asdf bug已解决)
bug
feiduoge21 小时前
教程 36 - 方向光照
windows·游戏引擎·图形渲染
咸虾米_21 小时前
解决九两酒商城[uni-pay-co]: Error:执行失败,积 分需要大于等于1的bug
bug·unicloud云开发·微信小程序商城·uniapp项目
凯子坚持 c1 天前
Git分支实战指南:如何优雅地管理版本、修复Bug并解决合并冲突
git·bug
怣疯knight1 天前
Cocos creator判断节点是否能用的方法
unity·cocos2d
tealcwu1 天前
Google Play的Keystore不可用时的解决方法
unity