• 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
10
Question by Xorglord · Dec 13, 2013 at 01:26 AM · texttextmeshsorting layers

3D textmesh not being drawn properly in a 2D game

Ok, so I've been having a couple of issues with 3D text in my 2D game. Mainly it boils down to setting the depth of the 3D text to be behind other objects instead of drawing over the top. I've changed the shader to match the one that I've seen linked around on the unity forums and it sort of works and sort of doesnt. When I created a test scene with just one cube and some 3d text it worked perfectly with the text being hidden behind the cube, however in my actual game scene (when I apply the same font & text material) it doesn't work the same way.

The only reason I can see this happening is because I'm using sprites in my main game and was using a cube in my test scene. Maybe there's an issue with the sorting depth interfering with how things are drawn? I am very stuck on this issue and I'd love any help.

I also read somewhere that non-sprite objects were sent to the default sorting layer, so this might be what's causing the issue? But I still wouldn't know how to fix it if this were the case.

Here are some pictures to illustrate the issue: alt text This one is working exactly as it should (this is the text scene). The text is behind the object since it is further away from the camera.

alt text However, in the actual game scene the text for the behind clipboard is being rendered on top of the above clipboard, even though it's set up almost the exact same as in the test scene.

Thanks for any light you're able to shed on this one :).

1.png (19.1 kB)
2.png (50.1 kB)
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

6 Replies

· Add your reply
  • Sort: 
avatar image
28
Best Answer

Answer by Xorglord · Dec 13, 2013 at 09:21 AM

Ok I've got an answer, finally! The problem was because of all non-sprite components defaulting to be in the "default" sorting layer. I though it wasn't possible to edit the sorting layer or layer order of non-sprite elements since there are no sorting options in their renderers, but apparently those options are accessible via code and are just not available in the Inspector.

More info here http://forum.unity3d.com/threads/211822-Using-non-Sprites-with-the-new-sorting-layers

Anyway, by using

GetComponent().sortingLayerID = blah; and .sortingOrder = blah;

I was able to set the rendering depth of the text. Thanks for the advice though Young :).

Comment
Add comment · Show 9 · 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
avatar image Jinxology · Jan 04, 2014 at 10:25 AM 0
Share

I'll do you one better. This guy wrote an editor component that allows you to set it in the inspector on any renderer. Wooo!

https://github.com/nickgravelyn/UnityToolbag/tree/master/Sorting%20Layer%20Exposed

avatar image jim0_o · Aug 26, 2014 at 06:52 PM 0
Share

Thanks for posting and answering the question. Highly appreciated.

avatar image Marzoa · Oct 27, 2014 at 10:46 AM 8
Share

You can attach this script to such objects so you can edit these parameters right in the Editor, though they won't have effect until the game is run.

 using UnityEngine;
 using System.Collections;
 
 public class SortingLayerExposer : $$anonymous$$onoBehaviour
 {
 
         public string SortingLayerName = "Default";
         public int SortingOrder = 0;
 
         void Awake ()
         {
                 gameObject.GetComponent<$$anonymous$$eshRenderer> ().sortingLayerName = SortingLayerName;
                 gameObject.GetComponent<$$anonymous$$eshRenderer> ().sortingOrder = SortingOrder;
         }
 }

avatar image Tabu Marzoa · Apr 04, 2016 at 02:41 PM 1
Share

