一般在游戏中,主角或者怪物会受到减速效果,或者攻击速度减慢等类似的状态。本身动作减速的同时,衔接在角色上的特效也需要改变相应的播放速度。一般特效有三个游戏组件:
关键点就是改变Animator,Animation和Particle System的Speed这个速度变量。就可以随意的控制特效的播放速度了。
代码如下:
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);
}
}
}