Loading all sub-sprites of a sprite - c#

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!

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;

How do I keep an object always in front of everything else in SharpDX using HelixToolkit

I am adding a coordinate system (own class, derived from GroupModel3D) to my scene, and setting its Transform to the transform of the current selected object.
Everything is working fine, except when the object is to big, the coordinate system is inside and because of this not visible. So I like to make the coordinate system topMost, like it is in most CAD-systems.
I searched the internet and I found that simple clearing the z-/depth- buffer would do it (http://www.gamedev.net/topic/297605-how-to-keep-an-object-always-visiblein-front-of-others/) but i don't know how to do this inside HelixToolkit.
Found the Solution in the issues section of helixtoolkit:
https://github.com/helix-toolkit/helix-toolkit/issues/226

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.

Resourses.LoadAll not working

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");

Multiple sprites for a prefab in Unity

Im making a japanese educational game and made this sprite sheet. Ive already sliced it in the sprite editor and named all the symbols.
Im instantiating 5 prefabs that need to have 5 different sprites with different
symbols. I thought I was gonna be able to either assign the entire sheet to a game object or just keep adding sprite renderers to the game object, but I was wrong.
How can I achieve this?
You can change the sprite directly from code.
this.GetComponent<SpriteRenderer>().sprite = someSprite;
You can dependency inject the sprites themselves using the editor and a public variable for each; or if you prefer, you can use Resources.Load instead.

Categories