Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

3D text and 2D sprites - I can't get the text to be "in front" of the sprites

Discussion in 'Immediate Mode GUI (IMGUI)' started by guitarxe, Dec 27, 2013.

Thread Status:
Not open for further replies.
  1. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    So you know how you can have different sorting layers for sprites, so that you can put one in front or another behind? Well for 3D text objects there's no such thing, and so I don't know how to get my 3D text to appear infront of my sprite background. It always goes behind it so I can't see it. I tried changing the Z value, to get it infront of the sprite, but that still didn't work.

    Is it possible to do this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Renderer.sortingLayerID and/or Renderer.sortingOrder.

    --Eric
     
  3. binsint

    binsint

    Joined:
    Jul 16, 2011
    Posts:
    83
    Hi, this is my problem right now. it seems that it cannot show the 3d text on the game. :( :( tried changing z value too,but didn;t work... Im still having problems with this.
     
  4. alvintandian

    alvintandian

    Joined:
    Nov 20, 2013
    Posts:
    5
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You know, I already posted the answer...use renderer.sortingLayerID and/or renderer.sortingOrder. Simple and it works.

    --Eric
     
    HannaKalashnyk likes this.
  6. binsint

    binsint

    Joined:
    Jul 16, 2011
    Posts:
    83
    Yep! It worked! Thank you!
     
    HannaKalashnyk likes this.
  7. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    Its not in the Docs?
     
  8. Romano

    Romano

    Joined:
    Nov 27, 2013
    Posts:
    76
    Hey, can a kind chap help me get this to work?

    I've currently got this script attached to my text, which I want to appear in front of my sprites:

    var textRenderer : Renderer;

    function Awake ()
    {
    textRenderer = gameObject.GetComponent(Renderer);
    }

    function Start ()
    {
    textRenderer.sortingLayerID = 99;
    }

    Doesn't really work. Is Renderer.sortingLayerID read only or something?

    Was looking at this post also: http://forum.unity3d.com/threads/213817-Setting-Renderer-sortingLayerID-serialized-property?p=1434598&viewfull=1#post1434598

    It looks very complicated, so I'm hoping it's as simple as this thread suggests.

    Thanks very much!
    Romano
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If it was, the docs would say so, and the script would generate an error. I really doubt you have 100 sorting layers in your project, so try using the layers you actually have.

    --Eric
     
  10. Romano

    Romano

    Joined:
    Nov 27, 2013
    Posts:
    76
    That is the actual answer, I just figured it out. Thanks :) The sortingLayerID has to be created in the inspector beforehand. No idea if there's a way of creating them from code.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I wish there was....

    --Eric
     
  12. badjano

    badjano

    Joined:
    Aug 2, 2012
    Posts:
    23
    I just learned something you all haven't thought of...

    I just wasn't satisfied by fixing the sorting via script, so I was messing around and figured that I could create a sorting layer called "Background" behind Default sorting layer ( where all 3D objects and texts are ) and put everything that I wanted to be behind 3D objects in it. And ( as you may figured ) created a "Foreground" sorting layer in front of Default so if I wanted to but some sprite in front of the 3D stuff and it worked perfectly!

    you're welcome ;)
     
    imwundev, Laury21, pythongeko and 3 others like this.
  13. VigRoco

    VigRoco

    Joined:
    Aug 20, 2013
    Posts:
    1
    I agree this is the best method and works.
     
  14. BDCJR

    BDCJR

    Joined:
    Apr 25, 2013
    Posts:
    19
    Test example:
    1. Place on scene a GameObject with SpriteRenderer component on it. Look at property "Order in layer" - you can set that to bring your sprite in front of another, incrementing the value will bring your object close to camera.
    2. Place on scene a 3DText GameObject. Look at property "Offset Z" - you can set it to bring your text close to cam.

    IMPORTANT!!!
    If you want the text to be on top of sprite the "Offset Z" value should be bigger than "Order in layer" but it need to be a negative value, because 3DText works in 3D space, and SpriteRenderer works in 2D space, you need to set "Offset Z" (at 3DText gameObject) with a negative value to bring it more close to camera.

    EX: offsetZ = (orderLayer + 1) x -1


    This works for me. I hope this make sense and my english is enough readable. :D
     
    ZZ_Max likes this.
  15. masterE

    masterE

    Joined:
    Mar 9, 2015
    Posts:
    3
    using UnityEngine;
    using System.Collections;

    public class SortingLayerExposer : MonoBehaviour
    {

    public string SortingLayerName = "Default";
    public int SortingOrder = 0;

    void Awake ()
    {
    gameObject.GetComponent<MeshRenderer> ().sortingLayerName = SortingLayerName;
    gameObject.GetComponent<MeshRenderer> ().sortingOrder = SortingOrder;
    }
    }



    this will be work.you are welcome !
     
    highflier likes this.
  16. Fexman

    Fexman

    Joined:
    Jun 17, 2015
    Posts:
    5
    Perfect thanks, just slide the default Sorting Layer to the level you want the text to render.
    upload_2015-6-19_15-21-59.png
     
  17. matt_hodson

    matt_hodson

    Joined:
    Oct 24, 2016
    Posts:
    1
    The final touch that helped me with this issue after following the above two suggestions was to add a Canvas to my text label - the Canvas then allowed 'override sorting' which then let me set a sorting layer for it.

    Epic for something that should be so simple.
     
    msramzani likes this.
  18. Raptor8

    Raptor8

    Joined:
    Feb 26, 2015
    Posts:
    3
    its not working for me , this is my code :
    GameObject.Find("frontP").GetComponent<Renderer>().sortingLayerID = 99;

    and unity gives me :
    Invalid layer id. Please use the unique id of the layer (which is not the same as its index in the list).
     
  19. krupps

    krupps

    Joined:
    Oct 17, 2017
    Posts:
    159
    It's a defect?? with the TextMesh. They didn't add the 2 values to the inspector. Create a Script to override it those values. You won't see it at design time, but at runtime it works fine
     
  20. Darwinlzl

    Darwinlzl

    Joined:
    Nov 15, 2017
    Posts:
    1
    Cant believe this Renderer.sortingLayerID and/or Renderer.sortingOrder is the solution for my sprite not appearing in my mesh background. Been stuck here for almost 3 day.
     
  21. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    47
    Hi, I am trying your solution but it is not working for me.
    The problem started happening when I updated to 2017.3 and only when I try it in an android device.

    Being the problem that meshes are rendered behind sprites I added a script as follows to my 3d objects:

    public class Sorting3d : MonoBehaviour {
    public int sortingOrder;
    public string sortingLayer;

    void Start () {
    sortingLayer = GetComponent<Renderer>().sortingLayerName;
    }

    void Update () {
    GetComponent<MeshRenderer>().sortingOrder = sortingOrder;
    }
    }

    When I run the game I see in the inspector that sortingLayer equals to Default both in sprites and meshes.
    Adjusting sortingObject value works between meshes but sprites still get rendered in front of meshes.

    What am I doing wrong?
     
  22. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    This is the Inspector answer. Just drag and drop! Nice
     
  23. Gen1n

    Gen1n

    Joined:
    Nov 14, 2016
    Posts:
    1
    For people still looking for this, in the "Add Component", you can add "sorting group". No need for scripts.
    Of course, you will still need to create a sorting layer from the "Tags & Layers" window.

    In my case, however, I just wanted the text to appear, so I didn't create any sorting layers. I added a "sorting group" component to my 3DText, and without changing layer name, I changed the "Order in Layer" to 1 (default: 0 )
     
Thread Status:
Not open for further replies.