Why this shader works in editor but not when I build the game?

Hi there, Please look at this shader. It’s a camera effect (Chromatic)
it works in editor but when I build the game It doesn’t applied
Why is that?

Shader

//--------------------------------------------------------------//
// Chromatic Abberation
//
// Copyright (c) Johannes Bausch. All rights reserved.
//
// Edited by that one guy who is realllyyy good at shaders, yeahh that guy. Optimized the.. wait why am I explaining I should be writing shader code now. Alright meet you at line 109.
//--------------------------------------------------------------//
Shader "Custom/AdvancedCA"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Pass
        {
       
        CGPROGRAM
        #pragma vertex vert_img
        #pragma fragment frag
        #include "UnityCG.cginc"
        #pragma glsl
        #pragma target 3.0
        #pragma multi_compile REDBLUE REDGREEN
//--------------------------------------------------------------//
// Params
//--------------------------------------------------------------//
        uniform sampler2D _MainTex;
        uniform float _Amount;
//--------------------------------------------------------------//
// Dangerous Shader that melts GPU's
//--------------------------------------------------------------//
        float4 frag(v2f_img i) : COLOR
        {
            #ifdef REDGREEN
            float2 coord = i.uv - float2(.5, .5);
            float multiplier = length(coord), strength;
            float2 color;
       
            const float STRENGTH = _Amount / 50;
            const int STEPS = 12;
       
            float4 fragColor = float4(0.0, 0.0, 0.0, 1.0);
       
            for (int i = 0; i < STEPS; i ++) {
                strength = 1.0 - i*STRENGTH * multiplier;
                color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).rg/STEPS;
                color.r *= (1.0 - (float)i/STEPS);
                color.g *= (float)i/STEPS;
                fragColor.rg += color.rg;
            }
            for (int i = STEPS; i < 2*STEPS; i ++) {
                strength = 1.0 - i*STRENGTH * multiplier;
                color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).gb/STEPS;
                color.r *= (1.0 - (float)(i-STEPS)/STEPS);
                color.g *= (float)(i-STEPS)/STEPS;
                fragColor.gb += color.rg;
            }
            for (int i = 2*STEPS; i < 3*STEPS; i ++) {
                strength = 1.0 - i*STRENGTH * multiplier;
                color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).br/STEPS;
                color.r *= (1.0 - (float)(i-2*STEPS)/STEPS);
                color.g *= (float)(i-2*STEPS)/STEPS;
                fragColor.br += color.rg;
            }
       
            return fragColor;
            #endif
           
            #ifdef REDBLUE
            float2 coord = i.uv - float2(.5, .5);
            float multiplier = length(coord), strength;
            float2 color;
       
            const float STRENGTH = _Amount / 100;
            const int STEPS = 12;
       
            float4 fragColor = float4(0.0, 0.0, 0.0, 1.0);
       
           
            for (int i = 0; i < STEPS; i ++) {
                strength = 1.0 - i*STRENGTH * multiplier;
                color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).rg/STEPS;
                color.r *= (1.0 - (float)i/STEPS);
                color.g *= (float)i/STEPS;
                fragColor.rg += color.rg;
            }
           
            for (int i = STEPS; i <= 2*STEPS; i ++) {
                strength = 1.0 - i*STRENGTH * multiplier;
                color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).gb/STEPS;
                color.r *= (1.0 - (float)(i-STEPS)/STEPS);
                color.g *= (float)(i-STEPS)/STEPS;
                fragColor.gb += color.rg;
            }
       
            // half cycling correction
            fragColor.rb *= (float)STEPS/(1-.5*(STEPS+1)+STEPS);
       
            return fragColor;
            #endif
       
        }

        ENDCG
        }
    }
}
//Ummm yeahh, enjoy!

C#

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class AdvancedCA : MonoBehaviour {
    #region Variables
    private Shader curShader;
    public float DispersionAmount = 1.0f;
    public enum ColorSet { RedBlue = 0 , RedGreen = 1 };
    public ColorSet Colors;
    private Material curMaterial;
    #endregion
   
    #region Properties
    Material material
    {
        get
        {
            if(curMaterial == null)
            {
                curMaterial = new Material(curShader);
                curMaterial.hideFlags = HideFlags.HideAndDontSave;   
            }
            return curMaterial;
        }
    }
    #endregion
    // Use this for initialization
    void Start ()
    {
        if(!SystemInfo.supportsImageEffects)
        {
            enabled = false;
            return;
        }
       
        //Find
        curShader = Shader.Find("Custom/AdvancedCA");
       
    }
   
    void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
    {
        if(curShader != null)
        {
            material.SetFloat("_Amount", DispersionAmount);
            Graphics.Blit(sourceTexture, destTexture, material);
        }
        else
        {
            Graphics.Blit(sourceTexture, destTexture);   
        }
       
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        if(ColorSet.RedBlue == Colors)
        {
            Shader.EnableKeyword("REDBLUE");
            Shader.DisableKeyword("REDGREEN");
        }
        else if(ColorSet.RedGreen == Colors)
        {
            Shader.EnableKeyword("REDGREEN");
            Shader.DisableKeyword("REDBLUE");
        }
    }
   
    void OnDisable ()
    {
        if(curMaterial)
        {
            DestroyImmediate(curMaterial);   
        }
       
    }
   
   
}

Please help if you can thanks…

What does “It doesn’t applied” mean? Do you get any player log messages saying what’s happening? What’s your build platform?

1 Like

It works when I play game in editor but when I build the game it seems I never add this effect to camera! and there is no error … I’m using PC platform and unity PRO.

My game depends on that shader. please help thanks …

When Unity makes the build, how does it know that the shader is needed? Are there any links from objects or scripts to the shader. (Example, if you add a text asset to your project, but don’t use it at all, Unity will know it’s not needed in the build.) I can see you have script that called Shader.Find(). The docs say:

When building a player, a shader will only be included if it is assigned to a material that is used in any scene or if the shader is placed in a “Resources” folder.

3 Likes

That’s it, Thanks. solved, I put the shader on the “Resources” folder and solved.
Thanks

Amazing how good the docs are.

1 Like