XunJianPhotos.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class XunJianPhotos : MonoBehaviour
  6. {
  7. public RawImage rawImage;
  8. public Image screenshotImage;
  9. public Button saveBtn;
  10. public Button cancelBtn;
  11. public Image showImage;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. cancelBtn.onClick.AddListener(() =>
  16. {
  17. gameObject.SetActive(false);
  18. });
  19. saveBtn.onClick.AddListener(() =>
  20. {
  21. XunJianInitialize.Instance.xjDetails.SetShowImage(SwitchSprite(rawImage));
  22. gameObject.SetActive(false);
  23. });
  24. }
  25. public void SetImage(Image image)
  26. {
  27. this.showImage = image;
  28. }
  29. private void OnEnable()
  30. {
  31. XRRGBCamera.Instance.playCamera(1280, 720);
  32. rawImage.texture = XRRGBCamera.Instance.CaptureImage;
  33. }
  34. public Texture2D SwitchSprite(RawImage rawImage)
  35. {
  36. //SaveLocal(rawImage, "1", "0");
  37. // 获取RawImage的纹理
  38. Texture2D rawTexture = TextureToTexture2D(rawImage.texture);
  39. // 创建一个新的纹理,用于截图
  40. Texture2D screenshot = new Texture2D(rawTexture.width, rawTexture.height);
  41. // 截取RawImage的画面,并应用到新的纹理上
  42. screenshot.SetPixels(rawTexture.GetPixels());
  43. screenshot.Apply();
  44. return screenshot;
  45. }
  46. /// <summary>
  47. /// 运行模式下Texture转换成Texture2D
  48. /// </summary>
  49. /// <param name="texture"></param>
  50. /// <returns></returns>
  51. public Texture2D TextureToTexture2D(Texture texture)
  52. {
  53. Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
  54. RenderTexture currentRT = RenderTexture.active;
  55. RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
  56. Graphics.Blit(texture, renderTexture);
  57. RenderTexture.active = renderTexture;
  58. texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  59. texture2D.Apply();
  60. RenderTexture.active = currentRT;
  61. RenderTexture.ReleaseTemporary(renderTexture);
  62. return texture2D;
  63. }
  64. }