⭐ 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拖入其中

相关推荐
小贺儿开发2 小时前
Unity3D 八大菜系连连看
游戏·unity·互动·传统文化
在路上看风景2 小时前
25. 屏幕像素和纹理像素不匹配
unity
王家视频教程图书馆3 小时前
修复服务端500相应,修复客户端上传文件.tmp 服务端接受不到文件bug
bug
ۓ明哲ڪ4 小时前
Unity功能——创建新脚本时自动添加自定义头注释
unity·游戏引擎
熬夜敲代码的小N4 小时前
Unity大场景卡顿“急救包”:从诊断到落地的全栈优化方案
java·unity·游戏引擎
qq_401700414 小时前
Qt开发过程中遇到哪些经典的bug
qt·bug
派葛穆6 小时前
Unity-realvirtual-S7通讯快速配置(未完结)
unity·游戏引擎
w-白兰地16 小时前
【Addressable远端加载资源】
unity·addressable·资源加载
小张不爱写代码19 小时前
[Unity 技巧] 如何自定义 Inspector 变量显示名称 (CustomLabel)
unity·游戏引擎
Sator120 小时前
Unity开发中常用的随机方法扩展
unity