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

相关推荐
Wanda121430 分钟前
机器人与机电一体化3D数字孪生机器-Day4
3d·unity·机器人·数字孪生
kyle~34 分钟前
机器人通信 --- FANUC TCP 并发请求-响应错位 Bug 复盘
tcp/ip·机器人·bug
年少无知且疯狂20 小时前
【Unity】Shader spine动画冻结效果
unity·spine
淡海水21 小时前
05-02-源码-Boehm GC标记阶段
算法·unity·c#·游戏引擎·gc
淡海水1 天前
05-03-源码-Boehm GC分配与碎片管理
开发语言·算法·unity·c#·游戏引擎·gc
鬼手点金2 天前
Harness 工程:模型之外的竞争力
服务器·人工智能·深度学习·机器学习·bug
WarPigs3 天前
Unity无法修改Assembly-CSharp.csproj错误
unity
巴巴媛6663 天前
【Debug1】程序死机,while(1)循环,怎么办?
c语言·笔记·stm32·嵌入式硬件·bug
FairGuard手游加固4 天前
手游加固怎么做?SO、IL2CPP、AssetBundle 与运行时保护详解
安全·游戏·unity
阿松爱学习5 天前
【Unity开发】监听Scene场景事件
unity·c#