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;
            }
        }
        public override void OnEnter()
        {
            var go = Fsm.GetOwnerDefaultTarget(spineGameObject);

            if (go != null)
            {
                component = component != null ? component : go.GetComponent <ISkeletonComponent>();
                if (component != null && !flip.IsNone)
                {
                    var skeleton = component.Skeleton;

                    switch (flipAxis)
                    {
                    case SkeletonAxis2D.X:
                        skeleton.flipX = flip.Value;
                        break;

                    case SkeletonAxis2D.Y:
                        skeleton.flipY = flip.Value;
                        break;
                    }
                }
            }
            Finish();
        }
Beispiel #3
0
        public void Attach()
        {
            ISkeletonComponent component        = base.GetComponent <ISkeletonComponent>();
            SkeletonRenderer   skeletonRenderer = component as SkeletonRenderer;

            if (skeletonRenderer != null)
            {
                this.applyPMA = skeletonRenderer.pmaVertexColors;
            }
            else
            {
                SkeletonGraphic skeletonGraphic = component as SkeletonGraphic;
                if (skeletonGraphic != null)
                {
                    this.applyPMA = skeletonGraphic.SpineMeshGenerator.PremultiplyVertexColors;
                }
            }
            Shader shader = (!this.applyPMA) ? Shader.Find("Sprites/Default") : Shader.Find("Spine/Skeleton");

            this.loader = (this.loader ?? new SpriteAttachmentLoader(this.sprite, shader, this.applyPMA));
            if (this.attachment == null)
            {
                this.attachment = this.loader.NewRegionAttachment(null, this.sprite.name, string.Empty);
            }
            component.Skeleton.FindSlot(this.slot).Attachment = this.attachment;
            if (!this.keepLoaderInMemory)
            {
                this.loader = null;
            }
        }
 public void Initialize(bool overwrite = true)
 {
     if (overwrite || (this.attachment == null))
     {
         ISkeletonComponent component = base.GetComponent <ISkeletonComponent>();
         SkeletonRenderer   renderer  = component as SkeletonRenderer;
         if (renderer != null)
         {
             this.applyPMA = renderer.pmaVertexColors;
         }
         else
         {
             SkeletonGraphic graphic = component as SkeletonGraphic;
             if (graphic != null)
             {
                 this.applyPMA = graphic.MeshGenerator.settings.pmaVertexColors;
             }
         }
         if (this.overrideAnimation)
         {
             ISkeletonAnimation animation = component as ISkeletonAnimation;
             if (animation != null)
             {
                 animation.UpdateComplete -= new UpdateBonesDelegate(this.AnimationOverrideSpriteAttach);
                 animation.UpdateComplete += new UpdateBonesDelegate(this.AnimationOverrideSpriteAttach);
             }
         }
         if (this.spineSlot == null)
         {
         }
         this.spineSlot = component.Skeleton.FindSlot(this.slot);
         Shader shader = !this.applyPMA ? Shader.Find("Sprites/Default") : Shader.Find("Spine/Skeleton");
         this.attachment = !this.applyPMA ? this.sprite.ToRegionAttachment(GetPageFor(this.sprite.texture, shader), 0f) : this.sprite.ToRegionAttachmentPMAClone(shader, TextureFormat.RGBA32, false, null, 0f);
     }
 }
        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);
                    }
                }
            }
        }
