示例#1
0
    public static PlayAnimation SetAnimation(Rect currentAnimationBorder, Color animationColour, Texture2D givenTexture)
    {
        PlayAnimation output = new PlayAnimation();

        output.Initialise();
        Rect  currentFrameBorder = new Rect(currentAnimationBorder.position, new Vector2(0, currentAnimationBorder.size.y));
        Color pixelColour        = transparent;

        for (int j = 0; j < currentAnimationBorder.size.x; j++)
        {
            pixelColour = givenTexture.GetPixel(j, (int)currentFrameBorder.y);
            if (animationColour != pixelColour)
            {
                if (pixelColour == transparent)
                {
                    if (currentFrameBorder.size.x < 1)
                    {
                        currentFrameBorder.size = new Vector2(j, currentFrameBorder.size.y);
                    }
                    Rect imageBorder = new Rect(new Vector2(currentFrameBorder.x + 1, currentFrameBorder.y + 1),
                                                new Vector2(currentFrameBorder.size.x - 2, currentFrameBorder.size.y - 2));
                    output.AddFrame(givenTexture, imageBorder);
                    j += XGAP - 1;
                    currentFrameBorder.x = j + 1;
                }
            }
            else if (j == currentAnimationBorder.size.x - 1)
            {
                if (animationColour != pixelColour && pixelColour != transparent)
                {
                    throw new TextureNotfoundException(givenTexture.name + " didn't find any textures to make a frame from");
                }
                if (currentFrameBorder.size.x < 1)
                {
                    currentFrameBorder.size = new Vector2(j, currentAnimationBorder.size.y);
                    currentFrameBorder      = new Rect(new Vector2(currentFrameBorder.x + 1, currentFrameBorder.y + 1),
                                                       new Vector2(currentFrameBorder.size.x - 1, currentFrameBorder.size.y - 1));
                }
                output.AddFrame(givenTexture, currentFrameBorder);
                j += XGAP - 1;
                currentFrameBorder.x = j + 1;
            }
            else
            {
                if (currentFrameBorder.size.x > 2)
                {
                    //Debug.Log("skip");
                    j += (int)currentFrameBorder.size.x - 2;
                }
            }
        }
        return(output);
    }
    public static PlayAnimation SetAnimation(Rect currentAnimationBorder, Color animationColour, Texture2D givenTexture)
    {
        PlayAnimation output = new PlayAnimation();

        output.Initialise();
        Rect  currentFrameBorder = new Rect(new Vector2(0, currentAnimationBorder.y), new Vector2(0, currentAnimationBorder.size.y));
        Color pixelColour        = transparent;

        for (int i = 0; i <= currentAnimationBorder.size.x; i++)
        {
            Color currentPixel = givenTexture.GetPixel(i, (int)currentFrameBorder.y);
            if (currentPixel.a == 0)
            {
                if (i - currentFrameBorder.x > 0)
                {
                    Rect imageBorder = new Rect(new Vector2(currentFrameBorder.position.x + 1, currentFrameBorder.y + 1),
                                                new Vector2(i - currentFrameBorder.x - 2, currentFrameBorder.size.y - 1));
                    output.AddFrame(givenTexture, imageBorder);
                }
                while (givenTexture.GetPixel(i, (int)currentFrameBorder.y).a == 0)
                {
                    i++;
                    if (i >= currentAnimationBorder.size.x)
                    {
                        break;
                    }
                }
                currentFrameBorder.x = i;
            }
            else if (i == currentAnimationBorder.size.x)
            {
                if (animationColour != pixelColour && pixelColour.a != 0)
                {
                    throw new MalformedTextureException(givenTexture.name + " doesn't terminate appropiately");
                }
                Rect imageBorder = new Rect(new Vector2(currentFrameBorder.position.x, currentFrameBorder.y),
                                            new Vector2(i - currentFrameBorder.x - 2, currentFrameBorder.size.y - 1));
                output.AddFrame(givenTexture, imageBorder);
            }
        }
        return(output);
    }