ShareDataManager.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 void ShareImage(string imageUrl)
  16. {
  17. // 使用ShareSDK或其他分享插件来分享图片URL
  18. // 这里需要根据具体插件的API来实现分享功能
  19. }
  20. public void CaptureCameraToImage(bool isJump = true)
  21. {
  22. Timers.inst.StartCoroutine(CaptureCameraImage(isJump));
  23. }
  24. private IEnumerator CaptureCameraImage(bool isJump = true)
  25. {
  26. yield return new WaitForEndOfFrame();
  27. SetImageTargetPath();
  28. Rect rect = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
  29. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
  30. tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
  31. tex.Apply();//保存像素信息
  32. // 将Texture2D保存为图片
  33. imageBytes = tex.EncodeToPNG();
  34. // 保存到磁盘
  35. File.WriteAllBytes(outputImagePath, imageBytes);
  36. Debug.Log("Camera capture saved to " + outputImagePath);
  37. if(isJump)
  38. {
  39. ViewManager.Show<ShareView>();
  40. }
  41. }
  42. public Texture2D ConvertBytesToTexture(byte[] imageBytes)
  43. {
  44. // 创建一个新的Texture2D对象
  45. Texture2D texture = new Texture2D(UnityEngine.Screen.width, UnityEngine.Screen.height); // 宽度和高度需要根据图片实际尺寸来设置
  46. texture.LoadImage(imageBytes); // 加载图片数据
  47. // 应用纹理设置
  48. texture.Apply();
  49. return texture;
  50. }
  51. private void SetImageTargetPath()
  52. {
  53. #if UNITY_EDITOR
  54. outputImagePath = Application.dataPath + "/StreamingAssets" + "/share.png";
  55. #elif UNITY_IPHONE
  56. outputImagePath = Application.dataPath+"/Ray"+"/share.png";
  57. #elif UNITY_android
  58. outputImagePath = "jar:file://"+Application.dataPath+"!/assets/"+"/share.png";
  59. #endif
  60. }
  61. }
  62. }