Search Unity

How to make a 2D Shader works with OnGUI() through Graphics.DrawTexture()

Discussion in 'Shaders' started by manutoo, Apr 18, 2012.

  1. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    524
    Hello,

    I just got that problem, so here a mini tutorial to help anyone running into it.

    So if you want to use a custom 2D Shader within your OnGUI(), you'll have to use :
    Code (csharp):
    1. Graphics.DrawTexture(screenRect : Rect, texture : Texture, mat : Material = null)
    with 'mat' referencing your custom Shader.

    To make your Shader work correctly, you'll have to add these lines to it, at start of SubShader :
    Code (csharp):
    1. SubShader
    2.     {
    3.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    4.         Lighting Off
    5.         Cull Off
    6.         ZTest Always
    7.         ZWrite Off
    8.         Fog { Mode Off }
    Me, I didn't at 1st, and it was working fine in the Editor, but didn't work in the Webplayer nor in the Windows Build : nothing was appearing.
    After a few tests, I discovered it didn't work in the same way with original Unity shaders, except "GUI/Text Shader" that produced a white output.
    So I downloaded the legacy shaders (from here : http://unity3d.com/download_unity/builtin_shaders.zip ), and checked the "Font.Shader" where I found the mentioned lines.
    Maybe not all of them are necessary, but I didn't take the time to test all 1 by 1... ;)
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Change this Queue to Overlay, so you make sure its rendered last.
     
  3. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    524
    Thanks for the info, Aubergine !