ShotScreen.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net.NetworkInformation;
  6. using System.Security.Cryptography;
  7. using UnityEngine;
  8. public class ShotScreen : MonoBehaviour
  9. {
  10. string _name = "";
  11. public GameObject[] objs;
  12. public GameObject otherObj;
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. }
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. }
  21. string path = "";
  22. string destination = "";
  23. public void StartShot()
  24. {
  25. StartCoroutine("ShotAndSave");
  26. }
  27. IEnumerator ShotAndSave()
  28. {
  29. SetActFa(objs);
  30. otherObj.SetActive(false);
  31. yield return null;
  32. path = Application.streamingAssetsPath + "/Screenshot";
  33. _name = "/Screenshot_" + GetCurTime() + ".png";
  34. if (Application.platform == RuntimePlatform.Android)
  35. {
  36. destination = "/sdcard/DCIM/ARphoto";
  37. Debug.Log("[lgs] 1");
  38. if (!Directory.Exists(destination))
  39. {
  40. Debug.Log("[lgs] 2");
  41. Directory.CreateDirectory(destination);
  42. Debug.Log("[lgs] CreateDirectory" + destination);
  43. }
  44. Debug.Log("[lgs] 3");
  45. //Rect rect = new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f);
  46. //TexShot(rect,destination);
  47. ScreenCapture.CaptureScreenshot(destination + _name, 0);
  48. Debug.Log("[lgs] way: "+ destination + _name);
  49. }
  50. else if (Application.platform == RuntimePlatform.WindowsEditor)
  51. {
  52. Debug.Log("[lgs] 4");
  53. if (!Directory.Exists(path))
  54. {
  55. Directory.CreateDirectory(path);
  56. Debug.Log("[lgs] CreateDirectory" + path);
  57. }
  58. ScreenCapture.CaptureScreenshot(path + _name, 0);
  59. Debug.Log("[lgs] Screenshot:" + _name);
  60. }
  61. yield return new WaitForSeconds(0.5f);
  62. Debug.Log("[lgs] 5");
  63. SetActTa(objs);
  64. yield break;
  65. }
  66. private void TexShot(Rect rect,string filename)
  67. {
  68. Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
  69. // 读取屏幕像素信息并存储为纹理数据,
  70. screenShot.ReadPixels(rect, 0, 0);
  71. screenShot.Apply();
  72. // 然后将这些纹理数据,成一个png图片文件
  73. byte[] bytes = screenShot.EncodeToPNG();
  74. filename = filename + _name;
  75. Debug.Log("[lgs] "+ filename);
  76. File.WriteAllBytes(filename, bytes);
  77. }
  78. private void SetActTa(GameObject[] objs)
  79. {
  80. for (int i = 0; i < objs.Length; i++)
  81. {
  82. if (!objs[i].activeSelf)
  83. {
  84. objs[i].SetActive(true);
  85. }
  86. }
  87. }
  88. private void SetActFa(GameObject[] objs)
  89. {
  90. for (int i = 0; i < objs.Length; i++)
  91. {
  92. if (objs[i].activeSelf)
  93. {
  94. objs[i].SetActive(false);
  95. }
  96. }
  97. }
  98. string GetCurTime()
  99. {
  100. 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());
  101. }
  102. }