Unity3D: Add text to a PreFab - unity3d

I'm making a 2d board-game-style game in Unity 5, and I have a prefab made up of a couple of sprites which represents a game piece. I want some text in my prefab that I can update as the game progresses.
If i try to add text, it requires a canvas, but when I create a canvas, an extraordinarily enormous canvas is created, that looks to be at least 1000x times bigger than by camera area. If I try to place this canvas inside my prefab, my prefab is now made of an enormously huge canvas, and my tiny sprite images. This makes the prefab impossible to position, or calculate sizing or animate, or anything else I want to do.
How can I add text to a prefab, and make the text contained within the size of my prefab spites?
Here's what I have tried so far:
if I set the canvas for the text to "Render Mode: World Space" I'm able to make it's rect tranform smaller. However, if I get it as small as my sprites, the text becomes an unreadably blurry mess. I guess this happens because my sprites are literally at least 1000x smaller than the canvas, so when I zoom in enough to even see the sprites, the text has been zoomed into oblivion. My sprites are so much smaller than the canvas, that if I am zoomed out to see the full canvas, my sprites are not even visible.
I'm able to kind of make things work if I recreate my prefab using UI Images instead of sprites. This way, the UI Images, and the text are both UI elements contained in the enormous canvas, so the size disparity doesn't exist. However, I don't know what the pitfalls are going to be trying to build an entire game out of ui images instead of sprites. Do I get all the state capabilities of sprites?

The canvas Unity generates that is 1000x bigger is used for GUI related objects that are not meant to directly interact with the game world like the score or buttons. In order to have moving text, try using 3DText. Have the text face the camera so that the text appears to be 2D. You can find this option in the menu under
GameObject => 3D Object => 3D Text.
From what I understand, all you need to do is parent this GameObject to your prefab and do some script magic at runtime.
For high fidelity text, either decrease the character size or increase the font size or do both.
Note: A larger character size reduces fidelity, but will take less processing power.
Source: Unity 5.2.0f3

Related

Scale Player Size according to the screen size in Unity

I have an issue with different resolutions.
Everything perfectly works on 1920x1080 however when i set it to tablet size like 10x10 aspect ratio 'Player' isn't resizing.
My platforms created under Canvas due to scalement and correct positioning. However my player created outside of the canvas.
Should i create my character under canvas or should i create my platforms outside of it? Currently I am not sure how to solve the issue.
Since player is created outside the canvas, there's no way for canvas to affect it (also player is probably using SpriteRenderer not Image component).
One way would be to put player as Image inside a canvas, but to be honest, canavs is created for UI, not gameplay. Putting all gameplay into UI might (and probably will) create a lot of issues. I'm already surprised that player and platforms interact in your game well as they use different systems.
What you probably want to do is to put all gameplay elements (character, platforms, projectiles, etc.) outside the canvas as sprite renderers and leave canvas for what it's meant to be (UI, maybe backgrounds).
Then, you might come across a problem, where on different resolutions, you have smaller or larger area of gameplay. Your options will be to: live with that, create system that restricts gameplay and fills empty space with background or black bars, or something in between (which is for eg. let vertical gameplay area be different, but horizontal the same).
Here's idea how you could achieve it:
https://forum.unity.com/threads/maintain-the-game-content-area-on-different-types-of-screen-sizes.905384/

Unity 2D - How to make Resolution-Independent colliders on my sprite

