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);
}
}
//
/// 传入一个 RectTransform 区域!, 就不把资格区域的图像保存下来
///
///
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属性
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);
}
}
}