How to modify the Unity maximum pixel width and height? - c#

I want to get 1920x1080 camera view from Unity but output : (cam.pixelWidth: 256, cam.pixelHeight = 256) always . I have multiple camera. For main camera as a default I can get 1920x1080 resolution but I can not get for other cameras. How can I convert 256:256 image to 1920:1080 ?

Related

Unity2D Different Screen Resolution Problem

There is a game I made on Unity, but my game does not support different screens. I searched it online but all solutions contain "canvas" but I do not use canvas in my project.
my game works in 16:9 landscape resolution
What can I do about it
1280x720 screen resolution
2960x1440 screen resolution
You can write a script and attach it to the background that changes its dimensions using the resolution in use.
Example :
int width = Screen.Width;
int height = Screen.Height;
transform.localScale = new Vector3(width, height, tranform.localScale.z);
Just edit the code and use Vector2 for 2D objects.

Resize loaded sprite from a texture with type multiple (width and height not match)

hello I'm having a problem that I don't understand why that is happening, the problem is that:
I have a texture of type multiple where I load all of my cards sprites
texture:
as you can see each card has its own Sprite properties that define the limit of the image and on this example, you will see that the width is 402 and height is 563.
but for some reason when I load the sprite using
Sprite[] sprites = Resources.LoadAll<Sprite>(sheetName) and then accessing to the specific sprite for example (sprites[0])
if I do
sprites[0].rect.width
sprites[0].rect.height
the values returned are different than the values specified before on the image and I need these values to scale the sprite width a specific width and height using this formula
var factorX = (CARD_WIDTH / sprites[0].rect.width );
var factorY = (CARD_HEIGHT / sprites[0].rect.height ) ;
transform.localScale = new Vector2(factorX, factorY);
because the values for width and height are different the factor that I'm getting is wrong.
also, I just noted that the values that I'm getting are the same that unity editor shows to me here
My question is:
why I'm getting different values for width and height?
The two ratios are about the same, could it be showing you the resolution of the thumbnail's mip map rather than the raw texture? You could try turning off Generate Mipmaps in the texture import settings if it's on

Knowing the actual dimensions of a sprite after scaling it at runtime

I'm creating a 2D project and I've a particular sprite that when I import it to Unity I apply a 200 units per pixels in the importer and in runtime sometimes I scale it.
How can I know the actual dimensions of the rendered sprite?
Sprite width in pixels / pixels per unit * X scale = actual width
Do the same for height to get the size in both dimensions.
If you want to get the width of the sprite projected on the screen then you will need to modify the result depending on the camera size. I think you could just divide it by the camera size.

Drawing a circular magnifying lens showing scaled underlying content in XNA/Monogame (in 2D)

