123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using SUIFW;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UI;
- public class jietu : MonoBehaviour
- {
- private Camera screenShotCamera;
- private Texture shots;
- public Transform imageParent;
- public RawImage jietuBG;
- private void Start()
- {
- if (UIMaskMgr.GetInstance()._UICamear)
- {
- screenShotCamera = UIMaskMgr.GetInstance()._UICamear;
- }
- }
- public void ClickJietu()
- {
- if (screenShotCamera && RoomMainForms.Instance)
- {
- //Debug.Log("截屏");
- RoomMainForms.Instance.CkickOnJieTuBtn();
- PaintLine.canUse = false;
- Rect rect = new Rect(0, 0, Screen.width, Screen.height);
- shots = ScreenShot(screenShotCamera, rect);
-
-
- }
- }
- // <summary>
- /// 传入一个 RectTransform 区域!, 就不把资格区域的图像保存下来
- /// </summary>
- /// <param name="rect"></param>
- 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.RGB24, false);
- screenShot.ReadPixels(rect, 0, 0);
- screenShot.Apply();
- camera.targetTexture = null;
- RenderTexture.active = null;
- GameObject.Destroy(rt);
-
-
- //Debug.Log("截图成功");
- /*
- byte[] bytes = screenShot.EncodeToPNG();
- string filename = Application.dataPath + "/Resources/ScreenShot/screenshot.png";//存放路径
- File.WriteAllBytes(filename, bytes);
- //TextureImporter texture = AssetImporter.GetAtPath(filename) as TextureImporter;
- //texture.textureType = TextureImporterType.Sprite;*/
- #if UNITY_EDITOR
- UnityEditor.AssetDatabase.Refresh();//刷新,这步很关键,否则后面调用图片时没有。
- #endif
- Invoke("CreateImage", 0.5f);
- return screenShot;
-
- }
- void CreateImage()//在canvas显示截屏后的图片
- {
- if (RoomMainForms.Instance)
- {
- RoomMainForms.Instance.ShowShotsAllBtn();
- }
- GameObject Im = new GameObject();
- Im.name = "jieping";
- Im.transform.SetParent(imageParent, false);
- RawImage rawImage = Im.AddComponent<RawImage>();//添加RawImage属性
- if (shots != null && rawImage)
- {
- rawImage.texture = shots;//将图片赋到RawImage上
- rawImage.rectTransform.sizeDelta = new Vector2(shots.width, shots.height);//定义图片最初显示尺寸
- rawImage.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
- rawImage.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
- }
- }
- }
|