void Start()
        {
            skeletonComponent = skeletonComponent ?? GetComponent <ISkeletonComponent>();
            if (skeletonComponent == null)
            {
                return;
            }
            animationStateComponent = animationStateComponent ?? skeletonComponent as IAnimationStateComponent;
            if (animationStateComponent == null)
            {
                return;
            }
            var skeleton = skeletonComponent.Skeleton;

            if (skeleton == null)
            {
                return;
            }


            var skeletonData = skeleton.Data;
            var state        = animationStateComponent.AnimationState;

            foreach (var ep in events)
            {
                var eventData = skeletonData.FindEvent(ep.spineEvent);
                ep.eventDelegate = ep.eventDelegate ?? delegate(TrackEntry trackEntry, Event e) { if (e.Data == eventData)
                                                                                                  {
                                                                                                      ep.unityHandler.Invoke();
                                                                                                  }
                };
                state.Event += ep.eventDelegate;
            }
        }
Пример #2
0
        private void Start()
        {
            if (this.ghostShader == null)
            {
                this.ghostShader = Shader.Find("Spine/Special/SkeletonGhost");
            }
            this.skeletonRenderer = base.GetComponent <SkeletonRenderer>();
            this.meshFilter       = base.GetComponent <MeshFilter>();
            this.meshRenderer     = base.GetComponent <MeshRenderer>();
            this.nextSpawnTime    = Time.time + this.spawnRate;
            this.pool             = new SkeletonGhostRenderer[this.maximumGhosts];
            for (int i = 0; i < this.maximumGhosts; i++)
            {
                Type[]     components = new Type[] { typeof(SkeletonGhostRenderer) };
                GameObject obj2       = new GameObject(base.gameObject.name + " Ghost", components);
                this.pool[i] = obj2.GetComponent <SkeletonGhostRenderer>();
                obj2.SetActive(false);
                obj2.hideFlags = HideFlags.HideInHierarchy;
            }
            IAnimationStateComponent skeletonRenderer = this.skeletonRenderer as IAnimationStateComponent;

            if (skeletonRenderer != null)
            {
                skeletonRenderer.AnimationState.Event += new Spine.AnimationState.TrackEntryEventDelegate(this.OnEvent);
            }
        }
Пример #3
0
        private void Start()
        {
            if (ghostShader == null)
            {
                ghostShader = Shader.Find("Spine/Special/SkeletonGhost");
            }
            skeletonRenderer = GetComponent <SkeletonRenderer>();
            meshFilter       = GetComponent <MeshFilter>();
            meshRenderer     = GetComponent <MeshRenderer>();
            nextSpawnTime    = Time.time + spawnRate;
            pool             = new SkeletonGhostRenderer[maximumGhosts];
            for (int i = 0; i < maximumGhosts; i++)
            {
                GameObject gameObject = new GameObject(base.gameObject.name + " Ghost", typeof(SkeletonGhostRenderer));
                pool[i] = gameObject.GetComponent <SkeletonGhostRenderer>();
                gameObject.SetActive(value: false);
                gameObject.hideFlags = HideFlags.HideInHierarchy;
            }
            IAnimationStateComponent animationStateComponent = skeletonRenderer as IAnimationStateComponent;

            if (animationStateComponent != null)
            {
                animationStateComponent.AnimationState.Event += OnEvent;
            }
        }
Пример #4
0
        private void Start()
        {
            ISkeletonComponent component = base.GetComponent <ISkeletonComponent>();

            if (component != null)
            {
                Skeleton skeleton = component.Skeleton;
                if (skeleton != null)
                {
                    if (this.combinedSkin == null)
                    {
                    }
                    this.combinedSkin = new Skin("combined");
                    this.combinedSkin.Clear();
                    foreach (string str in this.skinsToCombine)
                    {
                        Skin source = skeleton.Data.FindSkin(str);
                        if (source != null)
                        {
                            this.combinedSkin.Append(source);
                        }
                    }
                    skeleton.SetSkin(this.combinedSkin);
                    skeleton.SetToSetupPose();
                    IAnimationStateComponent component2 = component as IAnimationStateComponent;
                    if (component2 != null)
                    {
                        component2.AnimationState.Apply(skeleton);
                    }
                }
            }
        }
                private void Start()
                {
                    _spineAnimator = GetComponent <SpineAnimator>();

                    ISkeletonAnimation s = _spineAnimator.GetSkeletonAnimation() as ISkeletonAnimation;

                    if (s != null)
                    {
                        s.UpdateLocal += HandleUpdateLocal;
                    }

                    IAnimationStateComponent sa = _spineAnimator.GetSkeletonAnimation() as IAnimationStateComponent;

                    if (sa != null)
                    {
                        this._state = sa.AnimationState;
                    }

                    SetSourceBone(_sourceBoneName);

                    Skeleton skeleton = s.Skeleton;

                    _siblingBones.Clear();
                    foreach (string bn in _siblingBoneNames)
                    {
                        Bone b = skeleton.FindBone(bn);
                        if (b != null)
                        {
                            _siblingBones.Add(b);
                        }
                    }
                }
