Resourses.LoadAll not working - c#

I'm trying load in a Sprite Array Sprite, sliced in Editor using code, but sprites. Length always returns 0. A folder named "Sprites" is in Assets' folder and a sliced picture is in Sprites. Why it isn't working?
Sprite[] sprites = Resources.LoadAll<Sprite>("Sprites");

The Sprites folder must be in the Resources folder. Like this:
It is also worth pointing out that your code is looking for files that are marked as Sprite.
Sprite[] sprites = Resources.LoadAll<Sprite>("Sprites");
This means that your pictures must have this format or they won't be added to the array.
Or you could just load them all as Objects like this:
Object[] sprites = Resources.LoadAll("Sprites");

Related

Replacing the picture on the button through the code (Sprite none)

There are two graphical buttons on the canvas (UI->Button). By clicking on the second one, the following script is executed:
But1 = GameObject.FindGameObjectWithTag("ButtonEgg");
But1.GetComponent<Image>().sprite = Resources.Load<Sprite>("Eggs02");
It should replace the value in the first button in the Image component from the default Eggs01 sprite with Eggs02. But instead it gives a sprite none and a white rectangle.
Both pictures are in the Sprites folder, Sprite (2D and UI) is set in the inspector, and Single is set in Sprite mode.
Tried to duplicate the sprite in the Resources folder.
Even tried to specify the path:
Resources.Load<Sprite>("Sprites/Eggs02");
up: Debug.Log(Resources.Load("Sprites/Eggs02")); issues null
but for sure the sprite is in the folder and even tried to copy the path through the context menu still null (((
I'm not a magician, I'm just learning, so I strongly ask you not to kick and help the young man solve the problem.)
Resources.Load only works with assets placed in and a path relative to a folder called Resources ... So unless your sprite is placed in e.g. Asset/Resources/Sprites/Eggs02 it will obviously return null.
In general you don't want to use Resources at all!
Rather have a
public Sprite sprite;
// or
//[SerializeField] private Sprite sprite;
and drag your sprite into the slot in the Inspector and then simply do
But1.GetComponent<Image>().sprite = sprite;

Loading all sub-sprites of a sprite

I'm trying to load all the sprites given the current sprite in the sprite renderer on an object however I am unable to load each individual sprite (2d sprite with multiple 'frames', i.e. a spritesheet) no matter what I try.
I've used
Resources.LoadAll<Sprite>(AssetDatabase.GetAssetPath(spriteRenderer.sprite.GetInstanceID()).Replace(".png", ""));
And even
Resources.LoadAll<Sprite>(Path);
But I can't seem to get it to load the things I want (usually it doesn't work at all but on an older version of Unity it loaded circular loading bars and error signs). I have created the frames/sub-sprites of the sprite.
I've tried changing the path to just using the sprite's name to including the filetype to anything else but nothing works.
I am using Unity 2019.1.0f2 if that matters.
So you want to load a bunch of sprites right?
Have you tried this?
Sprite[] loadedSprites = Resources.LoadAll("Sprites", typeof(Sprite));
If you use this your unity must have a folder named Resources. Basically what it says inside LoadAll at the first param is this: "Resources/Sprites" and then it looks for all Types that are a Sprite inside the Sprite folder.
Note: Make sure all the .png files that are planned to be used are signed as Sprite(2D and UI) this can be done by clicking of the png in unity and right corner it wil say Texture Type.
It ended up being a simple misunderstanding.
My folders were all inside the "Assets" folder while they should have been in the "Assets/Resources" folder. Now I can load the sprite renderer's sprite's sprites using
spriteRenderer.sprite.GetInstanceID()).Replace(".png","").Replace("Assets/Resources/","")
It's a bit long but it works!
Thanks to Louis Ingenthron for the hint!

Unity Resource.Load sprite returning null

Ok, I've beat my head against a wall on this enough... As the title says, Resource.Load("icons_3"); is returning null. Ive cut back on as much as I can with the code to figure it out (simplifying it). Heres what I have
icon.sprite = Resources.Load<Sprite>(items[i].item.itemIcon);
Pretty simple. The itemIcon is "icons_3"
This is coming out of a sprite that is set as multiple (a sprite sheet sliced up). I can drag the sprite manually onto the object, but that defeats the purpose.
The sprite sheet is in the Assets/Resources/ directory, so it should load off just the name. Originally it was in the Resources/Sprites folder, but to cut back everything, I moved it up a directory.
So im at a total loss with this one. Is it because its a sprite sheet? Do I need to use another method to load a sprite from a sprite sheet? Thanks for all the help in advance guys.
Did a quick test just to make sure.
void Start()
{
SpriteRenderer sr = gameObject.GetComponent<SpriteRenderer>();
sr.sprite = Resources.Load<Sprite>("Sprites/BlueBlock");
}
The Sprite sits in Assets/Misc/Resources/Sprites/.
Works just fine, so I guess you made an mistake somewhere else.
This must be done:
Your Sprite must be set to texture type Sprite (2D and UI).
This will create a "sub-sprite" in the Sprite (with the Sprite Editor) by default at least if it is a single sprite. The name of this might differ from the main sprite if it is multiple, so make sure you have that right. Make sure that the whole path you give to the Load is correct (case sensitive as fare as I know).
EDIT:
For a sprite that is set to multiple and is sliced you need to use Resources.LoadAll. Those sprites have a name property though which you can use for searching in a loop. Alternatively you can create a Dictionary (with string being the name) or more complex also a sprite atlas.

2d sprite did not rendered as original image after put in unity

image size = 2048 x 512px
as what shown on image i uploaded
I don't know what happened with my 2d sprite many people say that was compress problem but i still can't fix it and i tried to use unity asset store 2D pack its look good in my unity, someone ever got the same?
i used photo shop to make these 2D sprite, i used brushes, burn and dodge tools.
public GameObject background;

XNA Model won't compile?

I'm working on a game in XNA, and i'm loading a model from blender. The model didn't have a texture until now, and when it tries to compile I get this error:
The mesh "", using BasicEffect, contains geometry that is missing texture coordinates for channel 0.
The model loaded before this point. I know I have to add the texture file in the same location as the .x file in my content, and I did that. The .x file contains the segment that references the texture.
Material ShipMat {
0.640000; 0.552144; 0.594688; 1.000000;;
96.078431;
0.500000; 0.500000; 0.500000;;
0.000000; 0.000000; 0.000000;;
TextureFilename {"shipTexture.jpg";}
}
I'm using the add-on DirectX exporter for blender, because when I tried exporting my model as a .fbx it didn't load the texture and it was rotated in an odd direction. Any Ideas? Thanks in advance.
For a texture to work, each model vertex needs texture coordinates.
Sounds like the model did not export from blender with a texture coordinate element for each vertex. Most likely, your model vertices only have position, color, & maybe normal elements only.
Just go back to blender, apply any old texture you want, then re-export it & swap out textures in Xna and you will get what you're expecting now.

Categories