- Home /
UI Canvas sorting with custom UI Shader
So, I'm currently optimizing the shaders for a project and I'm stuck when it comes to custom UI shaders.
I tried to write a Shader that basically does the same as the standard UI Shader from Unity but without all the special cases like masks and whatnot. So I came up with the Shader below.
Problem is: It breaks the Unity internal canvas z-sorting system somehow. So Images with a material attached with this shader seem to sort randomly and I don't know why. Can somebody help me?
Here's the code:
Shader "Mobile/UI/Default"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
}
SubShader
{
Tags
{
"Queue"="Geometry"
"RenderType"="Opaque"
"ForceNoShadowCasting" = "True"
"IgnoreProjector"="True"
}
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
Cull Back
// I tried to edit these with no results
ZWrite Off
ZTest Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma glsl_no_auto_normalization
#pragma only_renderers gles gles3 metal d3d11 glcore
sampler2D _MainTex;
struct VertexInput
{
fixed4 position:POSITION;
fixed2 uv:TEXCOORD0;
fixed4 tintColor:COLOR;
};
struct VertexOutput
{
fixed4 position : SV_POSITION;
fixed2 uv:TEXCOORD0;
fixed4 tintColor:COLOR;
};
VertexOutput vert (VertexInput input)
{
VertexOutput output;
output.position = mul (UNITY_MATRIX_MVP, input.position);
output.uv = input.uv;
output.tintColor = input.tintColor;
return output;
}
fixed4 frag (VertexOutput input) : COLOR
{
fixed4 fragmentColor = tex2D (_MainTex, input.uv) * input.tintColor;
return fragmentColor;
}
ENDCG
}
}
}
I found something strange...
I've created a new Shader and copied the code over. So in the end I got 2 Shader files with different names but otherwise identical code. With the first Shader the sorting is broken with the second one it isn't!
So Unity must have some kind of Shader/$$anonymous$$aterial cache that may cache some things internally. Does anyone have a clue about that?
Answer by Plawius · Jul 12, 2016 at 12:06 PM
Hey! Open .mat file and remove this line:
m_CustomRenderQueue: #something#
Now it should work.
Don't ask, just old Unity bug.
Thanks, I will try this next time this becomes important. We moved on to other projects and left the UI System behind for now. :)
Your answer
Follow this Question
Related Questions
UI object above particles issue - Scene View different from the Game View 1 Answer
UI object drawing order is different between Unity Editor and Standalone version 1 Answer
Canvas override sorting issue. 1 Answer
Manually z-sorting opaque geometry 1 Answer
Shader tints transparency instead of leaving it invisible 1 Answer