|
@@ -1,4 +1,5 @@
|
|
|
using System;
|
|
|
+using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using ET;
|
|
@@ -11,7 +12,6 @@ namespace GFGGame
|
|
|
{
|
|
|
//当前分享
|
|
|
public byte[] imageBytes;
|
|
|
- public Camera targetCamera; // 指定要读取的摄像机
|
|
|
public string outputImagePath; // 输出图片的路径
|
|
|
|
|
|
public void ShareImage(string imageUrl)
|
|
@@ -21,36 +21,23 @@ namespace GFGGame
|
|
|
}
|
|
|
public void CaptureCameraToImage(bool isJump = true)
|
|
|
{
|
|
|
- targetCamera = GameObject.Find("Stage Camera").GetComponent<Camera>();
|
|
|
+ Timers.inst.StartCoroutine(CaptureCameraImage(isJump));
|
|
|
+ }
|
|
|
+ private IEnumerator CaptureCameraImage(bool isJump = true)
|
|
|
+ {
|
|
|
+ yield return new WaitForEndOfFrame();
|
|
|
SetImageTargetPath();
|
|
|
- // 创建RenderTexture
|
|
|
- RenderTexture renderTexture = new RenderTexture(targetCamera.pixelWidth, targetCamera.pixelHeight, 24);
|
|
|
- targetCamera.targetTexture = renderTexture;
|
|
|
-
|
|
|
- // 强制摄像机渲染
|
|
|
- targetCamera.Render();
|
|
|
-
|
|
|
- // 创建Texture2D
|
|
|
- Texture2D screenShot = new Texture2D(targetCamera.pixelWidth, targetCamera.pixelHeight, TextureFormat.RGBA32, false);
|
|
|
- RenderTexture.active = renderTexture;
|
|
|
- screenShot.ReadPixels(new Rect(0, 0, targetCamera.pixelWidth, targetCamera.pixelHeight), 0, 0);
|
|
|
- screenShot.Apply();
|
|
|
-
|
|
|
- // 将RenderTexture中的像素数据读取到Texture2D
|
|
|
- screenShot.ReadPixels(new Rect(0, 0, targetCamera.pixelWidth, targetCamera.pixelHeight), 0, 0);
|
|
|
- screenShot.Apply();
|
|
|
+ Rect rect = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
|
|
|
+ Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
|
|
|
+ tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
|
|
|
+ tex.Apply();//保存像素信息
|
|
|
|
|
|
// 将Texture2D保存为图片
|
|
|
- imageBytes = screenShot.EncodeToPNG();
|
|
|
+ imageBytes = tex.EncodeToPNG();
|
|
|
|
|
|
// 保存到磁盘
|
|
|
File.WriteAllBytes(outputImagePath, imageBytes);
|
|
|
|
|
|
- // 释放资源
|
|
|
- renderTexture.Release();
|
|
|
- Resources.UnloadUnusedAssets();
|
|
|
- targetCamera.targetTexture = null;
|
|
|
-
|
|
|
Debug.Log("Camera capture saved to " + outputImagePath);
|
|
|
if(isJump)
|
|
|
{
|
|
@@ -61,7 +48,7 @@ namespace GFGGame
|
|
|
public Texture2D ConvertBytesToTexture(byte[] imageBytes)
|
|
|
{
|
|
|
// 创建一个新的Texture2D对象
|
|
|
- Texture2D texture = new Texture2D(targetCamera.pixelWidth, targetCamera.pixelHeight); // 宽度和高度需要根据图片实际尺寸来设置
|
|
|
+ Texture2D texture = new Texture2D(UnityEngine.Screen.width, UnityEngine.Screen.height); // 宽度和高度需要根据图片实际尺寸来设置
|
|
|
texture.LoadImage(imageBytes); // 加载图片数据
|
|
|
|
|
|
// 应用纹理设置
|