1
\$\begingroup\$

I have different types of images for different sprites. and i am not certain that how much different sprites(images) i will have to show. So, i gotta create the sprites and apply textures programatically at runtime.

Now, I defiantly can't use prefabs because it will restrict me with the number of different sprites i can use. and also, changing texture on one sprite prefab instance in game, will change all the sprites prefab, that's not acceptable in my case.

Is there a way i can create sprites without having to create static prefab ?

where ever i looked for the solution every time i got the same answer "create a prefab", which is what can not be done in my case.

\$\endgroup\$
2
\$\begingroup\$

Your question is very unclear.

You can use prefabs if you copy from them into your sprites instead of referencing. This way sprites you get will be independent from prefabs after creation, so you can change prefabs and sprites will remain as they were made.

\$\endgroup\$
0
\$\begingroup\$

You can change to texture that the sprite is showing in code by doing something like

public Texture2D texture;

Sprite.texture = texture;

If you want to know everything about sprites please check out this link:http://docs.unity3d.com/ScriptReference/Sprite.html

\$\endgroup\$
0
\$\begingroup\$

What I did in a game (there's slot that displays a sprite that depends on the "element" chosen) is:

  1. Set a sprite renderer component on the prefab.
  2. Create sprites (not textures) in Resources/Sprites/Elements
  3. Set a script component on the prefab
  4. Define an private attribute "Sprite[] sprites"
  5. In the Awake method use "sprites = Resources.LoadAll ("Sprites/Elements");"
  6. When I change the element of the player I use gameObject.GetComponent().sprite = sprites[elementIndex] to update the image

You may need some aditional logic to be sure that you're setting the correct sprite from the array. You can use the name property of the sprites for this.

EDIT: I know that you said you don't want to create sprites, but maybe you're just avoiding them because you couldn't find a way to use them, so I think this solution would help.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.