SaveShot.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using LitJson;
  6. using SUIFW;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. public class SaveShot : MonoBehaviour
  11. {
  12. private Camera screenShotCamera;
  13. public GameObject lineParent;
  14. public GameObject imageParent;
  15. public static byte[] bytes = null;
  16. private void Start()
  17. {
  18. if (UIMaskMgr.GetInstance()._UICamear)
  19. {
  20. screenShotCamera = UIMaskMgr.GetInstance()._UICamear;
  21. }
  22. }
  23. public void ClickJietu()
  24. {
  25. //Debug.Log("保存截图");
  26. if (RoomMainForms.Instance)
  27. {
  28. RoomMainForms.Instance.AllNeedShowUI.gameObject.SetActive(false);
  29. }
  30. PaintLine.canUse = false;
  31. Rect rect = new Rect(0, 0, Screen.width, Screen.height);
  32. Texture2D shots = ScreenShot(screenShotCamera, rect);
  33. bytes = shots.EncodeToPNG();
  34. WSHandler.Rtc.uploadCert();
  35. if (lineParent.transform.childCount > 0)
  36. {
  37. for (int i = 0; i < lineParent.transform.childCount; i++)
  38. {
  39. Destroy(lineParent.transform.GetChild(i).gameObject);
  40. }
  41. }
  42. if (imageParent.transform.childCount > 0)
  43. {
  44. for (int i = 0; i < imageParent.transform.childCount; i++)
  45. {
  46. Destroy(imageParent.transform.GetChild(i).gameObject);
  47. }
  48. }
  49. if (RoomMainForms.Instance)
  50. {
  51. RoomMainForms.Instance.JieTuWancheng();
  52. }
  53. }
  54. public Texture2D ScreenShot(Camera camera, Rect rect)
  55. {
  56. RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
  57. camera.targetTexture = rt;
  58. camera.Render();
  59. RenderTexture.active = rt;
  60. Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
  61. screenShot.ReadPixels(rect, 0, 0);
  62. screenShot.Apply();
  63. camera.targetTexture = null;
  64. RenderTexture.active = null;
  65. GameObject.Destroy(rt);
  66. //Debug.Log("保存图片成功");
  67. #if UNITY_EDITOR
  68. UnityEditor.AssetDatabase.Refresh();//刷新,这步很关键,否则后面调用图片时没有。
  69. #endif
  70. return screenShot;
  71. }
  72. }