Bob5421
Bob5421

Reputation: 9073

unity 3d sprite management for 2d game

I am writing a very basic 2d game with unity 3d.

I have some 2d objets which can have multiple states. For example, imagine Mario: You have small mario, big mario, mario and yoshi. And each mario's state has different sub states (mario running, etc.)

This is exactly what i want to do. So i designed each state in a png file. I have about 50 png files for each object.

Now, i want do integrate this states in my unity project.

I have created a prefab for each object. Each prefab contains its 50 png files. I also have a C# component attached to the prefab object. My C# component contains 50 public GameObject properties. I have linked this properties to the png files. Then, my C# code tells each png file should be visible or not.

For example:

    small_mario_running_1.SetActive(false);
    small_mario_running_2.SetActive(true);

    small_mario_running_1.SetActive(false);
    small_mario_running_2.SetActive(false);

Please note mario is just an example.

I have a lot of SetActive calls so i am wondering if there is a best way to do that with unity.

Thanks

Upvotes: 1

Views: 206

Answers (1)

Stephen Docy
Stephen Docy

Reputation: 4788

I'm not sure what you mean by Each prefab contains its 50 png files.

I have only done basic Unity graphics so far, so I'm sure someone here can come up with a more elegant idea, but as a first approach, I would create sprites out of you png files and use 50 public Sprite variables in your C# script, linked to each sprite. Then dynamically set the Sprite for the Sprite Renderer on your mario. Assuming your prefab is using a Sprite Renderer.

Something like

public Sprite running;
public Sprite bigMario;

if (isRunning)
     GetComponent(SpriteRenderer).sprite = running;
else if (isBig)
     GetComponent(SpriteRenderer).sprite = bigMario;

Upvotes: 1

Related Questions