Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help solving Z-order of instantiated object from same prefab.

Discussion in '2D' started by johanesnw, Apr 18, 2014.

  1. johanesnw

    johanesnw

    Joined:
    Dec 11, 2013
    Posts:
    56
    Look at these pics.
    $Capture.PNG
    $Capture2.PNG

    those object created from a prefab. so it has same sorting layer and order in layer.
    when one overlaps another, it will look like the 2nd pic.
    bcos in the layer order, "hand" object will always be above "body".

    is there a quick way to fix this?
    what I've thought is adding some code in void Start() and traverse all children to add order in layer +10.
    so the new one will always be in front.
    and that will make 30th spawned at 300 depth in layer. is it a bad thing?

    We're 3-men indie developer and this is our first project. I do all the programming and have learned(and using) unity for 4 months.
    there's still really much code to do, so I don't even think about taking days just to fix the game visuals (yeah it does matter, but not critical. gameplay is more important)

    I'm asking your opinion or another approach to fix this.
    thanks.
     
  2. Deleted User

    Deleted User

    Guest

    I think you're on the right track.
    Just remember to set all sprites to the same sortingLayer and sort them by order.
    Then simply increment order via code by accessing renderer.sortingOrder...

    I think easiest approach for that would be adding script to your prefab, something like
    Code (csharp):
    1.  
    2. // pseudo-code, probably will not work
    3. void Start(){
    4.     //get how much other prefabs are spawned, if you don't have a way of knowing... you might want to use tags
    5.     int OtherPrefabsCount = GameObject.FindGameObjectsWithTag("Piggie").Count();
    6.     foreach(SpriteRenderer SR in this.GetComponents<SpriteRenderer>()){
    7.         SR.renderer.sortingOrder += OtherPrefabsCount*10;
    8.     }
    9. }
    10.  
    I hope I've got my idea across. It seems kind of quick dirty solution but it should work and shouldn't be hard on performance.
     
  3. johanesnw

    johanesnw

    Joined:
    Dec 11, 2013
    Posts:
    56
    yea seems like it's the fastest way. but I got the count from manager script.
    I got my code working like this

    Code (csharp):
    1. manager = GameObject.Find("Manager").GetComponent<Game_Manager>();
    2. Transform[] child = this.GetComponentsInChildren<Transform>();
    3.         foreach (Transform trans in child)
    4.         {
    5.             if (trans != trans.root) //bcos root object just contains collider and control scripts
    6.             {
    7.                 trans.GetComponent<SpriteRenderer>().sortingOrder += manager.spawnCount*10;
    8.                 //Debug.Log(trans.name + "->" + trans.GetComponent<SpriteRenderer>().sortingOrder);
    9.             }
    10.         }
    11.         manager.spawnCount++;
     
    ow3n likes this.
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    You have 65534 levels on a layer, so it should be a problem if unless you have over 6.5k groups on the screen.

    I deal with it one of two ways. The first is exactly the way you described. On awake I cache an array of the sprite renderers in the object and then have public method that I can change the "z-depth" which is as you described.

    The other way I handle is entirely with the z. This works well with a known amount of elements. For example playing cards. Each card is actually a stack of sprites constructed programmatically, and contains several hidden elements used in the animation. In that case I don't deal with sort order at all, z is the only factor. This works well because I am only change the z on the top level, rather than have to adjust all the children. I looks like this in the game/scene:
     

    Attached Files:

    Last edited: Apr 19, 2014