⭐ 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 小时前
Unity 局域网遥控幻灯片展示工具 3.0
科技·http·unity·工具·ppt·通信·网页
itmigrate15 小时前
异步请求竞态,藏在页面渲染里的隐形bug
bug
今夕资源网2 天前
OmniVoice 今夕整合包 本地语音TTS断网可用 修复了一些BUG 优化生成速度 极速秒生成
bug·语音克隆·omnivoice·断网可用·本地语音tts·语音tts
WarPigs2 天前
Unity对象不应该使用null传播的问题
unity
每日出拳老爷子3 天前
【VR】【Unity】抓走转放系列21|一天搭完门和抽屉工具槽:小场景按这个次序拼装
unity·vr·驱动·排错·snap zone
淡海水4 天前
02-02-原理篇-分代GC
算法·unity·c#·游戏引擎·.net·gc
小贺儿开发4 天前
Unity 文物新生 会讲故事的文物展墙
人工智能·科技·unity·ai·视频·互动·演示
淡海水4 天前
02-03-原理篇-大对象堆
unity·c#·游戏引擎·.net·gc
WarPigs5 天前
Unity在Create菜单添加创建自定义脚本模板选项
unity
郝学胜-神的一滴5 天前
C++ Templates 01:拨开迷雾,读懂C++模板经典著作
开发语言·c++·程序人生·游戏·游戏引擎·软件构建