Пример #6
0
 public static void Initialize(this IAnimationStateComponent Anim, bool isOverride = false)
 {
     if (Anim is SkeletonAnimation)
     {
         ((SkeletonAnimation)Anim).Initialize(isOverride);
     }
     else
     {
         ((SkeletonGraphic)Anim).Initialize(isOverride);
     }
 }
        void OnDestroy()
        {
            animationStateComponent = animationStateComponent ?? GetComponent <IAnimationStateComponent>();
            if (animationStateComponent == null)
            {
                return;
            }

            var state = animationStateComponent.AnimationState;

            foreach (var ep in events)
            {
                if (ep.eventDelegate != null)
                {
                    state.Event -= ep.eventDelegate;
                }
                ep.eventDelegate = null;
            }
        }
 private void OnDestroy()
 {
     if (this.animationStateComponent == null)
     {
     }
     this.animationStateComponent = base.GetComponent <IAnimationStateComponent>();
     if (this.animationStateComponent != null)
     {
         Spine.AnimationState animationState = this.animationStateComponent.AnimationState;
         foreach (EventPair pair in this.events)
         {
             if (pair.eventDelegate != null)
             {
                 animationState.Event -= pair.eventDelegate;
             }
             pair.eventDelegate = null;
         }
     }
 }
Пример #9
0
    public void Watch()
    {
        if (state != null)
        {
            state.Event -= OnSpineEvent;
            state        = null;
        }

        IAnimationStateComponent animation = GetComponent <IAnimationStateComponent>();

        if (animation == null)
        {
            return;
        }

        state = animation.AnimationState;

        if (state != null)
        {
            state.Event += OnSpineEvent;
        }
    }
 private void Start()
 {
     if (this.skeletonComponent == null)
     {
     }
     this.skeletonComponent = base.GetComponent <ISkeletonComponent>();
     if (this.skeletonComponent != null)
     {
         if (this.animationStateComponent == null)
         {
         }
         this.animationStateComponent = this.skeletonComponent as IAnimationStateComponent;
         if (this.animationStateComponent != null)
         {
             Skeleton skeleton = this.skeletonComponent.Skeleton;
             if (skeleton != null)
             {
                 SkeletonData         data           = skeleton.Data;
                 Spine.AnimationState animationState = this.animationStateComponent.AnimationState;
                 using (List <EventPair> .Enumerator enumerator = this.events.GetEnumerator())
                 {
                     while (enumerator.MoveNext())
                     {
        public void PreviewEditModePose(Playable playable,
                                        ISkeletonComponent skeletonComponent, IAnimationStateComponent animationStateComponent,
                                        SkeletonAnimation skeletonAnimation, SkeletonGraphic skeletonGraphic)
        {
            if (Application.isPlaying)
            {
                return;
            }
            if (animationStateComponent.IsNullOrDestroyed() || skeletonComponent == null)
            {
                return;
            }

            int inputCount             = playable.GetInputCount();
            int lastNonZeroWeightTrack = -1;

            for (int i = 0; i < inputCount; i++)
            {
                float inputWeight = playable.GetInputWeight(i);
                if (inputWeight > 0)
                {
                    lastNonZeroWeightTrack = i;
                }
            }

            if (lastNonZeroWeightTrack != -1)
            {
                ScriptPlayable <SpineAnimationStateBehaviour> inputPlayableClip =
                    (ScriptPlayable <SpineAnimationStateBehaviour>)playable.GetInput(lastNonZeroWeightTrack);
                SpineAnimationStateBehaviour clipData = inputPlayableClip.GetBehaviour();

                var skeleton = skeletonComponent.Skeleton;

                bool skeletonDataMismatch = clipData.animationReference != null && clipData.animationReference.SkeletonDataAsset &&
                                            skeletonComponent.SkeletonDataAsset.GetSkeletonData(true) != clipData.animationReference.SkeletonDataAsset.GetSkeletonData(true);
                if (skeletonDataMismatch)
                {
                    Debug.LogWarningFormat("SpineAnimationStateMixerBehaviour tried to apply an animation for the wrong skeleton. Expected {0}. Was {1}",
                                           skeletonComponent.SkeletonDataAsset, clipData.animationReference.SkeletonDataAsset);
                }

                // Getting the from-animation here because it's required to get the mix information from AnimationStateData.
                Animation fromAnimation = null;
                float     fromClipTime  = 0;
                bool      fromClipLoop  = false;
                if (lastNonZeroWeightTrack != 0 && inputCount > 1)
                {
                    var fromClip     = (ScriptPlayable <SpineAnimationStateBehaviour>)playable.GetInput(lastNonZeroWeightTrack - 1);
                    var fromClipData = fromClip.GetBehaviour();
                    fromAnimation = fromClipData.animationReference != null ? fromClipData.animationReference.Animation : null;
                    fromClipTime  = (float)fromClip.GetTime() * (float)fromClip.GetSpeed();
                    fromClipLoop  = fromClipData.loop;
                }

                Animation toAnimation = clipData.animationReference != null ? clipData.animationReference.Animation : null;
                float     toClipTime  = (float)inputPlayableClip.GetTime() * (float)inputPlayableClip.GetSpeed();
                float     mixDuration = clipData.mixDuration;

                if (!clipData.customDuration && fromAnimation != null && toAnimation != null)
                {
                    mixDuration = animationStateComponent.AnimationState.Data.GetMix(fromAnimation, toAnimation);
                }

                if (trackIndex == 0)
                {
                    skeleton.SetToSetupPose();
                }

                // Approximate what AnimationState might do at runtime.
                if (fromAnimation != null && mixDuration > 0 && toClipTime < mixDuration)
                {
                    dummyAnimationState = dummyAnimationState ?? new AnimationState(skeletonComponent.SkeletonDataAsset.GetAnimationStateData());

                    var  toEntry   = dummyAnimationState.GetCurrent(0);
                    var  fromEntry = toEntry != null ? toEntry.MixingFrom : null;
                    bool isAnimationTransitionMatch = (toEntry != null && toEntry.Animation == toAnimation && fromEntry != null && fromEntry.Animation == fromAnimation);

                    if (!isAnimationTransitionMatch)
                    {
                        dummyAnimationState.ClearTracks();
                        fromEntry = dummyAnimationState.SetAnimation(0, fromAnimation, fromClipLoop);
                        fromEntry.AllowImmediateQueue();
                        if (toAnimation != null)
                        {
                            toEntry = dummyAnimationState.SetAnimation(0, toAnimation, clipData.loop);
                            toEntry.HoldPrevious = clipData.holdPrevious;
                        }
                    }

                    // Update track times.
                    fromEntry.TrackTime = fromClipTime;
                    if (toEntry != null)
                    {
                        toEntry.TrackTime = toClipTime;
                        toEntry.MixTime   = toClipTime;
                    }

                    // Apply Pose
                    dummyAnimationState.Update(0);
                    dummyAnimationState.Apply(skeleton);
                }
                else
                {
                    if (toAnimation != null)
                    {
                        toAnimation.Apply(skeleton, 0, toClipTime, clipData.loop, null, 1f, MixBlend.Setup, MixDirection.In);
                    }
                }
                skeleton.UpdateWorldTransform();

                if (skeletonAnimation)
                {
                    skeletonAnimation.LateUpdate();
                }
                else if (skeletonGraphic)
                {
                    skeletonGraphic.LateUpdate();
                }
            }
            // Do nothing outside of the first clip and the last clip.
        }
        // 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)
        {
            var skeletonAnimation = playerData as SkeletonAnimation;
            var skeletonGraphic   = playerData as SkeletonGraphic;

            animationStateComponent = playerData as IAnimationStateComponent;
            var skeletonComponent = playerData as ISkeletonComponent;

            if (animationStateComponent.IsNullOrDestroyed() || skeletonComponent == null)
            {
                return;
            }

            var skeleton = skeletonComponent.Skeleton;
            var state    = animationStateComponent.AnimationState;

            if (!Application.isPlaying)
            {
#if SPINE_EDITMODEPOSE
                PreviewEditModePose(playable, skeletonComponent, animationStateComponent,
                                    skeletonAnimation, skeletonGraphic);
#endif
                return;
            }

            int inputCount = playable.GetInputCount();

            // Ensure correct buffer size.
            if (this.lastInputWeights == null || this.lastInputWeights.Length < inputCount)
            {
                this.lastInputWeights = new float[inputCount];

                for (int i = 0; i < inputCount; i++)
                {
                    this.lastInputWeights[i] = default(float);
                }
            }
            var  lastInputWeights = this.lastInputWeights;
            int  numStartingClips = 0;
            bool anyClipPlaying   = false;

            // Check all clips. If a clip that was weight 0 turned into weight 1, call SetAnimation.
            for (int i = 0; i < inputCount; i++)
            {
                float lastInputWeight = lastInputWeights[i];
                float inputWeight     = playable.GetInputWeight(i);
                bool  clipStarted     = lastInputWeight == 0 && inputWeight > 0;
                if (inputWeight > 0)
                {
                    anyClipPlaying = true;
                }
                lastInputWeights[i] = inputWeight;

                if (clipStarted && numStartingClips < 2)
                {
                    ScriptPlayable <SpineAnimationStateBehaviour> clipPlayable = (ScriptPlayable <SpineAnimationStateBehaviour>)playable.GetInput(i);
                    startingClips[numStartingClips++] = clipPlayable;
                }
            }
            // unfortunately order of clips can be wrong when two start at the same time, we have to sort clips
            if (numStartingClips == 2)
            {
                ScriptPlayable <SpineAnimationStateBehaviour> clipPlayable0 = startingClips[0];
                ScriptPlayable <SpineAnimationStateBehaviour> clipPlayable1 = startingClips[1];
                if (clipPlayable0.GetDuration() > clipPlayable1.GetDuration())                   // swap, clip 0 ends after clip 1
                {
                    startingClips[0] = clipPlayable1;
                    startingClips[1] = clipPlayable0;
                }
            }

            for (int j = 0; j < numStartingClips; ++j)
            {
                ScriptPlayable <SpineAnimationStateBehaviour> clipPlayable = startingClips[j];
                SpineAnimationStateBehaviour clipData = clipPlayable.GetBehaviour();
                pauseWithDirector = !clipData.dontPauseWithDirector;
                endAtClipEnd      = !clipData.dontEndWithClip;
                endMixOutDuration = clipData.endMixOutDuration;

                if (clipData.animationReference == null)
                {
                    float mixDuration = clipData.customDuration ? GetCustomMixDuration(clipData) : state.Data.DefaultMix;
                    state.SetEmptyAnimation(trackIndex, mixDuration);
                }
                else
                {
                    if (clipData.animationReference.Animation != null)
                    {
                        TrackEntry       currentEntry = state.GetCurrent(trackIndex);
                        Spine.TrackEntry trackEntry;
                        float            customMixDuration = clipData.customDuration ? GetCustomMixDuration(clipData) : 0.0f;
                        if (currentEntry == null && customMixDuration > 0)
                        {
                            state.SetEmptyAnimation(trackIndex, 0);                             // ease in requires empty animation
                            trackEntry = state.AddAnimation(trackIndex, clipData.animationReference.Animation, clipData.loop, 0);
                        }
                        else
                        {
                            trackEntry = state.SetAnimation(trackIndex, clipData.animationReference.Animation, clipData.loop);
                        }

                        trackEntry.EventThreshold      = clipData.eventThreshold;
                        trackEntry.DrawOrderThreshold  = clipData.drawOrderThreshold;
                        trackEntry.TrackTime           = (float)clipPlayable.GetTime() * (float)clipPlayable.GetSpeed();
                        trackEntry.TimeScale           = (float)clipPlayable.GetSpeed();
                        trackEntry.AttachmentThreshold = clipData.attachmentThreshold;
                        trackEntry.HoldPrevious        = clipData.holdPrevious;

                        if (clipData.customDuration)
                        {
                            trackEntry.MixDuration = customMixDuration;
                        }

                        timelineStartedTrackEntry = trackEntry;
                    }
                    //else Debug.LogWarningFormat("Animation named '{0}' not found", clipData.animationName);
                }

                // Ensure that the first frame ends with an updated mesh.
                if (skeletonAnimation)
                {
                    skeletonAnimation.Update(0);
                    skeletonAnimation.LateUpdate();
                }
                else if (skeletonGraphic)
                {
                    skeletonGraphic.Update(0);
                    skeletonGraphic.LateUpdate();
                }
            }
            startingClips[0] = startingClips[1] = ScriptPlayable <SpineAnimationStateBehaviour> .Null;
            if (lastAnyClipPlaying && !anyClipPlaying)
            {
                HandleClipEnd();
            }
            this.lastAnyClipPlaying = anyClipPlaying;
        }