123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net.NetworkInformation;
- using System.Security.Cryptography;
- using UnityEngine;
- public class ShotScreen : MonoBehaviour
- {
- string _name = "";
- public GameObject[] objs;
- public GameObject otherObj;
- // Start is called before the first frame update
- void Start()
- {
-
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- string path = "";
- string destination = "";
- public void StartShot()
- {
- StartCoroutine("ShotAndSave");
- }
- IEnumerator ShotAndSave()
- {
- SetActFa(objs);
- otherObj.SetActive(false);
- yield return null;
- path = Application.streamingAssetsPath + "/Screenshot";
- _name = "/Screenshot_" + GetCurTime() + ".png";
- if (Application.platform == RuntimePlatform.Android)
- {
- destination = "/sdcard/DCIM/ARphoto";
-
- Debug.Log("[lgs] 1");
- if (!Directory.Exists(destination))
- {
- Debug.Log("[lgs] 2");
- Directory.CreateDirectory(destination);
- Debug.Log("[lgs] CreateDirectory" + destination);
-
- }
- Debug.Log("[lgs] 3");
- //Rect rect = new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f);
- //TexShot(rect,destination);
- ScreenCapture.CaptureScreenshot(destination + _name, 0);
- Debug.Log("[lgs] way: "+ destination + _name);
- }
- else if (Application.platform == RuntimePlatform.WindowsEditor)
- {
- Debug.Log("[lgs] 4");
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- Debug.Log("[lgs] CreateDirectory" + path);
- }
- ScreenCapture.CaptureScreenshot(path + _name, 0);
-
- Debug.Log("[lgs] Screenshot:" + _name);
- }
- yield return new WaitForSeconds(0.5f);
- Debug.Log("[lgs] 5");
- SetActTa(objs);
- yield break;
- }
-
- private void TexShot(Rect rect,string filename)
- {
- Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
- // 读取屏幕像素信息并存储为纹理数据,
- screenShot.ReadPixels(rect, 0, 0);
- screenShot.Apply();
- // 然后将这些纹理数据,成一个png图片文件
- byte[] bytes = screenShot.EncodeToPNG();
- filename = filename + _name;
- Debug.Log("[lgs] "+ filename);
- File.WriteAllBytes(filename, bytes);
-
- }
- private void SetActTa(GameObject[] objs)
- {
- for (int i = 0; i < objs.Length; i++)
- {
- if (!objs[i].activeSelf)
- {
- objs[i].SetActive(true);
- }
- }
- }
- private void SetActFa(GameObject[] objs)
- {
- for (int i = 0; i < objs.Length; i++)
- {
- if (objs[i].activeSelf)
- {
- objs[i].SetActive(false);
- }
- }
- }
- string GetCurTime()
- {
- return (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString());
- }
- }
|