public void PreviewEditModePose(Playable playable, SkeletonAnimation spineComponent)
 {
     if (!Application.isPlaying && (spineComponent != null))
     {
         int inputCount = playable.GetInputCount <Playable>();
         int inputPort  = -1;
         for (int i = 0; i < inputCount; i++)
         {
             if (playable.GetInputWeight <Playable>(i) >= 1f)
             {
                 inputPort = i;
             }
         }
         if (inputPort != -1)
         {
             ScriptPlayable <SpineAnimationStateBehaviour> input = (ScriptPlayable <SpineAnimationStateBehaviour>)playable.GetInput <Playable>(inputPort);
             SpineAnimationStateBehaviour behaviour = input.GetBehaviour();
             Skeleton skeleton = spineComponent.Skeleton;
             if ((behaviour.animationReference != null) && (spineComponent.SkeletonDataAsset.GetSkeletonData(true) != behaviour.animationReference.SkeletonDataAsset.GetSkeletonData(true)))
             {
                 object[] args = new object[] { spineComponent.SkeletonDataAsset, behaviour.animationReference.SkeletonDataAsset };
                 Debug.LogWarningFormat("SpineAnimationStateMixerBehaviour tried to apply an animation for the wrong skeleton. Expected {0}. Was {1}", args);
             }
             Spine.Animation from = null;
             float           time = 0f;
             bool            loop = false;
             if ((inputPort != 0) && (inputCount > 1))
             {
                 ScriptPlayable <SpineAnimationStateBehaviour> playable3 = (ScriptPlayable <SpineAnimationStateBehaviour>)playable.GetInput <Playable>((inputPort - 1));
                 SpineAnimationStateBehaviour behaviour2 = playable3.GetBehaviour();
                 from = behaviour2.animationReference.Animation;
                 time = (float)playable3.GetTime <ScriptPlayable <SpineAnimationStateBehaviour> >();
                 loop = behaviour2.loop;
             }
             Spine.Animation animation   = behaviour.animationReference.Animation;
             float           num6        = (float)input.GetTime <ScriptPlayable <SpineAnimationStateBehaviour> >();
             float           mixDuration = behaviour.mixDuration;
             if (!behaviour.customDuration && (from != null))
             {
                 mixDuration = spineComponent.AnimationState.Data.GetMix(from, animation);
             }
             if (((from != null) && (mixDuration > 0f)) && (num6 < mixDuration))
             {
                 skeleton.SetToSetupPose();
                 float alpha = 1f - (num6 / mixDuration);
                 alpha = (alpha <= 0.5f) ? (alpha * 2f) : 1f;
                 from.Apply(skeleton, 0f, time, loop, null, alpha, MixPose.Setup, MixDirection.Out);
                 animation.Apply(skeleton, 0f, num6, behaviour.loop, null, num6 / mixDuration, MixPose.Current, MixDirection.In);
             }
             else
             {
                 skeleton.SetToSetupPose();
                 animation.PoseSkeleton(skeleton, num6, behaviour.loop);
             }
         }
     }
 }
Ejemplo n.º 2
0
        protected void TrySetAnimation(Object o, bool multi)
        {
            var skeletonAnimation = o as SkeletonAnimation;

            if (skeletonAnimation == null)
            {
                return;
            }
            if (!skeletonAnimation.valid)
            {
                return;
            }

            if (!isInspectingPrefab)
            {
                if (wasAnimationNameChanged)
                {
                    if (!Application.isPlaying)
                    {
                        if (skeletonAnimation.state != null)
                        {
                            skeletonAnimation.state.ClearTrack(0);
                        }
                        skeletonAnimation.skeleton.SetToSetupPose();
                    }

                    Spine.Animation animationToUse = skeletonAnimation.skeleton.Data.FindAnimation(animationName.stringValue);

                    if (!Application.isPlaying)
                    {
                        if (animationToUse != null)
                        {
                            animationToUse.PoseSkeleton(skeletonAnimation.Skeleton, 0f);
                        }
                        skeletonAnimation.Update(0);
                        skeletonAnimation.LateUpdate();
                        requireRepaint = true;
                    }
                    else
                    {
                        if (animationToUse != null)
                        {
                            skeletonAnimation.state.SetAnimation(0, animationToUse, loop.boolValue);
                        }
                        else
                        {
                            skeletonAnimation.state.ClearTrack(0);
                        }
                    }

                    wasAnimationNameChanged = false;
                }

                // Reflect animationName serialized property in the inspector even if SetAnimation API was used.
                if (!multi && Application.isPlaying)
                {
                    TrackEntry current = skeletonAnimation.state.GetCurrent(0);
                    if (current != null)
                    {
                        if (skeletonAnimation.AnimationName != animationName.stringValue)
                        {
                            animationName.stringValue = current.Animation.Name;
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
		static void BakeBoneConstraints (Bone bone, Spine.Animation animation, AnimationClip clip) {
			Skeleton skeleton = bone.Skeleton;
			bool inheritRotation = bone.Data.TransformMode.InheritsRotation();

			animation.PoseSkeleton(skeleton, 0);
			skeleton.UpdateWorldTransform();
			float duration = animation.Duration;

			AnimationCurve curve = new AnimationCurve();

			List<Keyframe> keys = new List<Keyframe>();

			float rotation = bone.AppliedRotation;
			if (!inheritRotation)
				rotation = GetUninheritedAppliedRotation(bone);

			keys.Add(new Keyframe(0, rotation, 0, 0));

			int listIndex = 1;

			float r = rotation;

			int steps = Mathf.CeilToInt(duration / BakeIncrement);

			float currentTime = 0;
			float angle = rotation;

			for (int i = 1; i <= steps; i++) {
				currentTime += BakeIncrement;
				if (i == steps)
					currentTime = duration;

				animation.PoseSkeleton(skeleton, currentTime, true);
				skeleton.UpdateWorldTransform();

				int pIndex = listIndex - 1;

				Keyframe pk = keys[pIndex];

				pk = keys[pIndex];

				rotation = inheritRotation ? bone.AppliedRotation : GetUninheritedAppliedRotation(bone);

				angle += Mathf.DeltaAngle(angle, rotation);

				r = angle;

				float rOut = (r - pk.value) / (currentTime - pk.time);

				pk.outTangent = rOut;

				keys.Add(new Keyframe(currentTime, r, rOut, 0));

				keys[pIndex] = pk;

				listIndex++;
			}

			curve = EnsureCurveKeyCount(new AnimationCurve(keys.ToArray()));

			string path = GetPath(bone.Data);
			string propertyName = "localEulerAnglesBaked";

			EditorCurveBinding xBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".x");
			AnimationUtility.SetEditorCurve(clip, xBind, new AnimationCurve());
			EditorCurveBinding yBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".y");
			AnimationUtility.SetEditorCurve(clip, yBind, new AnimationCurve());
			EditorCurveBinding zBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".z");
			AnimationUtility.SetEditorCurve(clip, zBind, curve);
		}