I'm currently working on a school project where we have to merge a 2010 game with an arcade-style game. I chose to merge COD:Blackops Zombie mode with Pacman.
Right off the bat I'm having issues. I'm trying to figure out how I can make the colliders for my level scale with the resolution.
I have an orthographic camera that has a size of 5.
This is what my level currently looks like.
As you can see above, the level itself is a single sprite, placed in a canvas that has Scale with Screen Size enabled.
I've placed a couple of 2D colliders on empty game objects on it for testing purposes.
The current resolution is 1920x1080, which is a 16:9 aspect ratio.
However, if I change the resolution to something like 800x600, which is a 4:3 aspect ratio... then the colliders are completely wrong now.
I thought that the colliders would scale with screen size just like the level sprite does, but I was wrong.
I am now trying to find an alternative approach to making the level have colliders that also change with the size of the sprite...
If you need more information please let me know and I will gladly update my question with the requested information.
To keep things simple, first start with a square aspect canvas.
Make an Empty GameObject, and place it at the bottom left corner of your canvas.
Put all your colliders inside this empty GameObject, and set them up so they match your play-field.
Now all you'll need to do is have a script on this empty gameobject that sets in Update():
Vector2 screenScale = new Vector2(Screen.width, Screen.height).normalized;
transform.localScale = new Vector3(screenScale.x, screenScale.y, 1);
This will make it scale everything inside it to the same proportions as the screen. Alternatively, you could use the Canvas's size if you want to make it based on that instead of the screen itself.

Purpose of mipmaps for 2D sprites?

In current Unity,
For use in Unity.UI as conventional UI ..
for any "Sprite (2D and UI)", in fact it always defaults to having "Generate Mip Maps" turned ON. Every time you drop an image in, you have to turn that "off" and apply.
As noted in the comments, these days you can actually use world space UI canvasses, and indeed advanced users may indeed have (say) "buttons that float over the head of Zelda and they are in the far distance". However if you're a everyday Unity user adding a button, just turn it off :)
In Unity, "sprites" can still be positioned in 3D space. For example, on a world space canvas. Furthermore, mipmaps are used when the sprite is scaled. This is because the mipmap sampling is determined by the texel size rather than the distance.
If a sprite is flat and perfectly scaled then there is no reason to use mipmaps. This would likely apply to your icon example.
I suspect that it is enabled by default for 2D games where sprites will often not be perfectly scaled. To clarify, a sprite does not need to be on a canvas. Sprites can exist as their own GameObject with a Sprite Renderer (not on a canvas.) When this is the case, scaling the camera view will change the sprite's size on the screen resulting in mipmapping due to the texel size changing. This results in making the sprite always perfectly scaled challenging without a canvas.

Merge several textures on screen into a single big one

In my 2D app, I create many gameObjects with UI image components (I work with new UI). These are simple colored circles. They are placed in different positions on screen. But when I play long enough, the count of these gameObjects becomes too high and it impacts performance.
I thought since these objects are just textures (I do not need them to be animated or something, just simple textures that stay on the same place in the screen all the time), maybe there is a way to merge them into one gameObject with Image component that would hold all these circles in one texture?
I tried to capture the camera view through RenderTexture ReadPixels method, but it captures the whole screen and all objects in it, while I need to merge only these circles and in a specific rectangle on the screen.

Unexpected sprite tearing?

I making a 2d game and I am having some very random problems... My sprites are not displaying as they should be. Below is a screen shot of what I see when I run my game. This bug is present in the game window and the scene window, both when the game is running and not running. And the glitch is not because of the tiles being offset (The background is made up of tiles), if you look closely the player sprite is also glitched up. I have tried restarting unity and my computer, one of which has worked. I have been having this problem ever since i started using tilesets (using one PNG image and cropping out smaller sprites by setting the sprite mode to multiple, instad of using just one PNG image where there is only one sprite and the sprite mode is singular). How do I fix this? (BTW I am using Oryx's lo-fi fantasy and sci-fi sprites at www.oryxdesignlab.com)
This happend when your atlas is not sliced correctly. When you making 2D game there are several important things about sprites.
Set Filter Mode to Point
Set Fromat to 16 bit or TrueColor (in special cases)
Make sure that your atlas is sliced properly.
Edit:
Adjust "Max Size" - make sure is value represents number equal or bigger than spritesheet size
The best way in sliceing atlas in Sprite Editor is NOT to use automatic. I got glithes like that in my project too and those steps can eliminate them.
Have a look at your Sprite in the Sprite Editor. I think you can get to it by double clicking.
Your sprites will have sort of boxes around them to identify each individual sprite (if you set your texture to multiple). One of these boxes will not have sliced correctly, and will have overlapped into another sprite, hence why you're seeing what looks like the arm of another sprite in your picture :)