Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity 4.3 - 2D, how to assign programmatically sprites to an object

Tags:

c#

unity3d

I'm trying to create an object that will be responsible of creating and showing different sprites, so I would like to access directly the assets/sprites programmatically instead of drag and drop a sprite in the hierarchy under that object.

There's a way to programmatically create a new sprite and assign what I have in the assets folder?

I also would like to have a sort of data structure with a few images loaded at the start of the game, an array or a dictionary or something like that so I can change based on some situation which images I need to show. But what confuses me since I'm new to Unity is how to create a sprite taking the reference to the sprite programmatically using the assets folder.

edit with progress:

I've created an array of sprites like this:

public Sprite[] mySprites;

in Unity I've added sprites inside the array manually (I just dragged png's inside the variable array) so actually in this Object I have a Component with this array full of sprites

inside the component I also have done this:

public SpriteRenderer renderer;
renderer = transform.GetComponent<SpriteRenderer>();
renderer.sprite = (Sprite)mySprites [0];

I got no error until I run the game, then the sprite is NOT assigned and I got this:

"PPtr cast failed when dereferencing! Castin from Texture2D to Sprite!"

I got this error even without casting (Sprite) and btw I don't know why he is telling my about Texture2D since verything is setted as sprite

like image 228
Adarkuccio Avatar asked Mar 15 '14 22:03

Adarkuccio


People also ask

How do I change a sprite in Unity?

How to change a Sprite from a script in Unity. To change a Sprite from a script in Unity, create a reference variable to hold the new Sprite. Then set the Sprite property of the Sprite Renderer Component on the Game Object you wish to change to match the new, replacement Sprite.

What are the different parts of a 2D project in Unity?

While these assets are the same as the ones you made in Unity 4.3 2D Tutorial: Getting Started, this version of the project has been organized into several folders: Animations, Scenes, Scripts and Sprites. This will help keep things tidy as you add assets to the project.

How to create multiple sprites for a prefab in Unity 3?

1 Multiple sprites for a prefab in Unity 3 Using Pygame, draw a copy of an image in a different location 0 Unity C# - 'Instantiate' only accepting "Transform", not 'Vector3' for instantiated object position

How do you add a sprite to a game object?

Select the new Sprite by dragging it into the field or by using Circle Select. Make sure to also set the Sprite Renderer reference in the Inspector, otherwise you’ll get an error. Alternatively, if the script is on the same Game Object, you can easily get it by using Get Component in Start.


2 Answers

To create a sprite programmatically might be a little too difficult to do. You'd probably need to create a Texture in memory, fill in the data, and then write that texture on the disk. After that, you should be able to read that using C#'s File, or Unity's WWW.LoadFromCacheOrDownload.

Here is how you would create a texture dynamically: http://answers.unity3d.com/questions/9919/how-do-i-create-a-texture-dynamically-in-unity.html


To load your sprites programmatically (not the ones created dynamically though), they need to be under a folder with the name Resources (which is a special folder).

I.e. suppose you have a sprite named MySprite.png under a folder named Sprites which is under a folder named Resources. You would then load it like this:

renderer.sprite = Resources.Load<Sprite>("Sprites/MySprite");

(notice how you do not include the Resources folder and the extension of the sprite)

You can find more details in the Loading Resources at Runtime documentation.


PPtr cast failed when dereferencing!

I did some searching and found out from this forum post that usually that happens when you had a list of one type and then changed it into another type. I.e. if mySprites was at some point GameObject[] and you linked a bunch of Sprites, and then changed it to Sprite[]. And since it specifically says Texture2D, I'm assuming that the import settings of your Sprites (or some of them) are set to Texture instead of Sprite:

Sprite import settings


Also, all MonoBehaviours have already a field named renderer, which is of type Renderer. I would suggest you rename that field to avoid confusion, unless you only used that as an example.

like image 184
pek Avatar answered Oct 18 '22 10:10

pek


Here is real code.. will help you

void SetSprite(UI2DSprite uiSprite, Sprite[] sprites, string strKey)
 {
     foreach (Sprite stexture in sprites)
     {
         if (stexture.name == strKey)
         {
             uiSprite.sprite2D = stexture;
             break;
         }
     }
 }

use it following the way..

UI2DSprite[] uiSprites = tmpObject.GetComponentsInChildren<UI2DSprite>();
 Sprite[] sprites = Resources.LoadAll<Sprite>("Textures");
 string resName = "icon_favorite_2";

 foreach (UI2DSprite uiSprite in uiSprites)
 {
 if(uiSprite.name = "icon")
  SetSprite(uiSprite, sprites , resName);

 }
like image 41
gitstar Avatar answered Oct 18 '22 09:10

gitstar