Beispiel #6
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType != SerializedPropertyType.String)
            {
                EditorGUI.LabelField(position, "ERROR:", "May only apply to type string");
                return;
            }

            var dataProperty = property.serializedObject.FindProperty(TargetAttribute.dataField);

            if (dataProperty != null)
            {
                if (dataProperty.objectReferenceValue is SkeletonDataAsset)
                {
                    skeletonDataAsset = (SkeletonDataAsset)dataProperty.objectReferenceValue;
                }
                else if (dataProperty.objectReferenceValue is ISkeletonComponent)
                {
                    var skeletonComponent = (ISkeletonComponent)dataProperty.objectReferenceValue;
                    if (skeletonComponent != null)
                    {
                        skeletonDataAsset = skeletonComponent.SkeletonDataAsset;
                    }
                }
                else
                {
                    EditorGUI.LabelField(position, "ERROR:", "Invalid reference type");
                    return;
                }
            }
            else if (property.serializedObject.targetObject is Component)
            {
                var component = (Component)property.serializedObject.targetObject;
                ISkeletonComponent skeletonComponent = component.GetComponentInChildren(typeof(ISkeletonComponent)) as ISkeletonComponent;
                if (skeletonComponent != null)
                {
                    skeletonDataAsset = skeletonComponent.SkeletonDataAsset;
                }
            }

            if (skeletonDataAsset == null)
            {
                EditorGUI.LabelField(position, "ERROR:", "Must have reference to a SkeletonDataAsset");
                return;
            }

            position = EditorGUI.PrefixLabel(position, label);

            var propertyStringValue = property.stringValue;

            if (GUI.Button(position, string.IsNullOrEmpty(propertyStringValue) ? NoneLabel : propertyStringValue, EditorStyles.popup))
            {
                Selector(property);
            }
        }
        protected virtual void Start()
        {
            skeletonComponent = GetComponent <ISkeletonComponent>();
            GatherTopLevelBones();
            SetRootMotionBone(rootMotionBoneName);

            var skeletonAnimation = skeletonComponent as ISkeletonAnimation;

            if (skeletonAnimation != null)
            {
                skeletonAnimation.UpdateLocal += HandleUpdateLocal;
            }
        }
Beispiel #8
0
        void OnEnable()
        {
            if (skeletonRenderer == null)
            {
                skeletonRenderer = GetComponent <SkeletonRenderer>();
            }
            if (skeletonGraphic == null)
            {
                skeletonGraphic = GetComponent <SkeletonGraphic>();
            }
            if (skeletonAnimation == null)
            {
                skeletonAnimation = skeletonRenderer != null?skeletonRenderer.GetComponent <ISkeletonAnimation>() :
                                        skeletonGraphic != null?skeletonGraphic.GetComponent <ISkeletonAnimation>() :
                                            GetComponent <ISkeletonAnimation>();
            }
            if (skeletonComponent == null)
            {
                skeletonComponent = skeletonRenderer != null?skeletonRenderer.GetComponent <ISkeletonComponent>() :
                                        skeletonGraphic != null?skeletonGraphic.GetComponent <ISkeletonComponent>() :
                                            GetComponent <ISkeletonComponent>();
            }

            if (skeletonRenderer != null)
            {
                skeletonRenderer.OnRebuild -= HandleRendererReset;
                skeletonRenderer.OnRebuild += HandleRendererReset;
            }
            else if (skeletonGraphic != null)
            {
                skeletonGraphic.OnRebuild -= HandleRendererReset;
                skeletonGraphic.OnRebuild += HandleRendererReset;
                canvas = skeletonGraphic.canvas;
                if (canvas == null)
                {
                    canvas = skeletonGraphic.GetComponentInParent <Canvas>();
                }
                if (canvas == null)
                {
                    positionScale = 100.0f;
                }
            }

            if (skeletonAnimation != null)
            {
                skeletonAnimation.UpdateLocal -= UpdateLocal;
                skeletonAnimation.UpdateLocal += UpdateLocal;
            }

            CollectBones();
        }
Beispiel #9
0
        private void ApplySettings()
        {
            ISkeletonComponent component = base.GetComponent <ISkeletonComponent>();

            if (component != null)
            {
                Skeleton skeleton = component.Skeleton;
                skeleton.SetColor(this.skeletonColor);
                foreach (SlotSettings settings in this.slotSettings)
                {
                    Slot slot = skeleton.FindSlot(settings.slot);
                    if (slot != null)
                    {
                        slot.SetColor(settings.color);
                    }
                }
            }
        }
