Search Unity

[SOLVED] Issue with shader on android mobile

Discussion in 'Shaders' started by Pau1, Jan 13, 2017.

  1. Pau1

    Pau1

    Joined:
    Mar 21, 2016
    Posts:
    7
    I have a custom shader for an effect in game, and it's been working fine and as intended in the editor and on standalone builds on mac, however when I build and run on my android device (Nexus 5) I don't see the effect at all.

    I've narrowed down the problem to this line which I've simplified further to this: o.col = fixed4(1, 1, 1, 1) * _Brightness (o is v2f, col is fixed4 col : COLOR; in v2f).

    If I change the line to o.col = fixed4(1, 1, 1, 1) * 1 it works fine (but I no longer have a property I can change obviously).

    The property is defined
    _Brightness ("Brightness", Range(0, 1)) = 1 and has the uniform float _Brightness; line too.

    In code I have
    Shader.SetGlobalFloat("_Brightness", 1f); but still I don't see anything (I guess brightness is actually 0 when it runs, even though I'm setting it to 1 and the default should be 1 anyway)

    Any idea what's going on here? Unity 5.3.5f1
     
    Last edited: Jan 13, 2017
  2. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    I think you need to remove the property definition if you are going to set it as a global.
     
  3. Pau1

    Pau1

    Joined:
    Mar 21, 2016
    Posts:
    7
    Thanks for the reply, I've commented out all of the property definitions in the shader just to be sure and still seeing the same (working in editor mode, and not on android). I had them as properties as I thought I had to to expose them to the code, so I've learnt something new.

    The reason I'm setting global is basically because it's the only way I know how to change a value of a shader from code. I'm setting this shader with a custom effect as a replacement shader on a second camera that only sees one layer in it's culling mask. For the effect I change the objects to that layer, and animate the shader values for the effect.
     
  4. Pau1

    Pau1

    Joined:
    Mar 21, 2016
    Posts:
    7
    It's very unlikely someone will run into the same issue and be searching for the same thing but I resolved this in the end. I spent a bit of time on it and gave up moving onto other parts of the game. I just stumbled upon the issue just now in a completely different part of the project and realised what the issue was.

    Basically the issue was I had some initialisation code in OnValidate because I wanted to run it again if I tweaked the values in the editor, I didn't realise this method doesn't seem to be called at all on builds. Simply moving the initialisation code into a Setup method and calling it from both OnValidate and Awake methods solved the issue.