سؤال

I have a Unity football game (2D top down view) that lags when the player (user team) gets the ball. I animate the player using Texture2d [] frames. I have 48 frames of images. Does reducding the frames to 32 frames make the game faster?

This is how I animate,

void Update () {
    if (players.moving == true)
    {
        if (timer > 0.0f)
            timer -= Time.deltaTime;
        if (timer <= 0.0f)
            timer = 0.0f;
        if (timer == 0.0f)
            NextFrame();
    }
    else
    {
        if (players.movingUp == true)
            renderer.material.mainTexture = frames[0]; }

public void NextFrame()
{
    if (!play)
        return;
    //test velocity direction
    if (players.movingUp == true)
    {
        if (northFrame >= 0 && northFrame <= 2)
        {
            northFrame ++;
            renderer.material.mainTexture = frames[northFrame];
        }
        else
        {
            northFrame = 0;
            renderer.material.mainTexture = frames[northFrame];
        }
        //renderer.material.mainTexture = frames[northFrame];
        //Debug.Log ("North frame: " + northFrame);
    }
}

This is my user player update function.

if (players[0].GetComponent<TPCCS>().highlight == true)
    {
        ring1.renderer.enabled = true;
        ring2.renderer.enabled = false;
        ring3.renderer.enabled = false;
        ring4.renderer.enabled = false;
    }

    if (players[1].GetComponent<TPCCS>().highlight == true)
    {
        ring1.renderer.enabled = false;
        ring2.renderer.enabled = true;
        ring3.renderer.enabled = false;
        ring4.renderer.enabled = false;
    }

    if (players[2].GetComponent <TPCCS>().highlight == true)
    {
        ring1.renderer.enabled = false;
        ring2.renderer.enabled = false;
        ring3.renderer.enabled = false;
        ring4.renderer.enabled = true;
    }
    rigidbody.freezeRotation = true;
هل كانت مفيدة؟

المحلول

Whether or not reducing the frames will make it faster is for you to test. My guess is yes, but it's still not how I would approach it. Ideally you don't want to have individual textures at all.

Switching textures is costly. You don't want to be doing that for each frame. What you might want to have a look at is combining all your frames into an atlas (or sprite sheet). That is, a single large texture, which contains all your smaller textures. Something you could do with TexturePacker for example.

Then instead of switching textures to animate, you "animate" your texture coordinates (or the texture offset for your material) to select the correct part of the bigger atlas to be displayed. Like that you're not switching textures at all and performance should improve significantly.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top