12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using LitJson;
- using SUIFW;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UI;
- public class SaveShot : MonoBehaviour
- {
- private Camera screenShotCamera;
- public GameObject lineParent;
- public GameObject imageParent;
- public static byte[] bytes = null;
- private void Start()
- {
- if (UIMaskMgr.GetInstance()._UICamear)
- {
- screenShotCamera = UIMaskMgr.GetInstance()._UICamear;
- }
- }
- public void ClickJietu()
- {
- //Debug.Log("保存截图");
- if (RoomMainForms.Instance)
- {
- RoomMainForms.Instance.AllNeedShowUI.gameObject.SetActive(false);
- }
- PaintLine.canUse = false;
- Rect rect = new Rect(0, 0, Screen.width, Screen.height);
- Texture2D shots = ScreenShot(screenShotCamera, rect);
- bytes = shots.EncodeToPNG();
- WSHandler.Rtc.uploadCert();
- if (lineParent.transform.childCount > 0)
- {
- for (int i = 0; i < lineParent.transform.childCount; i++)
- {
- Destroy(lineParent.transform.GetChild(i).gameObject);
- }
- }
- if (imageParent.transform.childCount > 0)
- {
- for (int i = 0; i < imageParent.transform.childCount; i++)
- {
- Destroy(imageParent.transform.GetChild(i).gameObject);
- }
- }
- if (RoomMainForms.Instance)
- {
- RoomMainForms.Instance.JieTuWancheng();
- }
- }
-
- public Texture2D ScreenShot(Camera camera, Rect rect)
- {
- RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
- camera.targetTexture = rt;
- camera.Render();
- RenderTexture.active = rt;
- Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
- screenShot.ReadPixels(rect, 0, 0);
- screenShot.Apply();
- camera.targetTexture = null;
- RenderTexture.active = null;
- GameObject.Destroy(rt);
- //Debug.Log("保存图片成功");
- #if UNITY_EDITOR
- UnityEditor.AssetDatabase.Refresh();//刷新,这步很关键,否则后面调用图片时没有。
- #endif
-
- return screenShot;
- }
- }
|