Unity3D插件之刀光

1:wiki上的开源脚本,原理:动态mesh

来源:http://wiki.unity3d.com/index.php?title=MeleeWeaponTrail

该刀光脚本由三部分组成:
最根本的刀光动态mesh脚本MeleeWeaponTrail.cs;刀光显示与否的控制脚本;以及刀光平滑插值脚本Interpolate.cs(http://wiki.unity3d.com/index.php/Interpolate)。其中插值脚本不是必须的。
使用方法:把MeleeWeaponTrail.cs脚本附加到3D模型武器所在的骨骼点上,同时为该骨骼点增加两个空的子物体,一个用来指示刀光的顶部位置,一个用来指示刀光的底部位置。脚本中取消“#define USE_INTERPOLATION”的注释,可以平滑刀光,但是记得到要在工程中添加Interpolate.cs脚本。
把脚本MeleeWeaponTrail.cs拖到骨骼点上,Inspector界面为

"刀光插件inspector视图"

Emit表示是否发出刀光,若是想通过代码控制,一般都取消Emit的勾选

EmitTime表示刀光发出后多久就停止发出刀光,0表示无限大,一般默认该值

Material为刀光使用的材质球。。。。注意材质球使用Particles/Additive的shader

LifeTime表示刀光面片存在的时间,单位s

Colors:淡出颜色,(通过设置Colors的alpha可以很好控制刀光的透明度,一般设置alpha为50效果还不错,另外通过设置Colors可以设置刀光的颜色,而不必再做一个带颜色的刀光了)

Sizes:设置刀光不同部位的大小

Min Vertex Distance: Never make segments shorter than this

Max Vertex Distance: Never make segments longer than this

Max Angle: Never make angles between segments greater than this. Increasing it will make a faster but uglier line.

Auto Destruct: Destroy this object and the render object if we are not emitting and have no segments left .

Base,Tip就是上面提到的刀光的底部和顶端位置

然后我们在刀光的控制脚本通过设置MeleeWeaponTrail.cs的Emit参数true/false来控制刀的开关。如下代码:

public class MeleeWeaponTrailControl : MonoBehaviour
{
    [SerializeField]
    MeleeWeaponTrail _trail;
    Animator animator;
    AnimatorStateInfo stateInfo0;
    void Start()
    {
        animator = GetComponent<Animator>();
        _trail.Emit = false;
    }
    void Update()
    {
        stateInfo0 = animator.GetCurrentAnimatorStateInfo(0);
        if (stateInfo0.IsName("Base Layer.Melee1")
            || stateInfo0.IsName("Base Layer.Melee2")
            || stateInfo0.IsName("Base Layer.Melee3")
            || stateInfo0.IsName("Base Layer.Melee4")
            || stateInfo0.IsName("Base Layer.Melee5"))
        {
            if (stateInfo0.normalizedTime > 0.1 && stateInfo0.normalizedTime < 0.8)
            {
                _trail.Emit = true;
            }
            else
            {
                _trail.Emit = false;
            }
        }
    }
}    

以上涉及到的脚本已经存到百度网盘:http://pan.baidu.com/s/1pJWqSP1

https://www.assetstore.unity3d.com/en/#!/content/1728这是Unity3D assetstore上的基于上述的工程。http://pan.baidu.com/s/1gel03AV这是该工程的网盘链接