LoginSignup
25
26

More than 5 years have passed since last update.

Unity2DのSpriteとParticleSystemの描画優先度設定

Posted at

ゲームを作っている時、基本的にUIは3Dオブジェクトよりも全面に表示するけど、たまにエフェクト(Particle System)をUIよりも前に表示したいといった時がある。
それをUnity2Dではどうやるのかなと思って調べてみた。

試しに何も考えずに置いてみると、

test01.jpg

PositionZの値をいくら変えてもエフェクトが背面に表示された。

Spriteを描画するとき、その優先度はSorting LayerとOrder in Layerによって決定される。
[Unity]Unity2d機能、スプライトの使い方

それと同じことをやってあげれば良さそうだが、Particle SystemではLayerは設定できるけどSorting LayerはInspector上で設定できないため、専用のスクリプトを用意する必要があるみたい。

SortingLayerSetter.cs
using UnityEngine;
using System.Collections;

public class SortingLayerSetter : MonoBehaviour {

    [SerializeField]
    private string _sortingLayerName = "Default";

    // Use this for initialization
    void Start () {
        renderer.sortingLayerName = _sortingLayerName;
    }
}

このスクリプトをコンポーネントに追加して、Inspector上からSortingLayerの名前を指定してあげればOK。

test02.jpg

無事にSortingLayerを指定して優先度を設定することができた。
デバッグほとんどしてないから何かあるかも。

25
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
26