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

295 lines
10 KiB
C#

using Assets.Framework.Tool;
using Assets.MyGame.Scripts.Net;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using UnityEngine.UI;
public class UIUserInfo : SubUIBase
{
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 ShowChoiceDialog()
{
}
public Button btnA, btnB;
public Text textTitleLabel;
// public 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>().text = btnTXTA;
// btnB.GetComponentInChildren<Text>().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;
// }
// public void AddImage()
// {
// // 弹出一个选择框,用户可以选择拍照或者从相册选择
// // 这里用简单的按钮逻辑,你可以替换成自己的 UI
// ShowChoiceDialog("选择图片来源", "拍照", "相册",
// () =>
// {
// // 拍照
// NativeCamera.TakePicture((path) =>
// {
// if (!string.IsNullOrEmpty(path))
// {
// StartCoroutine(LoadAndCropImage(path));
// }
// else
// {
// Debug.LogError("未拍摄图片");
// }
// });
// },
// () =>
// {
// // 相册
// NativeGallery.GetImageFromGallery((path) =>
// {
// if (!string.IsNullOrEmpty(path))
// {
// StartCoroutine(LoadAndCropImage(path));
// }
// else
// {
// Debug.LogError("未选择图片");
// }
// });
// }
// );
// }
// 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 = MakeReadable(croppedImage);
// // 显示裁剪后的图片
// 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 Texture2D MakeReadable(Texture2D src)
// //{
// // RenderTexture rt = RenderTexture.GetTemporary(src.width, src.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
// // 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;
// //}
// 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;
// }
// 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>();
// if (GameManager.Instance.user.BaseInfo.avatarSprite==null)
// {
// WebTool.DownImage(APIManager.baseURL + GameManager.Instance.user.BaseInfo.avatarUrl, (texture) =>
// {
// GameManager.Instance.user.BaseInfo.avatarSprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
// img_Title.sprite = GameManager.Instance.user.BaseInfo.avatarSprite;
// });
// }
// else
// {
// img_Title.sprite = GameManager.Instance.user.BaseInfo.avatarSprite;
// }
// //img_Title.sprite= Resources.Load<Sprite>("TitleImg/" + DataManager.Instance.myPlayerInfo.image);
// txt_Name.name = GameManager.Instance.user.BaseInfo.userName;
// //读取
// 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);
// });
// }
}