Forum rules - please read before posting.

Custom action works in Unity but not in the build

edited November 2020 in Technical Q&A

Hey! I use the Powersprite animator asset in unity and I wrote a custom action that I use to play those animations in my project. It calls the Play function of script on an object. I made it so the object to animate and the animation clip to play can both be parameters. It works perfectly in engine, but when I make a build, and the object to animate is a parameter, it bugs out, the actionlist seems to get stuck at that action. Here is the code in case you can find some error:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections.Generic;
    using PowerTools;
    #if UNITY_EDITOR
    using UnityEditor;

     #endif

    namespace AC
    {

[System.Serializable]
public class ActionPlaySpriteAnim : Action
{

    public GameObject objecttoaffect;
    public AnimationClip anim;

    public int parameterID = -1;  

    public int constantID;

    SpriteAnim m_anim = null;

    public ActionPlaySpriteAnim()
    {
        this.isDisplayed = true;
        category = ActionCategory.Custom;
        title = "Play Sprite Animation";
        description = "Play animation in Sprite Anim Component";
    }

    public override float Run ()
    {       
        if (!isRunning)
        {
            isRunning = true;
            DoAnim(anim);
            if (willWait)
            {
                float WaitTime;
                WaitTime = anim.length;
                return WaitTime;
            }
        }
        else
        {
            isRunning = false;
        }
        return 0f;
    }

    protected void DoAnim(AnimationClip animclip)
    {
        m_anim = objecttoaffect.GetComponent<SpriteAnim>();
        m_anim.Play(animclip);
        }

    public override void Skip ()
    {   
         Run ();
    }

    #if UNITY_EDITOR

    public override void ShowGUI (List<ActionParameter> parameters)
    {
        // Action-specific Inspector GUI code here
        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);
        }

       anim = (AnimationClip)EditorGUILayout.ObjectField
               ("Animation To Play:", anim, typeof(AnimationClip), true);


        constantID = FieldToID(objecttoaffect, constantID);
        objecttoaffect = IDToField(objecttoaffect, constantID, true);
        willWait = EditorGUILayout.Toggle("Wait until finish?", willWait);


        AfterRunningOption ();
    }
    override public void AssignValues(List<ActionParameter> parameters)
    {
        objecttoaffect = AssignFile(parameters, parameterID, constantID, objecttoaffect);
    }
    public override string SetLabel ()
    {
        // (Optional) Return a string used to describe the specific action's job.

        return string.Empty;
    }

    #endif
}

   }

Comments

  • Your AssignValues function needs to be outside of the UNITY_EDITOR check - since this is necessary when converting Parameter / Constant ID values to GameObjects at the time the Action is run.

  • Yeah I forgot about the unity editor check, thank you!!

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.