Is it possible to change the aspect ratio when the game is fullscreen in WebGL? If so, does it pillarbox or stretch to compensate for the difference?
Screen.SetResolution(800, 600, true);
does not work in WebGL.
Its not possible in WebGL, because you export with a body and canvas size, not screen resolution.
You need to set your canvas size in your template css file:
<canvas id="c" width="400" height="300"></canvas>
You can call this function to resize your canvas size when you go to full screen o resize your window:
function resizeCanvas() {
canvas = document.getElementById("c");
gl = canvas.getContext("webgl");
var width = canvas.clientWidth;
var height = canvas.clientHeight;
if (canvas.width != width ||canvas.height != height)
{
canvas.width = width;
canvas.height = height;
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
}
}
Related
I have some canvases with sliders and buttons with World Space render mode in my 3d game. Canvases attached to different game objects at different distances from each other.
How can I resize canvases depending on the zoom in or out of the Camera?
Importat! All canvases must do this at the same time.
For example: Canvas should shrink when Camera zoom in or stretch when zoom out.
For now I make it like this (but it seems to me there is a better way):
if (Vector3.Distance(_camera.transform.position, transform.position) < 70)
{
var scaleFactor = Vector3.Lerp(transform.localScale, new Vector3(0.5f, 0.5f, 0.5f), 0.01f);
transform.localScale = scaleFactor;
}
else
{
//Because default canvas scale is 1, 1, 1
var scaleFactor = Vector3.Lerp(transform.localScale, Vector3.one, 0.01f);
transform.localScale = scaleFactor;
}
Just asking, you can use canvas with overlay screen or overlay camera, it will always be on your screen with screen size no matter you zoom in or zoom out or you slide.
Or you can make a parent game object with two childrens in it. One children being the gameobject the canvas is attached to and another being the canvas and apply transform on the parent, It will also work for both the children I guess.
For my game UI requirements, I require to switch TvScreen in Normal and Maximal View/Size based on click of Toggle Button.
I have just tried to assign one Rect Transform to other Rect Transform but it is not working.
It just transferring reference data not actual component data like anchor point, pivot, position, scale and rotation.
I am attaching few images for TvScreen Rect Transform, Normal Screen Size Rect Transform and Maximum Screen Size Rect Transform.
Tv Screen With Its Rect Transform
Maximize Screen Size Rect Transform
Normal Screen Size Rect Transform
At present, I have done this but it is not visually changing Tv Screen size.
public void OnFullscreenMediaButtonClick()
{
SoundManager.Instance.PlayButtonClickSound();
Debug.Log("maximize button clicked: " + isTvScreenMaximized);
if (!isTvScreenMaximized)
{
isTvScreenMaximized = true;
tvScreen = maximizeScreenRectTransform;
}
else
{
isTvScreenMaximized = false;
tvScreen = normalSizeScreenRectTransform;
}
}
I hope you got my question so please give your suggestion related to this.
I notice the 2 RectTransform have different anchor preset. If it's not necessary, set them to the same anchor preset.
void CopyRectTransformSize(RectTransform copyFrom, RectTransform copyTo){
copyTo.anchorMin = copyFrom.anchorMin;
copyTo.anchorMax = copyFrom.anchorMax;
copyTo.anchoredPosition = copyFrom.anchoredPosition;
copyTo.sizeDelta = copyFrom.sizeDelta;
}
then call it like this to copy maximizeScreenRectTransform's size to tvScreen
CopyRectTransformSize(maximizeScreenRectTransform, tvScreen);
I am new in Unity. I'm trying to make a code that will take a screenshot of a object inside scrollview. My problem is that the whole object/image inside the scrollView is not visible in device.YOu have to pan the scrollView to see the hidden part.What I wanna do is to take the screenshot of the whole object/image inside the scrollview. Please help
My code below is
private IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] screenshot = tex.EncodeToPNG();
}
Code above only takes the screenshot that is visible to the device screen. I want to take the screenshot the whole object/image. Please help
Just point a camera with render texture setup towards your object and use the encode function.
Use of render texture and encodeToPng is recommended.
Your code simply creates a texture and reads the first pixel in it and doesn't do anything with it.
Fullscreen I mean the popup usercontrol covers the whole cellphone screen, maximize. can this be done?
thx!
You can get the current width and height by Content.ActualHeight/Width property
here's a code snippet
public void showPopUp()
{
//get heigth and width
double height=Application.Current.Host.Content.ActualHeight;
double width = Application.Current.Host.Content.ActualWidth;
//child content
StackPanel stk = new StackPanel();
stk.Height = height; //set height
stk.Width = width; //set width
stk.Background = new SolidColorBrush(Colors.Red);
TextBlock txtblock = new TextBlock() { FontSize=40, Text="HELLO WORLD", TextWrapping=TextWrapping.Wrap};
stk.Children.Add(txtblock);
Popup _popup = new Popup();
_popup.Child = stk; //set child content
this.LayoutRoot.Children.Add(_popup);
_popup.IsOpen = true;
}
and then you get this result ;)
For Windows Phone 8.1 you can get the current Application width and height by Window.Current.Bounds
double height = Window.Current.Bounds.Height;
double width = Window.Current.Bounds.Width;
I have used a C# method to resize the image size.I have used several methods from the following links.
C# Simple Image Resize : File Size Not Shrinking
shrinking
Resize an Image C#
but quality is missing when re-sizing the image
Please help me to resolve the issue.
You could use WIC (Windows Imaging Component) instead of GDI+. Here's a nice blog post with an example.
First convert your Bitmap to an Image
Bitmap b = new Bitmap("asdf.jpg");
Image i = (Image)b;
then pass the image to this method to resize
public static Image ResizeImage(Image image, int width, int height, bool onlyResizeIfWider)
{
using (image)
{
// Prevent using images internal thumbnail.
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
if (onlyResizeIfWider == true)
if (image.Width <= width)
width = image.Width;
// Resize with height instead?
int newHeight = image.Height * width / image.Width;
if (newHeight > height)
{
width = image.Width * height / image.Height;
newHeight = height;
}
Image NewImage = image.GetThumbnailImage(width, newHeight, null, IntPtr.Zero);
return NewImage;
}
}
I hope this helps.