I have a 2D scene in Monogame with some primitives and sprites (i.e. in PrimitiveBatches and SpriteBatches) and I would like to create a magnifying glass effect with a circular lens showing a zoomed view of the content under it. How do I do that?
Thanks.
I do not use your environment but I always did this effect with pixel displacement. If you got pixel access to rendered scene (ideally while still in a back-buffer so it does not flicker) then just move the pixels inside your lens to the outward positions. Either use constant displacement or even better is when you move more (bigger zoom) in the middle and less near the edges.
Typical implementation looks like this:
copy lens area to some temp buffer
loop (x,y) through lens area
compute actual radius r of processed pixel from lens center (x0,y0)
ignore pixels outside lens area (r>R)
compute actual zoom m of processed pixel
I like to use cos for this like this:
m=1.0+(1.5*cos(0.5*M_PI*double(r)/double(r0))); // M_PI=3.1415...
you can play with the 1.0,1.5 constants. They determine minimal (1.0) and maximal (1.0+1.5) zoom. Also this is for cos taking angle in [rad] so if yours need [deg] instead change the 0.5*M_PI with 90.0
copy pixel from temp to backbuffer or screen
backbuffer(x,y)=temp(x0+(x-x0)/m,y0+(y-y0)/m)
Here C++/VCL example:
void TMain::draw()
{
// clear bmp (if image not covering whole area)
bmp->Canvas->Brush->Color=clBlack;
bmp->Canvas->FillRect(TRect(0,0,xs,ys));
// copy background image
bmp->Canvas->Draw(0,0,jpg); // DWORD pxy[ys][xs] is bmp direct pixel access, (xs,ys) is bmp size
// here comes the important stuff:
int x0=mx,y0=my; // position = mouse
const int r0=50; // radius
DWORD tmp[2*r0+3][2*r0+3]; // temp buffer
double m;
int r,x,y,xx,yy,xx0,xx1,yy0,yy1;
// zoom area bounding box
xx0=x0-r0; if (xx0< 0) xx0=0;
xx1=x0+r0; if (xx1>=xs) xx1=xs-1;
yy0=y0-r0; if (yy0< 0) yy0=0;
yy1=y0+r0; if (yy1>=ys) yy1=ys-1;
// copy bmp to tmp
for (y=yy0;y<=yy1;y++)
for (x=xx0;x<=xx1;x++)
tmp[y-yy0][x-xx0]=pyx[y][x];
// render zoomed area
for (y=yy0;y<=yy1;y++)
for (x=xx0;x<=xx1;x++)
{
// compute radius
xx=x-x0;
yy=y-y0;
r=sqrt((xx*xx)+(yy*yy));
if (r>r0) continue;
if (r==r0) { pyx[y][x]=clWhite; continue; }
// compute zoom: 2.5 on center, 1.0 at eges
m=1.0+(1.5*cos(0.5*M_PI*double(r)/double(r0))); // M_PI=3.1415...
// compute displacement
xx=double(double(xx)/m)+x0;
yy=double(double(yy)/m)+y0;
// copy
if ((xx>=xx0)&&(yy>=yy0)&&(xx<=xx1)&&(yy<=yy1))
pyx[y][x]=tmp[yy-yy0][xx-xx0];
}
// just refresh screen with backbuffer
Canvas->Draw(0,0,bmp);
}
And here animated GIF preview (quality and fps is lowered by GIF encoding):
If you need help with understanding the gfx access in my code see:
gfx rendering in C++

How to scale texture2d in XNA with window resizing

I'm developing an UI for a project for school, and I've tried similar methods to scaling my texture as listed here, but here is the issue:
Our project is developed at 1440 x 900, so I've made my own images that fit that screen resolution. When we have to demo our project in class, the projector can only render up to 1024 x 768, thus, many things on the screen goes missing. I have added window resizing capabilities, and I'm doing my scaling like this. I have my own class called "button" which has a texture 2d, and a Vector2 position contruscted by Button(Texture2d img, float width, float height).
My idea is to set the position of the image to a scalable % of the window width and height, so I'm attempting to set the position of the img to a number between 0-1 and then multiply by the window width and height to keep everything scaled properly.
(this code is not the proper syntax, i'm just trying to convey the point)
Button button = new Button(texture, .01, .01 );
int height = graphicsdevice.viewport.height * button.position.Y;
int width = graphicsdevice.viewport.width * button.position.X;
Rectangle rect = new Rectangle(0,0,width, height);
sprite.being()
sprite.draw (button.img, rect, color.white);
sprite.end
it doesn't end up scaling anything when i go to draw it and resize the window by dragging the mouse around. if i hard code in a different bufferheight and bufferwidth to begin with, the image stays around the same size regardless of resolution, except that the smaller the resolution is, the more pixelated the image looks.
what is the best way to design my program to allow for dynamic texture2d scaling?
As Hannesh said, if you run it in fullscreen you won't have these problems. However, you also have a fundamental problem with the way you are doing this. Instead of using the position of the sprite, which will not change at all during window resize, you must use the size of the sprite. I often do this using a property called Scale in my Sprite class. So instead of clamping the position of the sprite between 0 and 1, you should be clamping the Size property of the sprite between 0 and 1. Then as you rescale the window it will rescale the sprites.
In my opinion, a better way to do this is to have a default resolution, in your case 1440 x 900. Then, if the window is rescaled, just multiply all sprites' scaling factors by the ratio of the new screensize to the old screensize. This takes only 1 multiplication per resize, instead of a multiplication per update (which is what your method will do, because you have to convert from the clamped 0-1 value to the real scale every update).
Also, the effects you noticed during manual rescale of the sprites is normal. Rescaling images to arbitrary sizes causes artifacts in the rendered image because the graphics device doesn't know what to do at most sizes. A good way to get around this is by using filler art during the development process and then create the final art in the correct resolution(s). Obviously this doesn't apply in your situation because you are resizing a window to arbitrary size, but in games you will usually only be able to switch to certain fixed resolutions.

Categories