Unity NGUI播特效出现穿透问题

本文介绍了在Unity游戏开发中如何避免粒子特效穿透UI的问题,通过设置粒子系统的sortingOrder属性来确保特效正确显示在UI之上。开发者可以了解如何初始化、控制粒子效果、停用和调整特效的排序顺序,以提升用户体验。

发现问题

  开发游戏的过程中,有些活动按钮需要播放特效,有些升级、强化需要播放特效,常常会遇到特效穿透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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值