I am trying to save/load sprites for my game, and am achieving it by simply getting the path of the asset. Although, whenever trying to save a sprite that is inside of .psb file I simply cannot as it is saved as one singular .psb file. For example, I am getting a path for a .png file inside my folder, and it works flawlessly, but, whenever trying to access lets say head sprite that is inside of .psb file in my assets folder, I cannot do that.
Any tips for saving/accessing those files and how to achieve it?
Please help :)
It seems to me that what you get is not a Sprite but a SpriteAtlas in which case you could use SpriteAtlas.GetSprites to list all sprites and SpriteAtlas.GetSprite to get specific sprite by name/tag.
SpriteAtlas theAtlas;
var sprites = new Sprite[theAtlas.spriteCount];
theAtlas.GetSprites(sprites);
var specificSprite = theAtlas.GetSprite("Head");
Related
What I want
To create an effect like an slash in my 2D project. For that, I've an image in my assets already, and I want to draw it from top to the bottom, allowing it to be drawn in maybe half a second, progressively.
The idea is to draw it to create the effect of the slash itself, and the progressively mode is to "fit" the slash image itself, imagine the first one of the following link, that would be drawn from left to right.
To achieve that kind of drawing, the only thing that I've found is fillAmount for an Image, combined with the fillMethod.
The problem
The problem is that these methods are for the Image class, that it's not recommended for game drawing but only for UI.
Anyway, I've tried to do it this way, and I've not been able to do it, because the Unity environment didn't let me drag&drop my slash asset into the Image variable that I had created into my script. I thought that it may be because of the type of import, but also trying to import it with the other options resulted in nothing better.
The questions
Just answering one of these questions would solve my situation (or I think so):
How can I make Unity accept my image/asset into my Image object in the script?
Another way to create such effect using Sprite or GameObjects directly?
Method 1: You can use sprite sheet to create a 2D animation
1-1. Prepare a splash effect sprite sheet (an image map contains continuous action image), like
1-2. Change the sprite mode to multiple in inspector window
1-3. Slice the sprites in sprite editor
1-4. Multi-select the sprites and drag into hierarchy window, Unity will ask you to save this animation
1-5. You will get an animation GameObject
Method 2: If you can make it like a circular progress bar, check Radial/ Circular Progress Bar in unity3d
How can I input image to be my questions on my quiz game app. I've managed to have the image script but I can't see any images
Here is my code for the Game Controller which holds the data of the game.
Here's an image of the Game Controller
Game Controller will look like this
public Sprite quizImage;
public TSSimpleObjectPool answerButtonObjectPool;
public Text questionText;
public Text scoreDisplay;
public Text timeRemainingDisplay;
public Transform answerButtonParent;
public GameObject questionDisplay;
public GameObject roundEndDisplay;
public Text highScoreDisplay;
The problem is image does not show when i run the app. What is the problem?
"Image doesn't show" could mean multiple things. You really need to clarify.
But here are some quick fixes from my experience. (Common error: "Image doesn't show")
a)
Make sure you look at the parameters when looking at Unity. When you superimpose images in unity (ie putting a background with a image), it's a common issue that sometimes images cover each other. You have like 5 images/text in your picture... make sure your paying attention to z values.
b) You need to looking into prefabs. IE dragging an image into the hierarchy and dragging it back to assets usually does the trick. Get the prefab -> drag it into the public gameobject. Judging by how you haven't plugged in a sprite in your image means you can't to figure out how ton drag objects in.
Some documentation on prefabs
https://docs.unity3d.com/Manual/Prefabs.html
c) You're mixing up Sprites and GameObjects together. Look up differences between the two and post again.
d) Make sure you are not plugging in a blank sprite. If you want to use sprites, make sure you know how to attach to image to them. You can follow this tutorial down here.
https://docs.unity3d.com/Manual/SpriteEditor.html
Second of all, sprites are used in unity 2D, not unity 3D. They are 2D graphical objects... stop mixing up the two.
Hope this helped.
First of all, sorry for my English.
I would like to backup some textures into a list from a game object that has different texture for every frame. I tried to do this and I pushed textures into the list every frames but all of pushed texture seems like overwritten to the latest texture... I also tried Instantiate(); when each textures are pushed into the list but the textures were completely transparent. I'm not using movie texture. Does anyone know how to make this??
I might didn't explain enough so please ask me if something is not clear.
Thank you
//Those line of codes are looping every frame
//This is the current code
transitionTextures.Add(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture);
//This is the another code that I tried
transitionTextures.Add(Instantiate(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture));
**transitionTextures.Add(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture);**
This line of code copies the reference of targetGameObject.GetComponent().materials[0].mainTexture to the list so according to copy by reference rule when ever targetGameObject's mainTexture is changed it will change all references in the list to the updated new value. so you should create a new object so it should be copied by value like this
Texture2D temp = targetGameObject.GetComponent<Renderer>().materials[0].mainTexture as Texture2D;
Texture2D tex = new Texture2D(temp.width, temp.height);
tex.SetPixels(temp.GetPixels());
tex.Apply();
transitionTextures.Add(tex);
Try this , it might help you.
Is it possible in Unity 3D using C# to create an array to load sprites randomly from a folder rather than from a sprite sheet? If it is, what code do I use to refer to the folder? From what I can find, sprites are usually coded to load using random.range with an array using a sprite sheet instead of actually accessing the folder. The only thing even similar to this that I have been able to find is here:
http://docs.unity3d.com/ScriptReference/Resources.html
but as you can see with this you can only load from a folder titled "Resources" in the "Assets" folder, and possibly I'm mistaken but it would also seem that this can only be done with a game object. (?)
You are looking at the right docs.
A sprite is a GameObject, just a more specific one meant to be used in 2d games. So you would create your sprites and make prefab of them. Those prefabs go in the Resources folder and here is the code:
GameObject [] objs = (GameObject[])Resources.LoadAll("SpriteFolder");
GameObject randomSprite = objs[Random.Range(0, objs.Length)];
Here is what I ended up using:
Sprite[] enemySprites = Resources.LoadAll("Sprites/Enemies");
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
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:
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.
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);
}
Just to throw my tuppence worth in. I had this problem when I changed a SerializedField from Texture2D to Sprite in the code. The texture was already a sprite in the editor but it needed to be Sprite in the code. The field in the editor initially said type mismatch and then displayed the sprite texture again.
To cure it I set the field in the editor to none and the reset it to the required sprite.