Issues Scaling Sprite Width to Screen Width

Setting a sprite's size with pixels

I figured it out, I had to use a combination of math functions that determined the width of the screen, they I had to perfect some of the numbers from there.

Here is a general idea of what works:

//Declaration Of Variables
float blockSize = Screen.width / 8.75f;
float blockDistanceX = Screen.width / 8.75f;
void Start()
{
//Extra variables that couldn't be declared earlier
float xValue = blockDistanceX * -1f * 3.8f;
float layerPos = -1500;

//Creates new Block
GameObject block = Instantiate(objectPrefab, new Vector3(0, 0, 1), Quaternion.identity, parent: canvas);
block.transform.localScale = new Vector2(blockSize, blockSize);
block.transform.localPosition = new Vector2(xValue, layerPos);
}

Scale Player Size according to the screen size in Unity

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/

How to scale sprites in libgdx according to screen resolutions?

Apparently this happened because I was using mipmaps. Without mipmapping, the sprites scale appropriately.

param.minFilter = Texture.TextureFilter.MipMapLinearLinear;
param.genMipMaps = true;

Set sprite width or height without scaling it's children

That's not possible directly, the size of a sprite depends on the bounds of its contents. So the only option would be to resize a child of the sprite, or place a dummy child on x: 0, y: 0 (assuming this would be your desired anchor point).

The coordinates of the contents bounds is not taken into account as you might expect, when having for example one child that is 100 x 100px, placed on x: 50, y: 50, the parents size would still be 100 x 100px, not 150 x 150px.

Adding another child of 100 x 100px on x: 0, y: 0 would increase the bounds to 150 x 150, because it now starts on child one x: 0, y: 0 and ends on child two x: 50, y: 50 + 100 x 100px.

content bounds

SpriteKit - Getting the actual size/frame of the visible area on scaled scenes .AspectFill

Here is how Aspect Fill works:

The dimensions of your scene will always remain the same, so if you say your scene is 480 points by 800 points, it will always remain that way regardless of device.

What then happens is it will scale the scene starting from the center of the SKView until it fills the farthest walls.

This will in turn crop your scene.

So when designing your game across the different aspect ratios, you will need to design your game to handle cropping.

Now in your particular case, You will need to figure out what the aspect ratio is for the device, and work off the difference from the scene aspect.

E.G. Your scene is 3x5, device is 9x16, This means your device is going to grow to become 9.6x16 to fill up to the size of your 9x16 device. We get this because our height has more distance than our width, so we need to do 3/5 = a/16, 3*16 = a * 5, (3*16)/5 = a so a = 9.6.

This means you have .6 of cropping to your scene on the width, .3 on both edges.

Now you are going to ask, How does the .3 relate to the various screen sizes.

Well we need to figure out what percentage .3 is of 9.6. We do that with division, .3/9.6 = .03125 or 3.125%; This means we need to push our border in 3.125% in the scene's width, which is 15 points. That means .03125 * 480 = 15. Your borders should then start at 15 width and at 465.

Pseudo Code:

let newSceneWidth = (scene.width * view.height) / (scene.height)
let sceneDifference = (newSceneWidth - view.Width)/2
let diffPercentage = sceneDifference / (newSceneWidth)

let leftEdge = diffPercentage * scene.width;
let rightEdge = scene.width - (diffPercentage * scene.width);

Now if there is more distance to scale in the height than the width, you would have to swap the width and height variables.



Related Topics



Leave a reply



Submit