Why doesn’t my particle system show in front of my sprites??
Why doesn’t my particle system show in front of my sprites??
This one had me befuddled for a little while. I could not figure out why my particle system was not showing up in front of my sprites for the life of me. I had the “Layer” set properly but I couldnot find a place to set the “Sorting Layer”. I came across some other folks who were saying you had to set the sorting layer manually in code so theparticle system would show up where it needed to (sorry, I can’t remember the references!). Okay, that seems easy enough, let’s see if we can figure that out. It looks like we can do that via the following code. The “sortingLayerName” is just a string of a sorting layer you have defined in Unity and the “sortingOrder” is an integer that specifies the z-order in that sorting layer. The higher the number, the closer the object “looks” to you.
1 2 | particleSystem.renderer.sortingLayerName = sortingLayerName; particleSystem.renderer.sortingOrder = sortingOrder; |
So now let’s just make this into a script so we can easily apply it to all particle systems we create.
I put the following code into a file named “ParticleSystemFix.cs” and attached it to any particle systems I create.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using UnityEngine; using System.Collections; public class ParticleSystemFix : MonoBehaviour { public string sortingLayerName; public int sortingOrder; /** */ void Start() { particleSystem.renderer.sortingLayerName = sortingLayerName; particleSystem.renderer.sortingOrder = sortingOrder; enabled = false ; } } |
I’ll usually tell a script to disable itself once it is done initializing or if the code in the Update() function starts to be skipped because of a certain condition. I’m sure there has to be a slight performance advantage to doing this…every little bit helps. Plus it lets me know if something is executing properly when I expect the script to become disabled.
Now, just attach this script to any particle system; there in the inspector, you can define a “sortingLayerName” and “sortingOrder” on the “ParticleSystemFix”. I used a large number like 999 as my “sortingOrder” since I want my particle systems to appear on top of all my sprites.
Make sure that what you pass into the “sortingLayerName” is defined in Unity!
That is about it, as long as the “sortingLayerName” for your particle system is where it needs to be hierarchically, your particle system should appear on top of the sprites.
출처 :
'Unity3D > Plugins' 카테고리의 다른 글
SVG Importer (0) | 2015.09.10 |
---|---|
WebView 플러그인 Awesomium(HTML UI Engine) (3) | 2015.08.05 |
UnityToolbag (0) | 2015.07.20 |
[펌] Unity ios 빌드시 Linq 문제 없이 사용할 수 있는 방법 (0) | 2015.07.01 |
[Spine] 게임 제작을 위한 최고의 애니메이션 툴! “스파인 (SPINE)” (1) | 2015.05.11 |