unity3d筆記:控制特效的播放速度

       通常在遊戲中,主角或者怪物會受到減速效果,或者攻擊速度減慢等相似的狀態。自己動做減速的同時,銜接在角色上的特效也須要改變相應的播放速度。通常特效有三個遊戲組件:this

 

關鍵點就是改變Animator,Animation和Particle System的Speed這個速度變量。就能夠隨意的控制特效的播放速度了。spa

代碼以下:orm

using UnityEngine;
using System.Collections;

public class EffectSpeedControl : MonoBehaviour
{

   
    public float time = 1;//銷燬時間
    float mSpeed = 1f;//播放時間

    void Start()
    {

        ChangeSpeed(transform);
    }
    void ChangeSpeed(Transform tr)
    {

        if (tr.animation != null)
        {
            foreach (AnimationState an in tr.animation)
            {
                an.speed = mSpeed;
            }
        }
        Animator ani = tr.GetComponent<Animator>();
        if (ani != null)
        {
            ani.speed = mSpeed;
        }
        if (tr.particleSystem != null)
        {
            tr.particleSystem.playbackSpeed = mSpeed;
        }
        for (int i = 0; i < tr.childCount; i++)
        {
            ChangeSpeed(tr.GetChild(i));
        }

    }
    
    public float Speed
    {
        get
        {
            return mSpeed;
        }
        set
        {
            mSpeed = value;
            ChangeSpeed(transform);
        }
    }
    void Update()
    {
        time -= Time.deltaTime * mSpeed;
        if (time < 0)
        {
            Destroy(this.gameObject);

        }
    }
}
相關文章
相關標籤/搜索