12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using ET;
- using FairyGUI;
- using UnityEngine;
- namespace GFGGame
- {
- class ShareDataManager : SingletonBase<ShareDataManager>
- {
- //当前分享
- public byte[] imageBytes;
- public string outputImagePath; // 输出图片的路径
- public GGroup group;
- public void ShareImage(string imageUrl)
- {
- // 使用ShareSDK或其他分享插件来分享图片URL
- // 这里需要根据具体插件的API来实现分享功能
- }
- //isJump:是否跳转
- //isCapture:是否截图
- public void CaptureCameraToImage(bool isJump = true,bool isCapture = true)
- {
- Timers.inst.StartCoroutine(CaptureCameraImage(isJump,isCapture));
- }
- private IEnumerator CaptureCameraImage(bool isJump = true, bool isCapture = true)
- {
- if (!isJump)
- {
- group.visible = false;
- }
- yield return new WaitForEndOfFrame();
- SetImageTargetPath();
- if (isCapture)
- {
- 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 = tex.EncodeToPNG();
- }
- // 保存到磁盘
- File.WriteAllBytes(outputImagePath, imageBytes);
- Debug.Log("Camera capture saved to " + outputImagePath);
- if (!isJump)
- {
- group.visible = true;
- }
- if (isJump)
- {
- ViewManager.Show<ShareView>();
- }
- }
- public Texture2D ConvertBytesToTexture(byte[] imageBytes)
- {
- // 创建一个新的Texture2D对象
- Texture2D texture = new Texture2D(UnityEngine.Screen.width, UnityEngine.Screen.height); // 宽度和高度需要根据图片实际尺寸来设置
- texture.LoadImage(imageBytes); // 加载图片数据
- // 应用纹理设置
- texture.Apply();
- return texture;
- }
- private void SetImageTargetPath()
- {
- outputImagePath = Application.persistentDataPath + "/share.png";
- }
- }
- }
|