Files
5.24BuYu/Assets/Scripts/UI/UIUserInfo.cs
T

187 lines
6.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class UIUserInfo : UIBase
{
// public Button btn_CloseBtn;
// public Button btn_Title;
// public Text txt_YinPiao;
// public Text txt_Name;
// Image img_Title;
// [Header("TitleChangePanel")]
// public GameObject obj_TitlePrefab;
// public Transform tra_TitleParent;
// //public void AddImage()
// //{
// // NativeGallery.GetImageFromGallery((path) =>
// // {
// // Debug.Log(path);
// // if (string.IsNullOrEmpty(path))
// // {
// // Debug.LogError("未选择图片");
// // return;
// // }
// // // 回调里不能 yield,所以启动协程
// // StartCoroutine(DownloadTextureWithRetry(path, 5, (tex) =>
// // {
// // if (tex != null)
// // {
// // Debug.Log("Image Down Ok");
// // StartCoroutine(DefaultTools.UploadingImage(DataManager.Instance.settingData.uid.ToString(), tex.EncodeToPNG()));
// // //WebSocketManager.Instance.SendMessageToUrl(UrlType.defaultType, new { CMD = 11011, avatar_id = tex.EncodeToPNG() });
// // // TODO: 使用 tex
// // //DataManager.Instance.myPlayerInfo.titleSprite = Sprite.Create(tex, new Rect(0, 0, 110, 110), new Vector2(0.5f, 0.5f));
// // }
// // else
// // {
// // UIManager.Instance.ShowLog("图片下载失败 5 次");
// // }
// // }));
// // });
// //}
// //public RawImage previewImage; // 用来显示裁剪后的头像
// public void AddImage()
// {
// NativeGallery.GetImageFromGallery((path) =>
// {
// if (string.IsNullOrEmpty(path))
// {
// Debug.LogError("未选择图片");
// return;
// }
// // 启动协程加载图片
// StartCoroutine(LoadAndCropImage(path));
// });
// }
// private IEnumerator LoadAndCropImage(string path)
// {
// 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 = new Texture2D(croppedImage.width, croppedImage.height, TextureFormat.RGBA32, false);
// Graphics.CopyTexture(croppedImage, readableTex); // 复制像素数据到可读纹理
// // 显示裁剪后的图片
// btn_Title.GetComponent<Image>().sprite = Sprite.Create(
// readableTex,
// new Rect(0, 0, readableTex.width, readableTex.height),
// new Vector2(0.5f, 0.5f)
// );
// // 上传服务器
// StartCoroutine(DefaultTools.UploadingImage(
// DataManager.Instance.settingData.uid.ToString(),
// readableTex.EncodeToPNG()
// ));
// }
// },
//settings: new ImageCropper.Settings()
//{
// ovalSelection = false,
// autoZoomEnabled = true,
// selectionMinAspectRatio = 1f,
// selectionMaxAspectRatio = 1f
//});
// }
// else
// {
// Debug.LogError("图片加载失败: " + req.error);
// }
// }
// }
// private IEnumerator DownloadTextureWithRetry(string url, int retryCount, System.Action<Texture2D> callback)
// {
// //url = "file://" + url;
// Texture2D tex = null;
// for (int i = 0; i < retryCount; i++)
// {
// using (UnityWebRequest req = UnityWebRequestTexture.GetTexture("file://" + url))
// {
// yield return req.SendWebRequest();
// if (req.result == UnityWebRequest.Result.Success)
// {
// tex = DownloadHandlerTexture.GetContent(req);
// break;
// }
// else
// {
// Debug.LogError($"Down Failed {i + 1}/{retryCount} 次: {req.error}" + url);
// try
// {
// byte[] bytes = System.IO.File.ReadAllBytes(url);
// tex.LoadImage(bytes);
// if (tex != null)
// {
// Debug.LogError($"Down Failed {i + 1}/{retryCount} 次: {req.error}" + "File Down Ok");
// }
// }
// catch
// {
// Debug.LogError($"Down Failed {i + 1}/{retryCount} 次: {req.error}" + "File Down No");
// }
// yield return new WaitForSeconds(0.5f); // 等半秒再重试
// }
// }
// }
// callback?.Invoke(tex);
// }
// public void Init()
// {
// img_Title = btn_Title.GetComponent<Image>();
// img_Title.sprite = DataManager.Instance.myPlayerInfo.titleSprite;
// //img_Title.sprite= Resources.Load<Sprite>("TitleImg/" + DataManager.Instance.myPlayerInfo.image);
// txt_Name.name = DataManager.Instance.myPlayerInfo.name;
// //读取
// foreach (Sprite item in Resources.LoadAll<Sprite>("TitleImg"))
// {
// var v = Instantiate(obj_TitlePrefab, tra_TitleParent);
// v.GetComponent<Image>().sprite = item;
// v.name = item.name;
// v.GetComponent<Button>().onClick.AddListener(() =>
// {
// tra_TitleParent.gameObject.SetActive(false);
// img_Title.sprite = item;
// WebSocketManager.Instance.SendMessageToUrl(UrlType.defaultType, new { CMD = 11011, avatar_id = v.name });
// });
// }
// btn_CloseBtn.onClick.AddListener(() =>
// {
// UIManager.Instance.CloseUI(UIType.UIUserInfo);
// });
// btn_CloseBtn.onClick.AddListener(() =>
// {
// UIManager.Instance.CloseUI(UIType.UIUserInfo);
// });
// }
}