发现问题
开发游戏的过程中,有些活动按钮需要播放特效,有些升级、强化需要播放特效,常常会遇到特效穿透UI的情况。只要将粒子的sortingOrder 设置成想要的层就可以了
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Core.Panel;
using FPS.Define;
using FPS;
using Core.Base;
public abstract class UIEffect : MonoBehaviour {
public List<EventDelegate> OnFinished = new List<EventDelegate>();
protected BaseWindow playWindow;
protected GameObject widget;
protected bool loop = false;
protected float duration = 1f;
protected string effectName = string.Empty;
protected int sortingOrder = 0;
protected virtual void Init() {
DontDestroyOnLoad(gameObject);
Send.RegisterMsg(SendType.WindowClose, OnWindowClose);
loop = false;
ParticleSystem[] particles = GetComponentsInChildren<ParticleSystem>(true);
for(int i = 0, max = particles.Length; i < max; i++) {
if(particles[i].main.loop) {
loop = true;
break;
}
}
}
protected virtual void Start() {
Update();
}
protected virtual void Update() {
if(widget != null) {
UIPanel panel = UIPanel.Find(widget.transform);
if(panel != null) {
Renderer[] renders = GetComponentsInChildren<Renderer>();
foreach(Renderer temp in renders) {
if(temp.sortingOrder != panel.sortingOrder + sortingOrder) {
temp.sortingOrder = panel.sortingOrder + sortingOrder;
}
}
}
} else {
gameObject.Destroy();
}
}
protected virtual void OnDestroy() {
Send.UnregisterMsg(SendType.WindowClose, OnWindowClose);
}
private void OnWindowClose(object[] _objs) {
BaseWindow window = (BaseWindow)_objs[0];
if(playWindow == window) {
Destroy(gameObject);
}
}
protected bool ParticleIsPlaying() {
ParticleSystem[] particles = GetComponentsInChildren<ParticleSystem>(true);
for(int i = 0, max = particles.Length; i < max; i++) {
if(particles[i].IsAlive(true)) {
return true;
}
}
return false;
}
protected static bool Predicate(UIEffect _widget) {
return _widget == null;
}
public void StopEffect() {
ParticleSystem[] particle = gameObject.GetComponentsInChildren<ParticleSystem>(true);
for(int i = 0; i < particle.Length; i++) {
particle[i].Clear(true);
particle[i].Stop(true);
}
}
public void ScaleEffect(float _scale) {
transform.localScale *= _scale;
ParticleSystem[] particle = gameObject.GetComponentsInChildren<ParticleSystem>(true);
for(int i = 0; i < particle.Length; i++) {
particle[i].startSpeed *= _scale;
particle[i].startSize *= _scale;
}
}
public void ForceBringToFront(int _sortingOrder) {
sortingOrder = _sortingOrder;
}
}