ShareDataManager.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using ET;
  6. using FairyGUI;
  7. using UnityEngine;
  8. namespace GFGGame
  9. {
  10. class ShareDataManager : SingletonBase<ShareDataManager>
  11. {
  12. //当前分享
  13. public byte[] imageBytes;
  14. public string outputImagePath; // 输出图片的路径
  15. public GGroup group;
  16. public void ShareImage(string imageUrl)
  17. {
  18. // 使用ShareSDK或其他分享插件来分享图片URL
  19. // 这里需要根据具体插件的API来实现分享功能
  20. }
  21. //isJump:是否跳转
  22. //isCapture:是否截图
  23. public void CaptureCameraToImage(bool isJump = true,bool isCapture = true)
  24. {
  25. Timers.inst.StartCoroutine(CaptureCameraImage(isJump,isCapture));
  26. }
  27. private IEnumerator CaptureCameraImage(bool isJump = true, bool isCapture = true)
  28. {
  29. if (!isJump)
  30. {
  31. group.visible = false;
  32. }
  33. yield return new WaitForEndOfFrame();
  34. SetImageTargetPath();
  35. if (isCapture)
  36. {
  37. Rect rect = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
  38. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
  39. tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
  40. tex.Apply();//保存像素信息
  41. // 将Texture2D保存为图片
  42. imageBytes = tex.EncodeToPNG();
  43. }
  44. // 保存到磁盘
  45. File.WriteAllBytes(outputImagePath, imageBytes);
  46. Debug.Log("Camera capture saved to " + outputImagePath);
  47. if (!isJump)
  48. {
  49. group.visible = true;
  50. }
  51. if (isJump)
  52. {
  53. ViewManager.Show<ShareView>();
  54. }
  55. }
  56. public Texture2D ConvertBytesToTexture(byte[] imageBytes)
  57. {
  58. // 创建一个新的Texture2D对象
  59. Texture2D texture = new Texture2D(UnityEngine.Screen.width, UnityEngine.Screen.height); // 宽度和高度需要根据图片实际尺寸来设置
  60. texture.LoadImage(imageBytes); // 加载图片数据
  61. // 应用纹理设置
  62. texture.Apply();
  63. return texture;
  64. }
  65. private void SetImageTargetPath()
  66. {
  67. outputImagePath = Application.persistentDataPath + "/share.png";
  68. }
  69. }
  70. }