using Assets.Framework.Tool; using Assets.MyGame.Scripts.Net; using System; using System.Collections; using System.Collections.Generic; using System.Text; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.Networking; using UnityEngine.UI; public class UIUserInfo : UIBase { public Button btnA, btnB; public Text textTitleLabel; //public Image showImage; /// /// 选择或拍摄照片 /// /// 裁剪后的照片处理 public void AddImage(Action action) { // 弹出一个选择框,用户可以选择拍照或者从相册选择 // 这里用简单的按钮逻辑,你可以替换成自己的 UI ShowChoiceDialog("选择图片来源", "拍照", "相册", () => { // 拍照 NativeCamera.TakePicture((path) => { if (!string.IsNullOrEmpty(path)) { StartCoroutine(LoadAndCropImage(path,action)); } else { Debug.LogError("未拍摄图片"); } }); }, () => { // 相册 NativeGallery.GetImageFromGallery((path) => { if (!string.IsNullOrEmpty(path)) { StartCoroutine(LoadAndCropImage(path,action)); } else { Debug.LogError("未选择图片"); } }); } ); } private void ShowChoiceDialog(string label, string btnTXTA, string btnTXTB, UnityAction actionA, UnityAction actionB) { textTitleLabel.transform.parent.gameObject.SetActive(true); btnA.onClick.RemoveAllListeners(); btnB.onClick.RemoveAllListeners(); btnA.GetComponentInChildren().text = btnTXTA; btnB.GetComponentInChildren().text = btnTXTB; btnA.onClick.AddListener(actionA); btnA.onClick.AddListener(() => { textTitleLabel.transform.parent.gameObject.SetActive(false); }); btnB.onClick.AddListener(actionB); btnB.onClick.AddListener(() => { textTitleLabel.transform.parent.gameObject.SetActive(false); }); textTitleLabel.text = label; } private IEnumerator LoadAndCropImage(string path,Action action) { using (UnityEngine.Networking.UnityWebRequest req = UnityEngine.Networking.UnityWebRequestTexture.GetTexture("file://" + path)) { yield return req.SendWebRequest(); if (req.result == UnityEngine.Networking.UnityWebRequest.Result.Success) { Texture2D tex = UnityEngine.Networking.DownloadHandlerTexture.GetContent(req); ImageCropper.Instance.Show(tex, (bool result, Texture originalImage, Texture2D croppedImage) => { if (result && croppedImage != null) { // 创建一个新的可读纹理副本 Texture2D readableTex = MakeReadable(croppedImage); //// 显示裁剪后的图片 //这里设计成1确认2取消 //showImage.GetComponent().sprite = Sprite.Create( // readableTex, // new Rect(0, 0, readableTex.width, readableTex.height), // new Vector2(0.5f, 0.5f) //); //这里暂时直接确认 action?.Invoke(readableTex); } }, settings: new ImageCropper.Settings() { ovalSelection = false, autoZoomEnabled = true, selectionMinAspectRatio = 1f, selectionMaxAspectRatio = 1f }); } else { Debug.LogError("图片加载失败: " + req.error); } } } private Texture2D MakeReadable(Texture2D src) { // 使用默认 RenderTextureFormat,避免 Linear/Gamma 不匹配 RenderTexture rt = RenderTexture.GetTemporary(src.width, src.height, 0, RenderTextureFormat.Default); Graphics.Blit(src, rt); RenderTexture previous = RenderTexture.active; RenderTexture.active = rt; Texture2D readableTex = new Texture2D(src.width, src.height, TextureFormat.RGBA32, false); readableTex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); readableTex.Apply(); RenderTexture.active = previous; RenderTexture.ReleaseTemporary(rt); return readableTex; } }