
[ad_1]
public class ScreenShotRect : MonoBehaviour {
public RectTransform rectT; // Assign the UI factor which you wanna seize
personal int width; // width of the article to seize
personal int top; // top of the article to seize
personal Transform rectTransformParent; //RectTransform father or mother to reasign after utilization
personal GameObject screenShotGO; //Tempory GameObject to repeat recttransform
//RectTransform values to reasign after utilization
personal Vector2 offsetMinValues;
personal Vector2 offsetMaxValues;
personal Vector3 localScaleValues;
// Update is named as soon as per body
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (rectT != null)
{
if (rectT.root.GetComponent<CanvasScaler>().uiScaleMode == CanvasScaler.ScaleMode.ScaleWithScreenMeasurement)
{
if (rectT.root.GetComponent<CanvasScaler>().screenMatchMode != CanvasScaler.ScreenMatchMode.MatchWidthOrHeight ||
rectT.root.GetComponent<CanvasScaler>().matchWidthOrHeight != 0.5f)
{
Debug.LogWarning("UI might not look the identical attributable to Canvas Scaler both screenMatchMode was not set MatchWidthOrHeight or MatchWidthOrHeight isn't set to 0.5f");
}
createCanvasWithRectTransform();
}
else if (rectT.root.GetComponent<CanvasScaler>().uiScaleMode == CanvasScaler.ScaleMode.ConstantPixelSize)
{
StartCoroutine(takeScreenShot());
}
else
{
Debug.LogWarning("Canvas Scaler mode not supported.");
}
}
else
{
Debug.Log("Rect rework is null to seize the screenshot, therefore fullscreen has been taken.");
Application.CaptureScreenshot("FullPageScreenShot.png");
}
}
}
personal void createCanvasWithRectTransform()
{
rectTransformParent = rectT.father or mother; //Assigning Parent rework to reasign after utilization
//Copying RectTransform values to reasign after switching father or mother
offsetMinValues = rectT.offsetMin;
offsetMaxValues = rectT.offsetMax;
localScaleValues = rectT.localScale;
//Creating secondary CANVAS with required fields
screenShotGO = new GameObject("ScreenShotGO");
screenShotGO.rework.father or mother = null;
Canvas canvas = screenShotGO.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
CanvasScaler canvasScalar = screenShotGO.AddComponent<CanvasScaler>();
canvasScalar.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize;
screenShotGO.AddComponent<GraphicRaycaster>();
rectT.SetParent(screenShotGO.rework); //Assigning seize recttransform to non permanent father or mother gameobject
//Reasigning recttransform values
rectT.offsetMin = offsetMinValues;
rectT.offsetMax = offsetMaxValues;
rectT.localScale = localScaleValues;
Canvas.PressureUpdateCanvases(); // Forcing all canvas to replace the UI
StartCoroutine(takeScreenShot()); //Once the whole lot was set prepared, Capture the screenshot
}
personal IEnumerator takeScreenShot()
{
yield return new WaitForEndOfFrame(); // it should be a coroutine
//Calcualtion for the width and top of the screenshot from recttransform
width = System.Convert.ToInt32(rectT.rect.width);
top = System.Convert.ToInt32(rectT.rect.top);
//Calcualtion for the beginning place of the recttransform to be captured
Vector2 temp = rectT.rework.place;
var startX = temp.x - width / 2;
var startY = temp.y - top / 2;
// Read the pixels from the feel
var tex = new Texture2D(width, top, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(startX, startY, width, top), 0, 0);
tex.Apply();
// break up the method up--ReadPixels() and the GetPixels() name inside the encoder are each fairly heavy
yield return 0;
var bytes = tex.EncodeToPNG();
Destroy(tex);
//Writing bytes to a file
File.WriteAllBytes(Application.dataPath + "/ScreenShotRect/ScreenShot.png", bytes);
//In case of ScaleMode was not ScaleWithScreenMeasurement, father or mother won't be assigned then no must revert the adjustments
if (rectTransformParent != null)
{
//Reasigning gameobject to its unique father or mother group
rectT.SetParent(rectTransformParent);
//Reasigning recttransform values
rectT.offsetMin = offsetMinValues;
rectT.offsetMax = offsetMaxValues;
rectT.localScale = localScaleValues;
//Destorying non permanent created gameobject after utilization
Destroy(screenShotGO);
}
Debug.Log("Picture taken");
}
}
Note: UI might not look the identical when Canvas Scaler both screenMatchMode was not set MatchWidthOrHeight or MatchWidthOrHeight isn’t set to 0.5f
This code is modified based mostly on Shuvro Sarkar‘s reply
[ad_2]