using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class XunJianPhotos : MonoBehaviour { public RawImage rawImage; public Image screenshotImage; public Button saveBtn; public Button cancelBtn; public Image showImage; // Start is called before the first frame update void Start() { cancelBtn.onClick.AddListener(() => { gameObject.SetActive(false); }); saveBtn.onClick.AddListener(() => { XunJianInitialize.Instance.xjDetails.SetShowImage(SwitchSprite(rawImage)); gameObject.SetActive(false); }); } public void SetImage(Image image) { this.showImage = image; } private void OnEnable() { XRRGBCamera.Instance.playCamera(1280, 720); rawImage.texture = XRRGBCamera.Instance.CaptureImage; } public Texture2D SwitchSprite(RawImage rawImage) { //SaveLocal(rawImage, "1", "0"); // 获取RawImage的纹理 Texture2D rawTexture = TextureToTexture2D(rawImage.texture); // 创建一个新的纹理,用于截图 Texture2D screenshot = new Texture2D(rawTexture.width, rawTexture.height); // 截取RawImage的画面,并应用到新的纹理上 screenshot.SetPixels(rawTexture.GetPixels()); screenshot.Apply(); return screenshot; } /// /// 运行模式下Texture转换成Texture2D /// /// /// public Texture2D TextureToTexture2D(Texture texture) { Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false); RenderTexture currentRT = RenderTexture.active; RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32); Graphics.Blit(texture, renderTexture); RenderTexture.active = renderTexture; texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); texture2D.Apply(); RenderTexture.active = currentRT; RenderTexture.ReleaseTemporary(renderTexture); return texture2D; } }