// Sets an animation for this instance with a frame timer
    public void SetAnimation(string[] spriteNames, float frametime)
    {
        Vector2 pivot = img.GetComponent <RectTransform>().pivot;

        Keyframe[] kfArray = new Keyframe[spriteNames.Length];
        for (int i = 0; i < spriteNames.Length; i++)
        {
            kfArray[i] = new Keyframe(SpriteRegistry.Get(spriteNames[i]), spriteNames[i].ToLower());
        }
        if (keyframes == null)
        {
            if (img.GetComponent <KeyframeCollection>())
            {
                keyframes         = img.GetComponent <KeyframeCollection>();
                keyframes.enabled = true;
            }
            else
            {
                keyframes     = img.AddComponent <KeyframeCollection>();
                keyframes.spr = this;
            }
        }
        else
        {
            keyframes.enabled = true;
        }
        keyframes.loop = loop;
        keyframes.Set(kfArray, frametime);
        UpdateAnimation();
        img.GetComponent <RectTransform>().pivot = pivot;
    }
    public void SetAnimation(string[] spriteNames, float frametime)
    {
        Keyframe[] kfArray = new Keyframe[spriteNames.Length];
        for (int i = 0; i < spriteNames.Length; i++)
        {
            kfArray[i] = new Keyframe(SpriteRegistry.Get(spriteNames[i]));
        }
        if (keyframes == null)
        {
            keyframes     = img.gameObject.AddComponent <KeyframeCollection>();
            keyframes.spr = this;
        }
        else
        {
            keyframes.enabled = true;
        }

        keyframes.Set(kfArray, frametime);
    }
Exemple #3
0
    // Sets an animation for this instance with a frame timer
    public void SetAnimation(string[] spriteNames, float frametime, string prefix = "")
    {
        if (spriteNames == null)
        {
            throw new CYFException("sprite.SetAnimation: The first argument (list of images) is nil.\n\nSee the documentation for proper usage.");
        }
        if (spriteNames.Length == 0)
        {
            throw new CYFException("sprite.SetAnimation: No sequence of animations was provided (animation table is empty).");
        }
        if (frametime < 0)
        {
            throw new CYFException("sprite.SetAnimation: An animation can not have negative speed!");
        }
        if (frametime == 0)
        {
            throw new CYFException("sprite.SetAnimation: An animation can not play at 0 frames per second!");
        }

        if (prefix != "")
        {
            while (prefix.StartsWith("/"))
            {
                prefix = prefix.Substring(1);
            }

            if (!prefix.EndsWith("/"))
            {
                prefix += "/";
            }

            for (int i = 0; i < spriteNames.Length; i++)
            {
                spriteNames[i] = prefix + spriteNames[i];
            }
        }

        Vector2 pivot = img.GetComponent <RectTransform>().pivot;

        Keyframe[] kfArray = new Keyframe[spriteNames.Length];
        for (int i = 0; i < spriteNames.Length; i++)
        {
            // at least one sprite in the sequence was unable to be loaded
            if (SpriteRegistry.Get(spriteNames[i]) == null)
            {
                throw new CYFException("sprite.SetAnimation: Failed to load sprite with the name \"" + spriteNames[i] + "\". Are you sure it is spelled correctly?");
            }

            kfArray[i] = new Keyframe(SpriteRegistry.Get(spriteNames[i]), spriteNames[i].ToLower());
        }
        if (keyframes == null)
        {
            if (img.GetComponent <KeyframeCollection>())
            {
                keyframes         = img.GetComponent <KeyframeCollection>();
                keyframes.enabled = true;
            }
            else
            {
                keyframes     = img.AddComponent <KeyframeCollection>();
                keyframes.spr = this;
            }
        }
        else
        {
            keyframes.enabled = true;
        }
        keyframes.loop = loop;
        keyframes.Set(kfArray, frametime);
        UpdateAnimation();
        img.GetComponent <RectTransform>().pivot = pivot;
    }