using System; using System.Collections.Generic; using System.IO; using ET; using FairyGUI; using UnityEngine; namespace GFGGame { class ShareDataManager : SingletonBase { //当前分享 public byte[] imageBytes; public Camera targetCamera; // 指定要读取的摄像机 public string outputImagePath; // 输出图片的路径 public void ShareImage(string imageUrl) { // 使用ShareSDK或其他分享插件来分享图片URL // 这里需要根据具体插件的API来实现分享功能 } public void CaptureCameraToImage(bool isJump = true) { targetCamera = GameObject.Find("Stage Camera").GetComponent(); 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(); // 将Texture2D保存为图片 imageBytes = screenShot.EncodeToPNG(); // 保存到磁盘 File.WriteAllBytes(outputImagePath, imageBytes); // 释放资源 renderTexture.Release(); Resources.UnloadUnusedAssets(); targetCamera.targetTexture = null; Debug.Log("Camera capture saved to " + outputImagePath); if(isJump) { ViewManager.Show(); } } public Texture2D ConvertBytesToTexture(byte[] imageBytes) { // 创建一个新的Texture2D对象 Texture2D texture = new Texture2D(targetCamera.pixelWidth, targetCamera.pixelHeight); // 宽度和高度需要根据图片实际尺寸来设置 texture.LoadImage(imageBytes); // 加载图片数据 // 应用纹理设置 texture.Apply(); return texture; } private void SetImageTargetPath() { #if UNITY_EDITOR outputImagePath = Application.dataPath + "/StreamingAssets" + "/share.png"; #elif UNITY_IPHONE outputImagePath = Application.dataPath+"/Ray"+"/share.png"; #elif UNITY_android outputImagePath = "jar:file://"+Application.dataPath+"!/assets/"+"/share.png"; #endif } } }