Goddamit - I wanted to report you for being awesome... however, that is not possible :( I have ins$$anonymous$$d rewarded you with an upvote and this comment. You are awesome!

avatar image Arkade Marzoa · Sep 10, 2018 at 09:14 AM 0
Share

To get it shown in-Editor (pre-Play time), here's an updated version of @$$anonymous$$arzoa's code (also mentioned above by @Jinxology)

 using UnityEngine;
 
 /// <summary>
 /// Place on a 3D object to modify its visibility relative to Sprite 'sorting
 /// layers'.
 /// </summary>
 [ExecuteInEdit$$anonymous$$ode]
 public sealed class SortingLayerExposer : $$anonymous$$onoBehaviour {
 
     [SerializeField]
     private string SortingLayerName = "Default";
 
     [SerializeField]
     private int SortingOrder = 0;
 
     public void OnValidate() {
         apply();
     }
 
     public void OnEnable() {
         apply();
     }
 
     private void apply() {
         var meshRenderer = gameObject.GetComponent<$$anonymous$$eshRenderer>();
         meshRenderer.sortingLayerName = SortingLayerName;
         meshRenderer.sortingOrder = SortingOrder;
     }
 }
avatar image Retrochamber · Dec 02, 2014 at 09:58 PM 0
Share

This works like a charm, thanks for providing me a solution.

avatar image newnibie · Feb 21, 2015 at 12:03 AM 0
Share

This had been causing me such a headache for the opposite reason (I wanted to display text above sprites in different layers) thank you for sharing that information!

Show more comments
avatar image
1

Answer by kernjiro · Jul 20, 2014 at 09:09 PM

Alright, I had this problem too and the only solutions i could find were SUPER complicated. Here, just attach this simple script to any game object with a mesh renderer.(C#)

using UnityEngine; using System.Collections;

public class RendererSorter : MonoBehaviour {

 void Start () {
     this.gameObject.GetComponent<MeshRenderer>().sortingLayerName = "Foreground";

//or whatever your layer name is

     this.gameObject.GetComponent<MeshRenderer>().sortingOrder = 50; 

//or whatever order you want it in the layer

}

}

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
avatar image
1

Answer by Free286 · Jun 08, 2016 at 03:55 PM

It is a useful pattern to write code that only runs in the editor, Unity internally serializes many fields such as the sorting layer and order, which we can take advantage of by creating editor only scripts. This is especially important on mobile platforms, where performance is critical.

 [ExecuteInEditMode]
 [RequireComponent(typeof(TextMesh))]
 public class CONTENT_Text : MonoBehaviour 
 {
     public string sortingLayerName = "Default";
     public int orderInLayer = 0;
 
     #if UNITY_EDITOR
     public void Update () 
     {
         gameObject.GetComponent<MeshRenderer>().sortingLayerName = sortingLayerName;
         gameObject.GetComponent<MeshRenderer>().sortingOrder = orderInLayer;
     }
     #endif
 }

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
avatar image
0

Answer by YoungDeveloper · Dec 13, 2013 at 01:31 AM

When you render anything gui related (text, rect, etc), you can use GUI.depth to set render priority, for example GUI.depth=0; is on top of everything.

http://docs.unity3d.com/Documentation/ScriptReference/GUI-depth.html

Comment
Add comment · Show 5 · 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
avatar image Xorglord · Dec 13, 2013 at 01:37 AM 0
Share

Does the GUI Depth work in relation to the depth of the other objects in the scene which aren't GUI? I tried looking at the reference you linked but it wasn't that informative. I assume since the sprites aren't really gui, their spriterenderer doesn't have a GUI.depth property? What would I set the 3D text's gui depth to be so it renders in front of some sprites and behind others?

avatar image YoungDeveloper · Dec 13, 2013 at 01:40 AM 0
Share

For what exactly are you using sprites in the view ?

avatar image Xorglord · Dec 13, 2013 at 01:42 AM 0
Share

Im a little confused by your question. The game is in 2D, so all of the objects are basically sprites apart from the dynamically created stuff (like text). Everything is a sprite apart from the text at this point.

EDIT: I might have misunderstood what you meant by view, i'm not sure. You mean just like, the view in the game?

avatar image YoungDeveloper · Dec 13, 2013 at 07:47 AM 0
Share

Yea, i mean game view, overally i never had any experience with sprites, so i can't tell much about it's render priority with gui text. I'm not sure why are you using sprites for drawing those notepads. You could use gui for all this and there would be no broblems. The only idea what comes to my $$anonymous$$d for now is drawing transparent gui box in the shape of the front notepad, so by that you could hide background text, but that might be inefficient from optimization perspective.

avatar image Xorglord · Dec 13, 2013 at 08:08 AM 0
Share

Yeah I feel like it might be a bit inefficient, plus I'd have to draw one for each item like a clipboard that I had (there would be multiple). I guess it's my best option for now though. I've never really used GUI, but wouldn't it be a bit dodgy to render every sprite in a 2D game with GUI? I feel like it should be reserved for actual GUI elements haha.

avatar image
0

Answer by GreyEyedBeast · Feb 17, 2014 at 12:49 AM

setting my default layer to render on top seems to have fixed my issues

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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UGUI custom material for some parts of the text 0 Answers

Text Mesh to Mesh ? 2 Answers

Using ALT + codes to type special characters in Unity 0 Answers

How do I set scale of individual TextMeshPro character? 0 Answers

Weird edges on alpha textures with text 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