Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Artych · Nov 10, 2015 at 12:59 AM · 2dshadermeshspritesorting layers

Shader vs Sprites - Sorting layer (ZWrite Off not working)

Hello!

My project is a 2d game. I'm using unity 5.2 64bits, free, on windows 10.

I'm trying to use sorting layer for a material so that the mesh that uses it shows on top of a sprite.

From my research, i've found that i need a custom shader with ZWrite Off. Also, the mesh is created dynamically, and in it (MeshRenderer) i set the sorting layer. Furthermore, i changed the editor so that i coud actually check if the sorting layer is being set on the MeshRenderer (it is). Even with all that, it just does not work (mesh is always below everything). See both codes below:

Mesh Creation:

         MeshRenderer mr = gameObject.AddComponent<MeshRenderer>();
         mr.sortingLayerName = "Particles";
         mr.materials = myMaterial;
         MeshFilter mf = gameObject.AddComponent<MeshFilter>();        
         Mesh mesh = new Mesh();
         mf.mesh = mesh;
         //Vertices
         // DO STUFF
         //Assign Arrays
         mesh.vertices = vertices;
         mesh.triangles = tri;
         mesh.normals = normals;
         mesh.uv = uv;

Shader:

 Shader "Custom/shader1" {
     Properties{
         _Color("Color", Color) = (1.0,1.0,1.0,1.0)
     }
     SubShader {
         Pass {
             ZWrite Off
 
             CGPROGRAM
             //pragmas
             #pragma vertex vert
             #pragma fragment frag
             //user variables
             //vertex f
             uniform float4 _Color;
             //base input structs
             struct vertexInput {
                 float4 vertex : POSITION;
             };
             struct vertexOutput {
                 float4 pos : SV_POSITION;
             };
             vertexOutput vert(vertexInput v) {
                 vertexOutput o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 return o;
             }
             float4 frag(vertexOutput i) : COLOR {
                 return _Color;
             }
             ENDCG
         }
     }
 }

Editor check:

 public override void OnInspectorGUI()
         {
             base.OnInspectorGUI();
 
             MeshRenderer renderer = target as MeshRenderer;
 
             EditorGUILayout.BeginHorizontal();
             EditorGUI.BeginChangeCheck();
             string name = EditorGUILayout.TextField("Sorting Layer Name", renderer.sortingLayerName);
             if (EditorGUI.EndChangeCheck())
             {
                 renderer.sortingLayerName = name;
             }
             EditorGUILayout.EndHorizontal();
 
             EditorGUILayout.BeginHorizontal();
             EditorGUI.BeginChangeCheck();
             int order = EditorGUILayout.IntField("Sorting Order", renderer.sortingOrder);
             if (EditorGUI.EndChangeCheck())
             {
                 renderer.sortingOrder = order;
             }
             EditorGUILayout.EndHorizontal();
 
         }

Thanks!

Sources:

  • http://forum.unity3d.com/threads/drawing-order-of-meshes-and-sprites.212006/

  • https://www.youtube.com/watch?v=d15kq_qTy_g

  • https://www.youtube.com/watch?v=R_kV3YiJqEw

  • http://docs.unity3d.com/Manual/SL-CullAndDepth.html

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GiyomuGames · Nov 10, 2015 at 01:03 AM

Hi Artych,

I have the same problem, tried the same things and like you I didn't get good results: http://answers.unity3d.com/questions/1064940/unity-5-meshrenderer-and-sortinglayer-not-working.html

You can see my work around in my post, but maybe we are both not doing the right thing?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shader for mesh to project onto sprite? 0 Answers

Render sprite only on top of a specific sorting layer 1 Answer

Sprite not visible from behind 0 Answers

Edge outline & 2D shadow Shader for planar mesh 0 Answers

Problem with coloring mesh with shaders properly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges