最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Unity 2D Draw object above other object if its child or below otherwise - Stack Overflow

programmeradmin1浏览0评论

I Have 2 types of Sprite Renderer GameObjects that user can spawn and place in world

  1. Window
  2. Block
  • blocks are basic elements
  • windows act as containers for blocks so user can group together similar blocks

how can i do such that blocks outside window appear below any window and blocks that are part of window (placed inside window) appear above that window.

I Have 2 types of Sprite Renderer GameObjects that user can spawn and place in world

  1. Window
  2. Block
  • blocks are basic elements
  • windows act as containers for blocks so user can group together similar blocks

how can i do such that blocks outside window appear below any window and blocks that are part of window (placed inside window) appear above that window.

Share Improve this question edited Mar 3 at 10:17 Jaimin Vashi asked Mar 3 at 10:11 Jaimin VashiJaimin Vashi 4311 bronze badges 5
  • 1 what is that? 3D ? Canvas UI? Something else? – derHugo Commented Mar 3 at 10:13
  • @derHugo i am sorry i missed that. those are sprites – Jaimin Vashi Commented Mar 3 at 10:14
  • 1 SpriteRenderer or sprites on UI.Image ? – derHugo Commented Mar 3 at 10:16
  • @derHugo Sprite Renderer Game Objects – Jaimin Vashi Commented Mar 3 at 10:18
  • 1 I suppose you would then go through the sortingOrder and calculate and update them accordingly whenever the user modifies the window groupings – derHugo Commented Mar 3 at 10:47
Add a comment  | 

1 Answer 1

Reset to default -1

To draw a block above glass if it is a child, set the sorting order property based on whether the glass is a parent of the block. For instance it could be something as simple as (and it goes without saying that this is pseudocode so don't use it):

var child = transform.GetChild(0);
if (child && child.CompareTag("block")){
GetComponent<SpriteRenderer>().sortingOrder = -1;
}

You would place such a script on all glass objects and ensure that all block objects are tagged with "block".

发布评论

评论列表(0)

  1. 暂无评论