unity特效管理

/********************************************************************
    created:    2013/12/24
    created:    24:12:2013   14:14
    filename:     FXController.cs
    author:        王迪
    
    purpose:    特效控制器,绑定在特效上,控制生命周期与释放
*********************************************************************/

using UnityEngine;
using System.Collections;
using Module.Log;
using GCGame.Table;
using System.Collections.Generic;

public class FXController : MonoBehaviour
{
    public enum FXType
    {
        TYPE_PARTICLE = 0,
        TYPE_WEAPONTRAIL = 1,
    }

    private float m_fDuration = 0;              // 特效持续时间
    public float Duration
    {
        get { return m_fDuration; }
        set { m_fDuration = value; }
    }
    private bool m_bOnlyDeactivate = false;     // 播放结束后是否只失效:true 播放完成后特效并不销毁,只是禁用。false 播放完成后,销毁特效
    public bool OnlyDeactivate
    {
        get { return m_bOnlyDeactivate; }
        set { m_bOnlyDeactivate = value; }
    }
    private int m_nEffectID = 0;                // EffectLogic分配ID 用于识别单个特效
    public int EffectID
    {
        get { return m_nEffectID; }
        set { m_nEffectID = value; }
    }
    private float m_fDelay = 0;                 // 播放延迟
    public float Delay
    {
        get { return m_fDelay; }
        set { m_fDelay = value; }
    }
    private FXType m_FxType;                   // 粒子种类
    public FXController.FXType FxType
    {
        get { return m_FxType; }
        set { m_FxType = value; }
    }

    private float m_fRemainduration = 0;
    public float Remainduration
    {
        get { return m_fRemainduration; }
        set { m_fRemainduration = value; }
    }
    private float m_fRemaindelay = 0;
    public float Remaindelay
    {
        get { return m_fRemaindelay; }
    }
    private GameObject m_fxObject = null;

    private GameObject m_EffectGameObj = null;
    Vector3 m_StartFXPos =new Vector3(0,0,0);
    Quaternion m_StartRotation =new Quaternion();
    private bool m_bIsFellowOwner = false;
    public bool IsFellowOwner
    {
        get { return m_bIsFellowOwner; }
        set { m_bIsFellowOwner = value; }
    }
    public UnityEngine.GameObject EffectGameObj
    {
        get { return m_EffectGameObj; }
        set { m_EffectGameObj = value; }
    }

    private EffectLogic m_curEffectHandle;      // EffectLogic
    public GameObject m_FirstChild;             // 第一个结点,通常绑定逻辑脚本\

    public float PlayerFinishTime = 0f;

    public float effectTime{ get; private set; }
    public void Play(EffectLogic curEffectHandle)
    {
        if (transform ==null)
        {
            return;
        }
        Transform child = transform.GetChild(0);
        if (null == child)
        {
            LogModule.ErrorLog("can not find effect in fxController");
            ResourceManager.DestroyResource(this.gameObject);
            return;
        }
        m_FirstChild = child.gameObject;
        m_fRemainduration = m_fDuration;
        ChangeDuration();
        m_fRemaindelay = m_fDelay;
        effectTime = 10.0f ;
        m_fxObject = child.gameObject;
        m_curEffectHandle = curEffectHandle;
        PlayerFinishTime = 0f;
        gameObject.SetActive(true);
    }

    public void Stop()
    {
        if (m_FxType == FXType.TYPE_PARTICLE)
        {
            StopCoroutine("CheckIfAlive");
        }
        RemoveParticle();
    }

    void DoPlay()
    {
        if (null == m_fxObject)
            return;

        if (m_FxType == FXType.TYPE_WEAPONTRAIL)
        {
            if (null != m_fxObject.GetComponent<MeleeWeaponTrail>())
                m_fxObject.GetComponent<MeleeWeaponTrail>().Emit = true;
        }
        else
        {
            m_fxObject.SetActive(true);
        }
        //记录下播放时候的位置
        m_StartFXPos = gameObject.transform.position;
        m_StartRotation = gameObject.transform.rotation;
    }

    void OnEnable()
    {
        if (gameObject.transform.parent != null)
        {
            gameObject.transform.position = gameObject.transform.parent.position;
            gameObject.transform.rotation = gameObject.transform.parent.rotation;
        }
        m_fRemaindelay = m_fDelay;
        if (m_fxObject != null && m_fxObject.GetComponent<ParticleSystem>() != null)
        {
            m_FxType = FXType.TYPE_PARTICLE;
            
        }
        if (gameObject.transform.parent.name == EffectLogic.m_NormalPonintName)
        {
            //StartCoroutine("CheckIfAlive");
        }

        m_fRemainduration = m_fDuration;
        ChangeDuration();

        if (m_fRemaindelay == 0)
        {
            DoPlay();
        }
    }

    void Update()
    {
        if (m_fRemaindelay > 0)
        {
            m_fRemaindelay -= Time.deltaTime;

            if (m_fRemaindelay <= 0)
            {
                DoPlay();
            }
            return;
        }

        if (m_fRemainduration > 0)
        {
            //如果不随主人移动 则 修正坐标为 播放时的坐标
            if (IsFellowOwner ==false)
            {
                gameObject.transform.position = m_StartFXPos;
                gameObject.transform.rotation = m_StartRotation;
            }
           
            m_fRemainduration -= Time.deltaTime;
            if (m_fRemainduration <= 0)
            {
                Stop();
            }
        }
    }

    IEnumerator CheckIfAlive()
    {

        while (effectTime>0)
        {
            yield return new WaitForSeconds(1.0f);
            effectTime -= 1.0f;
            if (effectTime <= 0)
            {
                Stop();
                StopCoroutine("CheckIfAlive");
            }
        }
    }

    void RemoveParticle()
    {
        PlayerFinishTime = Time.time;

        if (m_bOnlyDeactivate)
        {
            if (null != m_fxObject)
            {
                if (m_FxType == FXType.TYPE_WEAPONTRAIL)
                {
                    if (m_fxObject.GetComponent<MeleeWeaponTrail>() != null)
                        m_fxObject.GetComponent<MeleeWeaponTrail>().Emit = false;
                }
                else
                {
                    m_fxObject.SetActive(false);
                    this.gameObject.SetActive(false);
                }
            }

            if (null != m_curEffectHandle)
            {
                m_curEffectHandle.EffectDeactive(this);
            }
        }
        else
        {
            if (null != m_curEffectHandle)
            {
                m_curEffectHandle.EffectDestroyed(EffectID);
            }

            ResourceManager.DestroyResource(this.gameObject);

        }
    }
    //内存泄漏 fix low方案
    private void ChangeDuration()
    {
        foreach (KeyValuePair<int, List<Tab_ShenQiInfo>> kvp in TableManager.GetShenQiInfo())
        {
            for (int i = 0; i < kvp.Value.Count; i++)
            {
                if (kvp.Value[i].ShenQiEffctId == EffectID)
                {
                    return;
                }
            }
        }

        foreach (KeyValuePair<int, List<Tab_GemAttr>> kvp in TableManager.GetGemAttr())
        {
            for (int i = 0; i < kvp.Value.Count; i++)
            {
                if (kvp.Value[i].EffectID == EffectID)
                {
                    return;
                }
            }
        }


        if (!(m_fRemainduration > 0))
        {
            m_fRemainduration = 10.0f;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值