Replacing the picture on the button through the code (Sprite none) - c#

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;

Related

Can't change pivot on gameObject in unity 2D

Everything was fine till all gameObjects stoped to see pivots from their sprites
in sprite they have pivot in the bottom, and gameObject has sprite sort point set to pivot, but all they show and act as pivot is in the center
[gameObject]
[sprite]
Why could this happen and how to fix it?
I have tryed create gameObjects again and reasign pivot, also reimported sprite file, but nothing changed
It seems like tool handle position is set to center. You can change it from the top left part of the scene view from center to pivot.
I have found couse of problem - it was render settings, after shifting to new pipeline assets render my setting for transparency sort mode was changed, I set them againg and it works as it was

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!

Unity2D hide everything that is outside shape defined by edgecollider

I have a 2D polygon that is defined by a edgeCollider. Is there a way to hide everything that is outside of the shape and show only what is inside?
I tried using skyboxes and lights. I thought about creating a mask(but i dont know how to create such a mask).
Is there a way to only show what is inside the shape defined by edge collider?
What about using the colliders trigger events?
Set this polygon as a trigger in the edgeCollider2D component in the inspector. Then you can use the collider OnEnterTrigger2D.
Your gameobjects are all disabled until this edgeCollider collides with in. Then disable the gameobject OnExitTrigger2D.
If you wanted to limit it to a certain number of object only. You would set a layer to only hide/show these object.
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.layer == "hiddenObject"){
other.gameObject.enable = true;
}
}
Then the reverse on the OnTriggerExit2D.
I'm not sure the effect that you are aiming for. So another solution could be a postprocessing shader.
I can only give a high level description on this however.
You would take the final screen image texture, and the current position of the polygon and then add the pixels from the screen texture to the polygon texture and output this texture. (this shader has to exists already).
But you want the inverse of these.
https://docs.unity3d.com/Manual/shader-TransparentCutoutFamily.html
You're wanting to keep what's in the hole and get rid of what's outside of it?
You can download the built in shaders to edit them here
https://unity3d.com/get-unity/download/archive
Just find your version of unity and select builtin shaders from the drop down.
Edit: "SPOTLIGHT!", try this but with your custom shape
http://www.shaderslab.com/demo-49---spotlight.html

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

Categories