Search Unity

Shader.EnableKeyword();

Discussion in 'Shaders' started by IronHelmet, Jul 30, 2021.

  1. IronHelmet

    IronHelmet

    Joined:
    May 2, 2017
    Posts:
    85
    Hi folks, We are using Shader.DisableKeyword and EnableKeyword to toggle between some colors in our shaders. ie a box is red in one environment, and blue in another.

    It seems to be working when playing in a build, but it is really flaky in the editor. (And today I can't get it to work at all, hence the forum post)

    Is there something about the editor that makes DisableKeyword and EnableKeywod fail?
    Is there anything I can do to make it work consistently?
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    Hi!
    Which Unity version are you using?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    There isn't anything particularly different about those keyword when in the editor.

    However I'd ask how and when you're calling them. There's some behavior, especially anything concerning cameras, that can behave very differently in the editor if you're not careful. Maybe try adding some debug logging and make sure those functions are only being called when you expect them to be and by the things you want.

    For example anything that uses Camera callbacks can potentially get called by the Scene or inspector preview camera.
     
  4. IronHelmet

    IronHelmet

    Joined:
    May 2, 2017
    Posts:
    85
    Sorry for the slow reply. We are on 2020.3.1f1 URP 10.3.2

    I have log statements next to the calls and have been monitoring when our code is called. Nothing weird going on with cameras.