using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.UI; public class ScreenCapture : MonoBehaviour { [DllImport("__Internal")] public static extern void ScreenShotData(string data); int number; string filename; public void onValueChanged(bool isOn) { if (isOn) { StartCoroutine(GetShot()); } } public void DisableToggle() { GetComponent().isOn = false; } private void Awake() { filename = Application.streamingAssetsPath + "/ScreenShot"; GetComponent().onValueChanged.AddListener(onValueChanged); } IEnumerator GetShot() { yield return new WaitForEndOfFrame(); int width = Screen.width; int height = Screen.height; Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false); tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, true); tex.Apply(); byte[] imagebytes = tex.EncodeToPNG(); System.IO.File.WriteAllBytes(filename + number++.ToString() + ".png", imagebytes); yield return new WaitForSeconds(1f); DisableToggle(); ResourceLoadWindow.Instance.LoadTextHintWindow("截图已保存", 2); yield return new WaitForSeconds(30.0F); } }