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);

        }
    }
}
相关文章
相关标签/搜索