⭐ Unity development bug - shader failure or bug after packaging (I used Shader to merge two images and found the problem)

1. There’s nothing wrong with my code here~~~ There’s nothing wrong with the editor either

 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. But after packaging, the pictures that were to be merged did not respond.

3. The solution is as follows:

Select Edit->ProjectSettings->Graphics:

In Graphics in ProjectSettings, change the size of the Always Included Shaders array and drag the required Shader into it.

Guess you like

Origin blog.csdn.net/weixin_53501436/article/details/134648479