Unity TimeLine 对Spine动画拓展

TimeLine 对Spine动画拓展

因为公司需要对Spine动画特效制作时间线控制特效生成、动画、摄像机移动,故对TimeLine做了一点拓展,网上也没什么相关文章故在此记录下。

Defualt Playables

巨好用的插件
可以自定义生成自己想要的类型 并生成对应的五篇代码 我们在代码上拓展就能实现自己想要的功能了。
Unity TimeLine 对Spine动画拓展_第1张图片

///show help 是否打开帮助提示 很贴心

///Playable Name 自定义轨道的名字

///是否创建标准的可混合的Playable
选没选中,区别在于:
选中,Track Binding Type 可以选择: 可实例化,并继承自 UnityEngine.Component 的所有类。
没选,Track Binding Type 可以选择:继承自 UnityEngine.Component 的所有类外,还可以选中GameObject,与null,的额外两种。

///Track Binding Type 挂载的“导演”类型

///Exposed References 加入的物件 提供很多可选

///Behaviour Variables 加入的参数

///Clip Caps 轨道混合模式 可以选择 也可以后面用代码控制

///Track Color 轨道的颜色 没什么调用

///Create Drawer 提供给你一个调色盘

///Reset键就是复位

话不多说 直接上代码

SpineTrackBehaviou【主要用于控制每个track的表现】

using System;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using Spine.Unity;

[Serializable]
public class SpineTrackBehaviour : PlayableBehaviour
{
    public string palyAnimation;
    public string endAnimation;

    private PlayableDirector pd;
    private SkeletonAnimation SKA;

    public override void OnPlayableCreate (Playable playable)
    {
        this.pd = (PlayableDirector)playable.GetGraph().GetResolver();
        foreach (var track in pd.playableAsset.outputs)
        {
            if (this.SKA == null && track.streamName == "Spine Track Track")
            {
                this.SKA = (SkeletonAnimation)pd.GetGenericBinding(track.sourceObject);
            }
        }
    }

    public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        if (palyAnimation==string.Empty ||endAnimation==string.Empty)
        {
            return;
        }
        this.SKA.AnimationState.ClearTrack(0);
        Spine.TrackEntry trackEntry = this.SKA.AnimationState.SetAnimation(0, palyAnimation, false);
        trackEntry.Complete += (Spine.TrackEntry trackEntry) => {
            this.SKA.AnimationState.SetAnimation(0, endAnimation, true);
        };
    }

}

SpineTrackClip【处理属性 ClipCaps为混合模式可修改】

using System;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using Spine.Unity;

[Serializable]
public class SpineTrackClip : PlayableAsset, ITimelineClipAsset
{
    public SpineTrackBehaviour template = new SpineTrackBehaviour ();

    public ClipCaps clipCaps
    {
        get { return ClipCaps.None; }
    }

    public override Playable CreatePlayable (PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable<SpineTrackBehaviour>.Create (graph, template);
        SpineTrackBehaviour clone = playable.GetBehaviour ();
        return playable;
    }
  
}

SpineTrackMixerBehaviour【我的理解是在播放过程中执行的改变 可以参考插件demo中的灯光Mix】

using System;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using Spine.Unity;

public class SpineTrackMixerBehaviour : PlayableBehaviour
{
    // NOTE: This function is called at runtime and edit time.  Keep that in mind when setting the values of properties.
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        SkeletonAnimation trackBinding = playerData as SkeletonAnimation;

        if (!trackBinding)
            return;

        int inputCount = playable.GetInputCount ();

        for (int i = 0; i < inputCount; i++)
        {
            float inputWeight = playable.GetInputWeight(i);
            ScriptPlayable<SpineTrackBehaviour> inputPlayable = (ScriptPlayable<SpineTrackBehaviour>)playable.GetInput(i);
            SpineTrackBehaviour input = inputPlayable.GetBehaviour ();
            
            // Use the above variables to process each frame of this playable.
            
        }
    }
}

SpineTrackTrack

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using Spine.Unity;

[TrackColor(0.855f, 0.8623f, 0.87f)]
[TrackClipType(typeof(SpineTrackClip))]
[TrackBindingType(typeof(SkeletonAnimation))]
public class SpineTrackTrack : TrackAsset
{
    public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
    {
        return ScriptPlayable<SpineTrackMixerBehaviour>.Create (graph, inputCount);
    }
}


你可能感兴趣的:(c#,unity,unity3d)