Beispiel #10
0
        protected virtual void Start()
        {
            skeletonComponent = GetComponent <ISkeletonComponent>();
            GatherTopLevelBones();
            SetRootMotionBone(rootMotionBoneName);
            if (rootMotionBone != null)
            {
                initialOffset = new Vector2(rootMotionBone.X, rootMotionBone.Y);
            }

            var skeletonAnimation = skeletonComponent as ISkeletonAnimation;

            if (skeletonAnimation != null)
            {
                skeletonAnimation.UpdateLocal -= HandleUpdateLocal;
                skeletonAnimation.UpdateLocal += HandleUpdateLocal;
            }
        }
 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())
                     {
Beispiel #12
0
        protected override void PopulateMenu(GenericMenu menu, SerializedProperty property, SpineAttachment targetAttribute, SkeletonData data)
        {
            ISkeletonComponent skeletonComponent = GetTargetSkeletonComponent(property);
            var validSkins = new List <Skin>();



            if (skeletonComponent != null && targetAttribute.currentSkinOnly)
            {
                Skin currentSkin = null;

                var skinProperty = property.FindPropertyRelative(targetAttribute.skinField);
                if (skinProperty != null)
                {
                    currentSkin = skeletonComponent.Skeleton.Data.FindSkin(skinProperty.stringValue);
                }

                currentSkin = currentSkin ?? skeletonComponent.Skeleton.Skin;
                if (currentSkin != null)
                {
                    validSkins.Add(currentSkin);
                }
                else
                {
                    validSkins.Add(data.Skins.Items[0]);
                }
            }
            else
            {
                foreach (Skin skin in data.Skins)
                {
                    if (skin != null)
                    {
                        validSkins.Add(skin);
                    }
                }
            }

            var    attachmentNames  = new List <string>();
            var    placeholderNames = new List <string>();
            string prefix           = "";

            if (skeletonComponent != null && targetAttribute.currentSkinOnly)
            {
                menu.AddDisabledItem(new GUIContent((skeletonComponent as Component).gameObject.name + " (Skeleton)"));
            }
            else
            {
                menu.AddDisabledItem(new GUIContent(skeletonDataAsset.name));
            }

            menu.AddSeparator("");
            if (TargetAttribute.includeNone)
            {
                const string NullAttachmentName = "";
                menu.AddItem(new GUIContent("Null"), property.stringValue == NullAttachmentName, HandleSelect, new SpineDrawerValuePair(NullAttachmentName, property));
                menu.AddSeparator("");
            }

            Skin defaultSkin  = data.Skins.Items[0];
            var  slotProperty = property.FindBaseOrSiblingProperty(TargetAttribute.slotField);

            string slotMatch = "";

            if (slotProperty != null)
            {
                if (slotProperty.propertyType == SerializedPropertyType.String)
                {
                    slotMatch = slotProperty.stringValue.ToLower();
                }
            }

            foreach (Skin skin in validSkins)
            {
                string skinPrefix = skin.Name + "/";

                if (validSkins.Count > 1)
                {
                    prefix = skinPrefix;
                }

                for (int i = 0; i < data.Slots.Count; i++)
                {
                    if (slotMatch.Length > 0 && !(data.Slots.Items[i].Name.Equals(slotMatch, StringComparison.OrdinalIgnoreCase)))
                    {
                        continue;
                    }

                    attachmentNames.Clear();
                    placeholderNames.Clear();

                    skin.FindNamesForSlot(i, attachmentNames);
                    if (skin != defaultSkin)
                    {
                        defaultSkin.FindNamesForSlot(i, attachmentNames);
                        skin.FindNamesForSlot(i, placeholderNames);
                    }

                    for (int a = 0; a < attachmentNames.Count; a++)
                    {
                        string attachmentPath = attachmentNames[a];
                        string menuPath       = prefix + data.Slots.Items[i].Name + "/" + attachmentPath;
                        string name           = attachmentNames[a];

                        if (targetAttribute.returnAttachmentPath)
                        {
                            name = skin.Name + "/" + data.Slots.Items[i].Name + "/" + attachmentPath;
                        }

                        if (targetAttribute.placeholdersOnly && !placeholderNames.Contains(attachmentPath))
                        {
                            menu.AddDisabledItem(new GUIContent(menuPath));
                        }
                        else
                        {
                            menu.AddItem(new GUIContent(menuPath), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property));
                        }
                    }
                }
            }
        }
        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.
        }
 private void Start()
 {
     jointsNormalX     = activeOnNormalX.GetComponentsInChildren <HingeJoint2D>();
     jointsFlippedX    = activeOnFlippedX.GetComponentsInChildren <HingeJoint2D>();
     skeletonComponent = skeletonRenderer != null ? (ISkeletonComponent)skeletonRenderer : (ISkeletonComponent)skeletonGraphic;
 }