My 9 slice sprite doesn't act properly - c#

I'm trying to use a 9 slice sprite in unity so that when the image is resized only the center and the sides are scaled, and the corners stay the same anytime.
So I sliced my sprite, set the container (image) type to Sliced.
However when I run my code, the entire sprite suddenly show up in the center slice (so is resized in a weird way) leaving the sides and corners transparent.
What could I be missing ?
It should be the first case but when i run the second case happens :
here is how I sliced the sprite (sorry hard to see but its white corners over a transparent background) :

Related

tiled draw mode in Sprite Renderer component

Illustrating Image:
Ok so my goal is to fill up the sky with blue. I used the tiled draw mode in Sprite Renderer component but it doesn't seem to be a good choice. I thought of creating a blue square and then put it on top of the sky but is there any way to do it automatically? ( I found something called border but I couldn't understand what these green lines mean? )
You first have to set up the image to tile correctly. In the inspector for the image you want to tile, open the Sprite Editor, and move the border (green box) to select the SKY portion of your image. For example:
After doing this, your sprite should tile correctly:
Do Note:
If you're filling in a solid color for the sky, use the Sliced mode instead of Tiled to reduce polygon counts. (Tiled mode will make new quads for each repeated section, while Sliced will stretch the solid color in only one quad):
(Left is Tiled, right is Sliced)

Weird screen scaling and sprite displaying

When i scaled my window, the sprites were a lot smaller, also, sprites do not display after reaching about halfway across the screen. The background color blue still displays but the sprites get cut off.
In the above image as you can see the sprites did not stretch with the screen, and the sprites near the top are being sliced when they reach that part of the screen.

How do I create an image and save it for later to draw as texture in XNA?

In my game I need to draw a circle made of squares the sizes of a game tile (circle is made of squares). I could just draw monochrome square textures in form of a jagged circle each frame, but it consumes a significant amount of resources. What I'd like to do is to draw it somewhere in the memory just once and save to draw each frame after that.
I could simply draw said circle myself and use it as a ready texture, but my circle is not always the same. It has different size throughout the game (and it's not really a circle half of the time, but I've got the algo that says where to draw), so it has to be drawn programmatically.
First you render the circle to a custom RenderTarget2D. You can set a custom render target like this:
GraphicsDevice.SetRenderTarget(renderTarget);
After rendering your circle to the render target cast it to a Texture2D like this:
texture = (Texture2D)renderTarget;
Read more: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Render_to_texture.php

C# MDX drawing transparent rectangle over a sprite

I want to make a "Pausemenu" in a Tetrisgame, when I hit Esc, the Menu pops up and that the user clearly is aware of that the game isnt running, I want to draw a transparent black rectangle over the whole game sprite, I´m using C# Managed DirectX 9.0c on .Net Framework 3.5. (I could achieve the same effect with a texture, but since in the settings the Board Width/Height can be changed, this would be an ugly solution)
Is there an easy way to achieve this?
You can simple create a small PNG, make it black and 75% opaque.
In your Game just use the Sprite Class (with alpha enabled) and scale it (with transform) to the dimensions of your viewport.
You could also use the Sprite Class to modify the Color of the Sprite when drawing, so you can make it fade in or out or give it another color.
Hope that helps!

Letterboxing AND Scaling in XNA on PC

Is there a way that I could basically develop my XNA game at 1080p (or 720p) as my default resolution and then depending upon the set resolution just scale everything in the game to the proper size, without having to set the scaling factor in every Sprite Draw() method?
My thought is that, I could develop all the graphics, configure coordinates, etc based on a resolution of 1080p but then for the XBOX just set the res to 720p and scale down (so that the XBOX sees everything as being at 720 and therefore handles all resolutions as mentioned in the developer docs) and on PC scale to any needed resolution or aspect ratio by automatically letterboxing the view for resolutions that are not 16:9.
I already had my game setup so that spritebatch.begin() and end() were called at the highest level, around all the other Draw calls, so that I could technically just pass in the scaling matrix there, but whenever I do that it will do something weird like make the view off center, or only take up a quarter of the screen.
Is there a best practice way for achieving this?
If you set a scaling matrix in SpriteBatch.Begin, then it will scale the sizes and positions of every single sprite you draw, with that SpriteBatch, up until you call End.
SpriteBatch uses a client space where zero is the upper left corner of the Viewport, and one unit in that space is equivalent to one pixel in the viewport.
When you give SpriteBatch a transformation, the sprites that you draw will have that transformation applied to them for you, before they are drawn. So you can (and should) use this same technique to translate your scene (to centre it on your player, for example).
For Example:
Your game is developed at 720p and you're using SpriteBatch without a transformation. You have a sprite centred at the bottom right corner. Let's say its texture is (32, 32) pixels and the sprite's origin is (16, 16) (origin is specified in texture space, so this is the centre of the sprite). The sprite's position is (1280, 720). The sprite's scale is 1, which makes its resulting size (32, 32). You will see the top left quarter of the sprite at the bottom-right corner of your screen.
Now you move to a screen that is 1080p (1.5 times bigger than 720p). If you don't add a scaling matrix to SpriteBatch, you can see the whole sprite, with its at two-thirds of the screen, rightwards and downwards.
But you want to scale up your whole scene so that at 1080p it looks just like it did at 720p. So you add the matrix Matrix.CreateScale(1.5f, 1.5f, 1f) (note using 1 for Z axis, because this is 2D not 3D) to your SpriteBatch.Begin, and do nothing else.
Now your scene of sprites will be scaled 1.5 times bigger. Without making any changes to the actual Draw call, your sprite will be drawn at position (1920, 1080) (the bottom right corner of the screen), its size will be (48, 48) (1.5 times bigger), and its origin will still be the centre. You will see the sprite's top-left quadrant at the bottom-right corner of your screen, just like you did at 720p, and at the same relative size.

Categories

Resources