Unity: Having trouble in taking screenshot for the content inside scrollView - unity3d

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.

Related

How to resize / stretch World Space UI?

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.

How can I make sure my Xamarin.Forms iOS custom renderer of my entry uses the element's width?

In my Xamarin.Forms application I use a custom renderer for entries, since I want them to be made of a single, bottom border. The problem is that I can't find out the right code to make the custom renderer use the element's width. Currently, the situation is like this:
As you can see, the bottom border goes far beyond the real element's width, but I don't understand why. Here I found something, but still I don't understand how to fix this and why this happens. My current code for the renderer class is:
public class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.BorderStyle = UITextBorderStyle.None;
var view = Element as CustomEntry;
var borderLayer = new CALayer
{
Frame = new CGRect(0f, Frame.Height + 5, Frame.Width, 1f),
BorderColor = UIColor.Gray.CGColor,
BackgroundColor = UIColor.Magenta.CGColor,
BorderWidth = 13,
MasksToBounds = true
};
Control.Layer.AddSublayer(borderLayer);
}
}
}
The problem seems to be in that Frame.Width. If I set it to 100, for example, the width of the bottom line of the entry is set to 100, but the problem is that, doing so, I'm not able to horizontally center the line. I want this outcome:

How to copy one Rect Transform data to other Rect Transform

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);

Change unity aspect ratio in fullscreen WebGL

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);
}
}

How to screenshot just part of a screen

I need to crop an image to a rectangle with a height of 25 and a width the length of the screen.
I have the image ready and positioned in the center of the screen. I'd like to screenshot ONLY the rectangle.
I followed this answer, but it does not seem to be working for me for some reason.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.width,25), false, 0)
self.view.drawViewHierarchyInRect(CGRectMake(self.view.frame.width,25,view.bounds.size.width,view.bounds.size.height), afterScreenUpdates: true)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
statusImage = image
You are using wrong coordinates. With your code, the drawing starts at the top right corner of the view and draws void space at the right side of it. You should use 0 as X value and a negative Y value. For example, if you want to screenshot a bar starting at Y=50, you should use:
self.view.drawViewHierarchyInRect(CGRectMake(0, -50, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: true)
If you want to draw from the top of the view, just put 0 as Y value as well.