一般在游戏中,主角或者怪物会受到减速效果,或者攻击速度减慢等类似的状态。本身动作减速的同时,衔接在角色上的特效也需要改变相应的播放速度。一般特效有三个游戏组件:

关键点就是改变Animator,Animation和Particle System的Speed这个速度变量。就可以随意的控制特效的播放速度了。

代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)
        {
            var main = tr.particleSystem.main;
            main.simulationSpeed = 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);
 
        }
    }
}