Forum rules - please read before posting.

Spine AC Custom Action List - turn off and on my new Objects

Hi Chris,

Are you able to help me create a custom action list where I can call the attachment switch in Spine, where the string param would be the attachment name?

skeletonAnimation.skeleton.SetAttachment("weapon_one_handed", "weapons/grenade_bomb");

Apparently you can also use a component that changes the slot.

Please see screenshots here:

https://drive.google.com/drive/folders/1h1DJo2Iy9GObZD7zEwShzWp0Cw7qcwel?usp=sharing

Comments

  • I have a similar script that you could help me adaptd, where you use a string to change the skin:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionSkinSetter : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Character; }}
            public override string Title { get { return "Set skin"; }}
    
            public bool isPlayer;
            public Char character;
            public int _charID = 0;
            private Char runtimeCharacter;
    
            public string[] newSkinNames = new string[1] { "" };
    
    
            public override void AssignValues()
            {
                if (isPlayer)
                {
                    runtimeCharacter = KickStarter.player;
                }
                else
                {
                    runtimeCharacter = AssignFile<Char> (_charID, character);
                }
            }
    
    
            public override float Run ()
            {
                if (runtimeCharacter)
                {
                    runtimeCharacter.GetComponent<SkinSetter> ().newSkinNames = newSkinNames;
                }
                isRunning = false;
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                isPlayer = EditorGUILayout.Toggle ("Affect Player?", isPlayer);
                if (!isPlayer)
                {
                    character = (Char) EditorGUILayout.ObjectField ("Character:", character, typeof (Char), true);
                    _charID = FieldToID <Char> (character, _charID);
                    character = IDToField <Char> (character, _charID, true);
                }
    
                if (newSkinNames == null)
                {
                    newSkinNames = new string[0];
                }
                int numSkins = newSkinNames.Length;
                numSkins = EditorGUILayout.DelayedIntField ("# of skins:", numSkins);
                if (newSkinNames.Length != numSkins)
                {
                    string[] backup = new string[newSkinNames.Length];
                    for (int i = 0; i < newSkinNames.Length; i++)
                    {
                        backup[i] = newSkinNames[i];
                    }
                    newSkinNames = new string[numSkins];
                    for (int i = 0; i < Mathf.Min (numSkins, backup.Length); i++)
                    {
                        newSkinNames[i] = backup[i];
                    }
                }
    
                for (int i = 0; i < newSkinNames.Length; i++)
                {
                    newSkinNames[i] = EditorGUILayout.TextField ("Skin #" + i, newSkinNames[i]);
                }
            }
    
            #endif
    
        }
    
    }
    
  • using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    using Spine;
    using Spine.Unity;
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionAttachmentSetter : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Character; }}
            public override string Title { get { return "Set attachment"; }}
    
            public bool isPlayer;
            public Char character;
            public int _charID = 0;
            private Char runtimeCharacter;
    
            public string slotName, attachmentName;
    
    
            public override void AssignValues()
            {
                runtimeCharacter = isPlayer ? KickStarter.player : AssignFile<Char> (_charID, character);
            }
    
    
            public override float Run ()
            {
                if (runtimeCharacter == null) return 0f;
    
                SkeletonAnimation skeletonAnimation = runtimeCharacter.GetComponentInChildren<SkeletonAnimation> ();
                if (skeletonAnimation == null) return 0f;
    
                Skeleton skeleton = skeletonAnimation.Skeleton;
                if (skeleton == null) return 0f;
    
                skeleton.SetAttachment (slotName, attachmentName);
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                isPlayer = EditorGUILayout.Toggle ("Affect Player?", isPlayer);
                if (!isPlayer)
                {
                    character = (Char) EditorGUILayout.ObjectField ("Character:", character, typeof (Char), true);
                    _charID = FieldToID <Char> (character, _charID);
                    character = IDToField <Char> (character, _charID, true);
                }
    
                slotName = EditorGUILayout.TextField ("Slot name:", slotName);
                attachmentName = EditorGUILayout.TextField ("Attachment name:", attachmentName);
            }
    
            #endif
    
        }
    
    }
    
  • Awesome! thank you! Is it possible to set parameters for custom action scripts?

  • Hi Chris, apologies for my ignorance but I’m getting confused (and therefore errors with the last part of the tutorial) do I just replace the GUI code in the above script with this?

    override public void ShowGUI (List<ActionParameter> parameters)
    {
    parameterID = Action.ChooseParameterGUI
    ("GameObject to affect:", parameters, parameterID, ParameterType.GameObject);
    if (parameterID >= 0)
    {
    constantID = 0;
    objectToAffect = null;
    }
    else
    {
    objectToAffect = (GameObject) EditorGUILayout.ObjectField
    ("GameObject to affect:", objectToAffect, typeof (GameObject), true);

    constantID = FieldToID (objectToAffect, constantID);
    objectToAffect = IDToField (objectToAffect, constantID, true);
    }
    }
  • No - the exact code you add is based on the fields you want to parameterise.

    The code above is adapted from the previous tutorial.

    To have a string parameter field override e.g. slotName with slotNameParameterID, replace:

    slotName = EditorGUILayout.TextField ("Slot name:", slotName);
    

    with:

    slotNameParameterID = Action.ChooseParameterGUI ("Slot name:", parameters, slotNameParameterID, ParameterType.String);
    if (slotNameParameterID < 0)
        slotName = EditorGUILayout.TextField ("Slot name:", slotName);
    
  • edited February 2023

    getting this error

    Assets/Sleepytime Village/Scripts/Custom Actions5/ActionAttachmentSetter.cs(63,13): error CS0103: The name 'slotNameParameterID' does not exist in the current context

    My code is below

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    using Spine;
    using Spine.Unity;
    using System.Collections.Generic;
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionAttachmentSetter : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Character; }}
            public override string Title { get { return "Set attachment"; }}
    
            public bool isPlayer;
            public Char character;
            public int _charID = 0;
            public int parameterID = -1;
            private Char runtimeCharacter;
    
    
            public string slotName, attachmentName;
    
    
            public override void AssignValues()
            {
                runtimeCharacter = isPlayer ? KickStarter.player : AssignFile<Char> (_charID, character);
            }
    
    
            public override float Run ()
            {
                if (runtimeCharacter == null) return 0f;
    
                SkeletonAnimation skeletonAnimation = runtimeCharacter.GetComponentInChildren<SkeletonAnimation> ();
                if (skeletonAnimation == null) return 0f;
    
                Skeleton skeleton = skeletonAnimation.Skeleton;
                if (skeleton == null) return 0f;
    
                skeleton.SetAttachment (slotName, attachmentName);
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            override public void ShowGUI (List<ActionParameter> parameters)
    
            {
                isPlayer = EditorGUILayout.Toggle ("Affect Player?", isPlayer);
                if (!isPlayer)
                {
                    character = (Char) EditorGUILayout.ObjectField ("Character:", character, typeof (Char), true);
                    _charID = FieldToID <Char> (character, _charID);
                    character = IDToField <Char> (character, _charID, true);
                }
    
                slotNameParameterID = Action.ChooseParameterGUI ("Slot name:", parameters, slotNameParameterID, ParameterType.String);
                if (slotNameParameterID < 0)
                slotName = EditorGUILayout.TextField ("Slot name:", slotName);
                attachmentName = EditorGUILayout.TextField ("Attachment name:", attachmentName);
            }
    
            #endif
    
        }
    
    }
    
  • You need to declare slotNameParameterID as a variable, as covered by the tutorial:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    using Spine;
    using Spine.Unity;
    using System.Collections.Generic;
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionAttachmentSetter : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Character; }}
            public override string Title { get { return "Set attachment"; }}
    
            public bool isPlayer;
            public Char character;
            public int _charID = 0;
            public int parameterID = -1;
            private Char runtimeCharacter;
    
            public string slotName, attachmentName;
            public int slotNameParameterID = -1;
    
    
            public override void AssignValues(List<ActionParameter> parameters)
            {
                runtimeCharacter = isPlayer ? KickStarter.player : AssignFile<Char> (_charID, character);
                slotName = AssignString (parameters, slotNameParameterID, slotName);
            }
    
    
            public override float Run ()
            {
                if (runtimeCharacter == null) return 0f;
    
                SkeletonAnimation skeletonAnimation = runtimeCharacter.GetComponentInChildren<SkeletonAnimation> ();
                if (skeletonAnimation == null) return 0f;
    
                Skeleton skeleton = skeletonAnimation.Skeleton;
                if (skeleton == null) return 0f;
    
                skeleton.SetAttachment (slotName, attachmentName);
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            override public void ShowGUI (List<ActionParameter> parameters)
            {
                isPlayer = EditorGUILayout.Toggle ("Affect Player?", isPlayer);
                if (!isPlayer)
                {
                    character = (Char) EditorGUILayout.ObjectField ("Character:", character, typeof (Char), true);
                    _charID = FieldToID <Char> (character, _charID);
                    character = IDToField <Char> (character, _charID, true);
                }
    
                slotNameParameterID = Action.ChooseParameterGUI ("Slot name:", parameters, slotNameParameterID, ParameterType.String);
                if (slotNameParameterID < 0)
                slotName = EditorGUILayout.TextField ("Slot name:", slotName);
                attachmentName = EditorGUILayout.TextField ("Attachment name:", attachmentName);
            }
    
            #endif
    
        }
    
    }
    
  • Thanks, there is a slight issue with this however, the slot name can stay the same but it is the attachment I need the parameter for, (or ideally both). However, I can only set a parameter for the slot name? Please see screenshots

    https://drive.google.com/drive/folders/1hXwFGCTtYCH_SJXx0kFJxC_Im_d7QlGf?usp=share_link

  • You can parameterise any field in the same way - I'm trying to teach you with an example.

    Make the same changes to your attachment field and you can parameterise that as well.

  • edited February 2023

    I got it! I think? :)

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    using Spine;
    using Spine.Unity;
    using System.Collections.Generic;
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionAttachmentSetter : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Character; }}
            public override string Title { get { return "Set attachment"; }}
    
            public bool isPlayer;
            public Char character;
            public int _charID = 0;
            public int parameterID = -1;
            private Char runtimeCharacter;
    
            public string slotName, attachmentName;
            public int slotNameParameterID = -1;
            public int attachmentNameParameterID = -1;
    
    
    
            public override void AssignValues(List<ActionParameter> parameters)
            {
                runtimeCharacter = isPlayer ? KickStarter.player : AssignFile<Char> (_charID, character);
                slotName = AssignString (parameters, slotNameParameterID, slotName);
            }
    
    
            public override float Run ()
            {
                if (runtimeCharacter == null) return 0f;
    
                SkeletonAnimation skeletonAnimation = runtimeCharacter.GetComponentInChildren<SkeletonAnimation> ();
                if (skeletonAnimation == null) return 0f;
    
                Skeleton skeleton = skeletonAnimation.Skeleton;
                if (skeleton == null) return 0f;
    
                skeleton.SetAttachment (slotName, attachmentName);
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            override public void ShowGUI (List<ActionParameter> parameters)
            {
                isPlayer = EditorGUILayout.Toggle ("Affect Player?", isPlayer);
                if (!isPlayer)
                {
                    character = (Char) EditorGUILayout.ObjectField ("Character:", character, typeof (Char), true);
                    _charID = FieldToID <Char> (character, _charID);
                    character = IDToField <Char> (character, _charID, true);
                }
    
                slotNameParameterID = Action.ChooseParameterGUI ("Slot name:", parameters, slotNameParameterID, ParameterType.String);
                attachmentNameParameterID = Action.ChooseParameterGUI ("Attachment name:", parameters, slotNameParameterID, ParameterType.String);
    
                if (slotNameParameterID < 0)
                slotName = EditorGUILayout.TextField ("Slot name:", slotName);
                if (attachmentNameParameterID < 0)
                attachmentName = EditorGUILayout.TextField ("Attachment name:", attachmentName);
            }
    
            #endif
    
        }
    
    }
    
  • Add this to the AssignValues function:

    attachmentName = AssignString (parameters, attachmentNameParameterID, attachmentName);
    
  • Popped that line in, but there must be something missing with my script overall, It only lets me put one parameter in for both, se screenshot:

    https://drive.google.com/drive/folders/1hXwFGCTtYCH_SJXx0kFJxC_Im_d7QlGf?usp=share_link

  • Replace:

    attachmentNameParameterID = Action.ChooseParameterGUI ("Attachment name:", parameters, slotNameParameterID, ParameterType.String);
    

    with:

    attachmentNameParameterID = Action.ChooseParameterGUI ("Attachment name:", parameters, attachmentNameParameterID, ParameterType.String);
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.