当前位置:网站首页>Untiy3d controls scene screenshots through external JSON files
Untiy3d controls scene screenshots through external JSON files
2022-06-30 05:04:00 【zjh_ three hundred and sixty-eight】
{
"SceneName":"PrintScreen",
"Position":{"x":0,"y":10,"z":20},
"Rotation":{"x":0,"y":0,"z":0},
"ScreenWidth":1024,
"ScreenHeight":768,
"ImageName":"Test01.png",
"OutPath":"C://OutPath"
}It's on it Json File data , Through the data above , Let's control the capture of a partial picture of a scene , The above parameters are :
SceneName: Scene name
Position: Camera position
Rotation : The camera faces
ScreenWidth: Picture width
ScreenHight: Picture height
ImageName: The name of the picture
OutPath: Where the picture is saved
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
[Serializable]
public class ListRot
{
public float x;
public float y;
public float z;
}
public class JsonManage
{
public string SceneName;
public ListRot Position;
public ListRot Rotation;
public int ScreenWidth;
public int ScreenHeight;
public string ImageName;
public string OutPath;
}
public class PrintScreen : MonoBehaviour
{
private Camera camera;
private Rect rect;
JsonManage jm;
AsyncOperation async;
int num ;
void Start()
{
num = 0;
camera = Camera.main;
// Read json Information
jm = JsonUtility.FromJson<JsonManage>(File.ReadAllText("C:\\PrintScreen.json"));
// parameter assignment
camera.transform.position = new Vector3(jm.Position.x, jm.Position.y, jm.Position.z);
camera.transform.eulerAngles = new Vector3(jm.Rotation.x, jm.Rotation.y, jm.Rotation.z);
rect.width = jm.ScreenWidth;
rect.height = jm.ScreenHeight;
// Jump to the scene and keep Camera object
DontDestroyOnLoad(this);
async = SceneManager.LoadSceneAsync(jm.SceneName);
}
private void Update()
{
// Judge whether the scene jump is successful , Screenshot once
if (async.isDone && num == 0)
{
num++;
Capture3();
}
}
//private void OnGUI()
//{
// if(GUI.Button(new Rect(10, 10, 100, 50), " Screenshot "))
// {
// print(" Start screenshot ");
// Capture3();
// }
//}
public Texture2D Capture3()
{
// Create a RenderTexture object
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// Temporarily set the of the relevant camera targetTexture by rt, And manually render the relevant camera
camera.targetTexture = rt;
camera.Render();
//ps: --- If you add a second camera , You can only take a screenshot of the image seen by several specified Cameras .
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// Activate this rt, And read pixels from it .
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);// notes : This is the time , It's from RenderTexture.active Read the picture in the middle school
screenShot.Apply();
// Reset relevant parameters , To use the camera Continue to display... On the screen
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
// Finally, these texture data , Become a png Picture file
byte[] bytes = screenShot.EncodeToPNG();
// Automatically create a folder when it does not exist
if (!Directory.Exists(jm.OutPath))
{
Directory.CreateDirectory(jm.OutPath);
}
//string filename = Application.dataPath + "/Screenshot3.png";
string filename = jm.OutPath + @"/" + jm.ImageName +".png";
System.IO.File.WriteAllBytes(filename, bytes);
//AssetDatabase.Refresh();
Debug.Log(string.Format(" Screenshot of a picture : {0}", filename));
return screenShot;
}
} Operation method :
1、 Write well Json Files in C Under the plate ( In this case Json The document is placed in "C:\\PrintScreen.json")
2、 Create an empty scene and hang this script on MainCamera On
3、 Create another scene that needs a screenshot , The scene name needs to be followed by Json The first parameter of the file is consistent ( When running, it is necessary to ensure that all scenarios are added BuildSettings in )
4、 Run to get a screenshot , Save in the specified location ( The screenshot of this case is saved in C:\OutPath)
This case can control the screenshot of any scene , Just need to be outside Json Just modify it in the file ,capture3() The screenshot code comes from the network , Thank you for your sharing .
边栏推荐
- Unity3d lookat parameter description
- Autowired注解警告的解决办法
- Tensorflow2 of ubantu18.04 X installation
- Unity/ue reads OPC UA and OPC Da data (UE4)
- Ripple effect of mouse click (unity & shader)
- Yolov5 torch installation
- 产生 BUG 测试人员需要自己去分析原因吗?
- Some books you should not miss when you are new to the workplace
- Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
- Unit screenshot saved on the phone
猜你喜欢

Introduction to some representations, neighbors and degrees of Graphs

Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives

Approaching history, introduction to the London Guard Museum

Royal Albert Hall, a popular landmark in London

Unity is associated with vs. there is a compiler problem when opening

Unity/ue reads OPC UA and OPC Da data (UE4)

Some books you should not miss when you are new to the workplace

Ripple effect of mouse click (unity & shader)

PWN入门(2)栈溢出基础

PS1 Contemporary Art Center, Museum of modern art, New York
随机推荐
Pycharm database tool
Win10 vs2015 compiling curaengine
One command to run rancher
Unity dotween plug-in description
JS 数组的排序 sort方法详解
How to install win7 on AMD Ruilong CPU A320 series motherboard
Unity profiler performance analysis
中文版PyCharm改为英文版PyCharm
Nestjs入门和环境搭建
A collection of errors encountered in machine learning with unity
0 basic unity course. Bricklaying
Spring Festival Tourism Strategy: welcome the new year in Bangkok, Thailand
Unity notes_ SQL Function
harbor api 2.0查询
Some problems encountered in unity steamvr
ParticleSystem in the official Manual of unity_ Collision module
力扣704. 二分查找
Records of some problems encountered during unity development (continuously updated)
Connect() and disconnect() of socket in C #
Detailed explanation of sorting sort method of JS array