I created an animated prefab and want to use it as image in button. Is this possible? If not what can I do to insert an animated image in button.
I assume you are talking about showing an animated 3D prefab on top of your button.
If you have a 3D prefab and want it to be rendered on top of a button, you need to setup a separate render chain.
In essence you add a camera, that renders your object to a RenderTexture. Then you use this RenderTexture as source for an Image component on top of your button.
The critical part is setting up the camera and prefab, so that it doesn't render the whole scene, and isn't visible from whithin the scene.
There are many ways to do that, but I think the easiest way is to place the prefab and camera far away from the rest so that they are outside the view range of the main camera.
Just make sure the camera sees the prefab (and only the prefab) and has the render texture setup as render target.
Related
I want to create a button that has a custom shape in Unity.
I created a default UI>Button object and set it's Image to the following sprite:
It doesn't show it on this website, but the circle is the only part of this image that is transparent. It's a .png sprite.
When I set this as the sprite for my button, I could click on the entire rect transform of this sprite to activate the button using my button's OnPointerDown() method and un-hide some other sprites around it:
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
Debug.Log("Foo");
}
Each time any part of the sprite was clicked, transparent or not, "Foo" would print.
Now I set the button alphaHitTestMinimumThreshold to 0.5:
GetComponent<Button>().image.alphaHitTestMinimumThreshold = .5f;
And as the docs suggest, I disabled atlassing...
...and enabled read/write for the sprite:
Yet now, when I click the button in my scene.. nothing happens. No matter where I click, "Foo" doesn't print anymore.
Then I tried changing the alphaHitTestMinimumThreshold to a lot of different values. Again, the entire button was not clickable anymore. If I don't touch the alphaHitTestMinimumTreshold at all however, my "Foo" prints again.
Why does changing alphaHitTestMinimumTreshold to any value make my OnPointerDown method to never be called?
So after 12 hours of messing around with all sorts of options, I found out that there were 3+ consecutive issues causing this behaviour.
If you ever run into your transparent buttons not working, be sure to try the following steps:
Make sure read/write is enabled in the texture import settings
Try if setting the Sprite Mode Mesh Type to Full Rect helps (sometimes the button's hitbox is off)
Disable atlassing under Project Settings > Editor > Sprite Packer > Mode
..and finally what did the trick for me: if you're working in a prefab that's nested inside another prefab (say your have a prefab for your button and then another prefab for your UI canvas/contents), remove the nested prefab from the parent prefab (aka remove the button prefab from the UI prefab), and just add it back in again. It was that freaking simple for me.
I triple checked whether my nested button prefab differed from the button prefab blueprint, and it didn't. But somehow, removing it from the UI prefab and adding it back in again solved my problem with a large sprinkle of magic.
With transparency buttons, I found that detection improves when the image is a child of the button object. So hierarchy is Canvas>Button>Image.
I know, old thread, but it came up so heres my solution:
It did all of the suggestions, but it didn't work.
HOWEVER,
What finally worked for me, was TO SPECIFY THE RIGHT SIZE. My game Object was elongated, but the sprite had "preserve aspect ratio" ticked. I just didn't bother to size it properly.
So when you have an image, set the size of the gameObject to the right dimensions.
I'm trying to make an application that has sprites going down the screen with numbers in them. The Text(Script) component only works for 3D objects. I've tried using Text Mesh and making that a child of the sprites but I need them to be instantiated as a Prefab. So when I load the sprite in as a prefab it loses it's relationship to the Text Mesh. Does anyone know of a solution that worked for them?
There are many ways to do it depending upon your requirement. I will show you two of the most basic.
1- Use 2D Sprite and TextMesh.
2- Use Canvas Image and Text Objects.
P.S First one, that is 2D Sprite can be Instantiated directly in the hierarchy but for 2nd(Canvas Image) you need to instantiate it inside the canvas obj means as a child of canvas object.
I spawn the prefab in a gameobject and that gameobject is a child of UI Image what I'm trying to accomplish is display the prefab infront of a UI Image. I've tried making z position of gameobject to negative but nothing happens. I can't place the sorting layer of UI Image cause it doesn't exist. What should I do? Is this even possible?
Update
Tried adding second camera. I made gameobject layer to SecondCam. Change settings of second camera to depth only and it's culling mask to SecondCam only then it's layer to SecondCam also. Change settings of main camera culling mask to everything except SecondCam. But this doesn't work, Have I done something wrong?
Second Camera settings
Main Camera settings
If your Gameobject is not one of the UI components you may need to give it another layer and render it with another camera with depth property higher than the UI camera.
You can change the sorting layer on prefabs or any object created on runtime.
If your UI canvas is set to screen overlay, then it with ALWAYS render on top. You have to use a screen space or world space UI, along with Dave's answer.
I ought to have a normal map of tiles as below:
But when I load the scene, either switch from another in inspector (e.g. click "scene/menu", then click this scene), or transit into this scene in gameplay (SceneManager.loadScene), sprites get invisible randomly.
Each time some of the sprites get invisible, and the next time the invisible ones may not be the same. I suspect it is something with Unity editor, since this happens even when I'm not in playing mode.
In Debug.Log I definitely find those sprites (not null). They just don't show up. I have to reload sprites / reload the scene in Unity inspector, but the load doesn't persist.
Neither sprites, SpriteRenderer nor GameObject is null.
As below is a snapshot when some sprites invisible (not missing):
Just click another scene and switch back:
I just accidentally checked in 3D mode, Unity editor is randomizing with z axis. The sprites are there, they were just hidden behind background. This can be fixed then.
I am having a problem with my unity canvas as my background is on top and covering my gameobjects. I have tried putting the canvas sorting order to -10000 and changing the canvas rendering mode to overlay/camera/worldspace, but none of them works..
This is how my game looks like right now:
as you can see the white image which is my background is covering my gameobjects..
Attached are my component values for my canvas:
my canvas
Attached are the values for my camera:
my camera
Attached are the component values for my gameobjects:
my hero gameobject
Have you tried setting your game objects z position to a negative value?
For 2D games in Unity, the more negative your Z value is, the "closer" it is to the camera. If you try setting your knight to something like -1, it should no longer be obscured by your background.
Also, make sure to set the camera for your canvas' render camera and you can set the sorting order back to whatever it was before. Just check the individual pieces attached to your canvas to make sure their Z positions aren't "in front" of the game objects.
The background for your game shouldn't be on the canvas, it should be a gameobject with a sprite renderer and need to be attached with a sprite(background image) with a resolution that will fit most mobile screens (assuming you build it for mobile phone, i personally prefer making it 1028X720)
if you still insist on putting it on the canvas then you could set the canvas render mode to world space and set the sorting layer to something less than default or sort order -1
I know it is old post but #user8502296's answer saved me so I wanted to contribute. I changed the Canvas->RenderMode->Screen Space-Camera then I added Render Canvas->Camera->Main Camera.
Canvas