当前位置:网站首页>Unit screenshot saved on the phone
Unit screenshot saved on the phone
2022-06-30 04:42:00 【ToDoNothing】
Go straight to the code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class ScreenShot : MonoBehaviour
{
// This camera is used for screen capture , Camera's Claer Flags The attribute of is selected as Depth Only
public Camera CameraTrans;
private Texture2D mTexture1;
private string mPath;
public GameObject picture;
private string mingming;
// Use this for initialization
void Start()
{
CameraTrans = GameObject.FindGameObjectWithTag("ARCamera").GetComponent<Camera>();ji
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// Turn on the camera
/// </summary>
public void screenshot()
{
// Name with time
string mingming = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day + System.DateTime.Now.Hour + System.DateTime.Now.Minute + System.DateTime.Now.Second;
// Platform judgment
if (Application.platform == RuntimePlatform.Android)
{
// Photo album path of Android device
mPath = "/sdcard/DCIM/Camera/" + mingming + ".jpg";
Debug.Log(mPath);
}
else if (Application.platform == RuntimePlatform.WindowsEditor)
{
mPath = Application.dataPath + "\\Resources\\" + mingming + ".jpg";
Debug.Log(mPath);
}
StartCoroutine(CaptureByCamera(CameraTrans, new Rect(0, 0, 1024, 768), mPath));
}
private IEnumerator CaptureByCamera(Camera mCamera, Rect mRect, string mFileName)
{
// Wait for the rendering thread to end
yield return new WaitForEndOfFrame();
// initialization RenderTexture
RenderTexture mRender = new RenderTexture((int)mRect.width, (int)mRect.height, 0);
// Set the render target of the camera
mCamera.targetTexture = mRender;
// Start rendering
mCamera.Render();
// Activate render map to read information
RenderTexture.active = mRender;
Texture2D mTexture = new Texture2D((int)mRect.width, (int)mRect.height, TextureFormat.RGB24, false);
// The screen pixel information is read and stored as texture data
mTexture.ReadPixels(mRect, 0, 0);
// application
mTexture.Apply();
// Release the camera , Destroy render maps
mCamera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(mRender);
// Encode the picture information into byte information
byte[] bytes = mTexture.EncodeToPNG();
// preservation
File.WriteAllBytes(mFileName, bytes);
// Store the texture map in the static class
mTexture1 = mTexture;
// picture.GetComponent<Image>().sprite = Sprite.Create(mTexture1, new Rect(0, 0, 1024, 768), new Vector2(Screen.height / 2, Screen.width / 2));
// If necessary, you can return to the screenshot
//return mTexture;
}
// Start is called before the first frame update
public void OnScreenShotClick()
{
//if (picture == null) return;
Debug.Log(" Screenshot ");
screenshot();
}
public void ScreenShotBtn()
{
//StartCoroutine(CaptureByRect());
}
}
边栏推荐
- SSL universal domain name certificate
- Webots notes day 2
- Tea mall system based on SSM framework [project source code + database script + report]
- OneNote software
- 基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
- Network layer protocol hardware
- Difference between request forwarding and redirection
- 為什麼win10開熱點後電腦沒有網絡?
- Detailed explanation of cookies and sessions
- Difference between TCP three handshakes and four waves and tcp/udp
猜你喜欢

图的一些表示方式、邻居和度的介绍

OneNote production schedule

Dual domain SSL certificate

Geotrustov wildcard

Foreign SSL certificate

史上最全的Redis基础+进阶项目实战总结笔记

什么是光耦电路,实际使用中应该注意些什么?

How to apply for SSL certificate from the manufacturer

Bean creation process and lazy init delay loading mechanism

Directory operations and virtual file systems
随机推荐
Is the Flink connector JDBC open source? Where can I download it
Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
IO stream, character read / write, copy
internship:接口案例实现
What is the difference between synchronized and lock
[fpga] implementation of IIC read / write EEPROM
Salary management system based on servlet+jsp+mysql [source code + database]
Junior students summarize JS basic interview questions
Geotrustov wildcard
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
FortiGate firewall configuration log uploading regularly
【Paper】2015_ Coordinated cruise control for high-speed train movements based on a multi-agent model
Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
Qt Creator 8 Beta2发布
Process architecture and process management
Directory operations and virtual file systems
Mongodb learning
Redis实现短信登入功能(二)Redis实现登入功能
Matlab reads fig file and restores signal
How the FortiGate firewall rejects a port by using the local in policy policy