I need to resize an image to be sure it is a square.
So I do
var rectTransform = GetComponent<RectTransform>();
var width = rectTransform.rect.width;
var height = rectTransform.rect.height;
if (Math.Abs(width - height) > 0.1f)
{
float size = width < height? width : height;
transform.localScale = new Vector3(size/width,size/height);
}
When I play it I see that the rectTransform has the good size but the sprite as disappear an the image is now transparent, does somebody know about it?
Using unity V4.6b20
Problem solved by doing
var rectTransform = GetComponent<RectTransform>();
var width = rectTransform.sizeDelta.x;
var height = rectTransform.sizeDelta.y;
if (Math.Abs(width - height) > 0.1f)
{
float size = width < height ? width : height;
rectTransform.sizeDelta=new Vector2(size,size);
}
Related
I am looking to scale a orthographic camera in unity 2d to screen size.
When you edit your game you orient the camera in a way you want it to be, view all your objects etc.
But then when you maximise it your camera zooms out showing maybe other things you don't want to show
Images for example
Edit Mode View
Maximised
Thank you if you can answer my question
Best Regards
Dawid
Attach the following component to your camera. Make sure to set the target aspect ratio in the inspector (16/9 for example).
using UnityEngine;
[ExecuteAlways]
[RequireComponent(typeof(Camera))]
public class ViewportScaler : MonoBehaviour
{
private Camera _camera;
[Tooltip("Set the target aspect ratio.")]
[SerializeField] private float _targetAspectRatio;
private void Awake()
{
_camera = GetComponent<Camera>();
if (Application.isPlaying)
ScaleViewport();
}
private void Update()
{
#if UNITY_EDITOR
if (_camera)
ScaleViewport();
#endif
}
private void ScaleViewport()
{
// determine the game window's current aspect ratio
var windowaspect = Screen.width / (float) Screen.height;
// current viewport height should be scaled by this amount
var scaleheight = windowaspect / _targetAspectRatio;
// if scaled height is less than current height, add letterbox
if (scaleheight < 1)
{
var rect = _camera.rect;
rect.width = 1;
rect.height = scaleheight;
rect.x = 0;
rect.y = (1 - scaleheight) / 2;
_camera.rect = rect;
}
else // add pillarbox
{
var scalewidth = 1 / scaleheight;
var rect = _camera.rect;
rect.width = scalewidth;
rect.height = 1;
rect.x = (1 - scalewidth) / 2;
rect.y = 0;
_camera.rect = rect;
}
}
}
I'd like to expand on #sean-carey 's very useful answer.
I modified his code in order to add a min and max aspect ratio, so the viewport will be full screen in between those two aspects, and will pillarbox when < min and letterbox when > max
using UnityEngine;
[ExecuteAlways]
[RequireComponent(typeof(Camera))]
public class ViewportController : MonoBehaviour
{
private Camera _camera;
[Tooltip("Set the target aspect ratio.")]
[SerializeField] private float _minAspectRatio;
[SerializeField] private float _maxAspectRatio;
private void Awake()
{
_camera = GetComponent<Camera>();
if (Application.isPlaying)
ScaleViewport();
}
private void Update()
{
#if UNITY_EDITOR
if (_camera)
ScaleViewport();
#endif
}
private void ScaleViewport()
{
// determine the game window's current aspect ratio
var windowaspect = Screen.width / (float) Screen.height;
// current viewport height should be scaled by this amount
var letterboxRatio = windowaspect / _minAspectRatio;
var pillarboxRatio = _maxAspectRatio / windowaspect;
// if max height is less than current height, add letterbox
if (letterboxRatio < 1)
{
SetRect(1, letterboxRatio, 0, (1-letterboxRatio)/2);
}
// if min height is more than current height, add pillarbox
else if (pillarboxRatio < 1)
{
SetRect(pillarboxRatio, 1, (1-pillarboxRatio)/2, 0);
}
// full screen
else
{
SetRect(1, 1, 0, 0);
}
}
private void SetRect(float w, float h, float x, float y)
{
var rect = _camera.rect;
rect.width = w;
rect.height = h;
rect.x = x;
rect.y = y;
_camera.rect = rect;
}
}
I know that there are many questions like this on the net but I haven't found a solution to my problem yet...
I'm trying to make a background image that fills the screen. In my game, there is a canvas and inside this Canvas there is a gameObject which has attached the sprite component.
Many solutions includes this code:
void Awake()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
double variable = Camera.main.orthographicSize * 4.0;
float worldScreenHeight = (float)variable;
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
transform.localScale = new Vector2(worldScreenWidth / width, worldScreenHeight / height);
}
But I tried it and it makes the gameobject really small.
Any ideas?
At first, select your Canvas game object. in Canvas Scaler component set UI Scale Mode to Constant Pixel Size.
Note that, when you set UI Scale Mode to Scale With Screen Size, your image changes by screen width or height.
Also, Note that your image should be in the center of its parent. Set the transform position and rotation of your image to this:
Now Change your code to this:
void Awake()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
transform.localScale = new Vector2(Screen.width / width, Screen.height / height);
}
Hope it helps you.
I am working on a project. I want to take a picture from gallery of android device.
1. How can i take a picture from gallery of android device?
2. How can i Crop the picture selected circle portion in unity3d?
You can use below code for cropping image. I only pass Texture2D and set centre and radius automatically according to texture; but you can pass them manually if you want to.
Texture2D RoundCrop (Texture2D sourceTexture) {
int width = sourceTexture.width;
int height = sourceTexture.height;
float radius = (width < height) ? (width/2f) : (height/2f);
float centerX = width/2f;
float centerY = height/2f;
Vector2 centerVector = new Vector2(centerX, centerY);
// pixels are laid out left to right, bottom to top (i.e. row after row)
Color[] colorArray = sourceTexture.GetPixels(0, 0, width, height);
Color[] croppedColorArray = new Color[width*height];
for (int row = 0; row < height; row++) {
for (int column = 0; column < width; column++) {
int colorIndex = (row * width) + column;
float pointDistance = Vector2.Distance(new Vector2(column, row), centerVector);
if (pointDistance < radius) {
croppedColorArray[colorIndex] = colorArray[colorIndex];
}
else {
croppedColorArray[colorIndex] = Color.clear;
}
}
}
Texture2D croppedTexture = new Texture2D(width, height);
croppedTexture.SetPixels(croppedColorArray);
croppedTexture.Apply();
return croppedTexture;
}
I want to display a preview image which is a resized version of the original image with aspect ratio preserved in a fixed size Label. For example, I have an image of 1024x786 and I want to display it in a Label which has size 500x500. I want it to fit in this label keeping the aspect ratio of the image intact.
It would also be nice if the image could be automatically resized when a user resizes the part.
Is it possible to do this with Label? or do I need a canvas for this?
This image scaling code is based on JFace ImageDescriptor:
ImageDescriptor scaleImage(Display display, ImageDescriptor imageDesc,
int maxWidth, int maxHeight)
{
if (imageDesc == null)
return null;
ImageData imageData = imageDesc.getImageData();
if (imageData == null) // Can by JPEG using CMYK colour space etc.
return imageDesc;
int newHeight = maxHeight;
int newWidth = (imageData.width * newHeight) / imageData.height;
if (newWidth > maxWidth)
{
newWidth = maxWidth;
newHeight = (imageData.height * newWidth) / imageData.width;
}
// Use GC.drawImage to scale which gives better result on Mac
Image newImage = new Image(display, newWidth, newHeight);
GC gc = new GC(newImage);
Image oldImage = imageDesc.createImage();
gc.drawImage(oldImage, 0, 0, imageData.width, imageData.height, 0, 0, newWidth, newHeight);
ImageDescriptor result = ImageDescriptor.createFromImage(newImage);
oldImage.dispose();
gc.dispose();
return result;
}
I want to draw a rectangle on a form in C#. I read and found this article. Are there any samples or tutorials available ? The article was not very helpful.
The article you linked appears to be C++, which may explain why it didn't help you much.
If you create events for MouseDown and MouseUp, you should have the two corner points you need for a rectangle. From there, it's a matter of drawing on the form. System.Drawing.* should probably be your first stop. There are a couple of tutorials linked below:
Drawing with Graphics in WinForms using C#
Draw a rectangle using Winforms (StackOverflow)
Graphics Programming using C#
You need this 3 functions and variables:
private Graphics g;
Pen pen = new System.Drawing.Pen(Color.Blue, 2F);
private Rectangle rectangle;
private int posX, posY, width, height;
Second you need to make a mouse down event:
private void pictureCrop_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
posX = e.X;
posY = e.Y;
}
}
Third, you need to implemente the mouse up event:
private void pictureCrop_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
if (e.X > posX && e.Y > posY) // top left to bottom right
{
width = Math.Abs(e.X - posX);
height = Math.Abs(e.Y - posY);
}
else if (e.X < posX && e.Y < posY) // bottom right to top left
{
width = Math.Abs(posX - e.X);
height = Math.Abs(posY - e.Y);
posX = e.X;
posY = e.Y;
}
else if (e.X < posX && e.Y > posY) // top right to bottom left
{
width = Math.Abs(posX - e.X);
height = Math.Abs(posY - e.Y);
posX = e.X;
}
else if (e.X > posX && e.Y < posY) // bottom left to top right
{
width = Math.Abs(posX - e.X);
height = Math.Abs(posY - e.Y);
posY = e.Y;
}
g.DrawImage(_bitmap, 0, 0);
rectangle = new Rectangle(posX, posY, width, height);
g = pictureCrop.CreateGraphics();
g.DrawRectangle(pen, rectangle);
}
And to ensure that when you resize or move the form the rectangle will be there:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graph = e.Graphics;
graph.DrawImage(_bitmap, 0, 0);
Rectangle rec = new Rectangle(posX, posY, width, height);
graph.DrawRectangle(pen, rec);
}