diff --git a/TheStrongestSnail/Assets/Scripts/LqUiScripts/EditPanel.cs b/TheStrongestSnail/Assets/Scripts/LqUiScripts/EditPanel.cs index bf29870..06033f4 100644 --- a/TheStrongestSnail/Assets/Scripts/LqUiScripts/EditPanel.cs +++ b/TheStrongestSnail/Assets/Scripts/LqUiScripts/EditPanel.cs @@ -5,15 +5,19 @@ using TMPro; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; - +using System.IO; +using System.Threading.Tasks; +using UnityEngine.Networking; public class EditPanel : Base { public UserInfomation14 userInfomation14; public InputField UserInputField; public Text username; public PerSonalCenterPanel perSonalCenterPanel; - + public Avaterdata avaterdata; public Button avatar; + + public Image AvaImage; // Start is called before the first frame update async void Start() { @@ -26,9 +30,10 @@ public class EditPanel : Base string response14 = await web.SendRequest(web.URL + "/snail/user/queryUserInfo", "POST", "{}", head14); Debug.Log("1.4查询玩家信息" + response14); userInfomation14 = JsonConvert.DeserializeObject(response14); - + UserInputField.text = "" + userInfomation14.data.nickName; username.text = "" + userInfomation14.data.nickName; avatar.onClick.AddListener(selectavatar); + AvaImage.sprite = await Base.GlobalObj.GetComponent().LoadImageAsync(userInfomation14.data.headImg); } void selectavatar() @@ -49,33 +54,100 @@ public class EditPanel : Base Debug.Log("Couldn't load texture from " + path); return; } + Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); - // Assign texture to a temporary quad and destroy it after 5 seconds - GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad); - quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f; - quad.transform.forward = Camera.main.transform.forward; - quad.transform.localScale = new Vector3(1f, texture.height / (float)texture.width, 1f); - - Material material = quad.GetComponent().material; - if (!material.shader.isSupported) // happens when Standard shader is not included in the build - material.shader = Shader.Find("Legacy Shaders/Diffuse"); - - material.mainTexture = texture; - - Destroy(quad, 5f); - - // If a procedural texture is not destroyed manually, - // it will only be freed after a scene change - Destroy(texture, 5f); + // 设置 AvaImage 的 sprite + //AvaImage.sprite = sprite; + byte[] fileBytes = File.ReadAllBytes(path); + SetAvater(path); } }); Debug.Log("Permission result: " + permission); } // Update is called once per frame - void Update() + // 设置头像的方法 + public async void SetAvater(string filepath) { - + // 检查文件是否存在 + if (!File.Exists(filepath)) + { + Debug.LogError("文件不存在:" + filepath); + return; + } + + // 读取文件为字节数组 + byte[] fileBytes = File.ReadAllBytes(filepath); + + // 创建 HTTP 请求头部 + Dictionary head13 = new Dictionary + { + { "Authorization", Global.global.serverResponse.data.token } + }; + + Debug.Log("入参-----------------------------1.17"); + + // 构建请求 + UnityWebRequest request = CreateFileUploadRequest(filepath, fileBytes, head13); + + // 发送请求并等待响应 + await SendFileUploadRequest(request); + } + + // 创建文件上传的 UnityWebRequest + private UnityWebRequest CreateFileUploadRequest(string filepath, byte[] fileBytes, Dictionary headers) + { + string url = web.URL + "/snail/file/upload"; // 文件上传的接口 URL + + // 创建 WWWForm 表单 + WWWForm form = new WWWForm(); + + // 获取文件名并添加到表单("file" 为字段名) + string fileName = Path.GetFileName(filepath); + form.AddBinaryData("file", fileBytes, fileName, "image/jpeg"); // 这里 MIME 类型是 jpeg 图片 + + // 创建 UnityWebRequest + UnityWebRequest request = UnityWebRequest.Post(url, form); + + // 添加请求头部(Authorization) + foreach (var header in headers) + { + request.SetRequestHeader(header.Key, header.Value); + } + + return request; + } + + // 发送文件上传请求 + private async Task SendFileUploadRequest(UnityWebRequest request) + { + // 发送请求并等待响应 + var asyncOperation = request.SendWebRequest(); + + while (!asyncOperation.isDone) + { + await Task.Yield(); // 等待请求完成 + } + + // 处理请求结果 + if (request.result == UnityWebRequest.Result.Success) + { + string responseText = request.downloadHandler.text; + Debug.Log("响应: " + responseText); + + // 解析 JSON 响应 + + AvaterRe response = JsonUtility.FromJson(responseText); + //avaterdata.path = response.data; // 处理返回的数据 + Debug.Log(response.code); + Debug.Log(response.data); + AvaImage.sprite= await Base.GlobalObj.GetComponent().LoadImageAsync(response.data); + userInfomation14.data.headImg = response.data; + } + else + { + Debug.LogError("请求失败: " + request.error); + } } public async void SetEditPanel() { @@ -84,7 +156,14 @@ public class EditPanel : Base { "Authorization", Global.global.serverResponse.data.token } }; ChangeDetailbody body= new ChangeDetailbody(); - body.nickName = UserInputField.text; + if(UserInputField.text == null) + { + body.nickName = userInfomation14.data.nickName; + } + else + { + body.nickName = UserInputField.text; + } Debug.Log(UserInputField.text); body.birthday = userInfomation14.data.birthday; body.gender= userInfomation14.data.gender; @@ -107,7 +186,19 @@ public class EditPanel : Base transform.gameObject.SetActive(false); } } -public class ChangeDetailbody//登录和注册用 + + +public class AvaterRe : Response +{ + public string data; +} + +public class Avaterdata +{ + public string path; +} + +public class ChangeDetailbody { public string nickName = ""; public string headImg = ""; diff --git a/TheStrongestSnail/Assets/Scripts/LqUiScripts/PerSonalCenterPanel.cs b/TheStrongestSnail/Assets/Scripts/LqUiScripts/PerSonalCenterPanel.cs index 6e2d1ed..97af2a3 100644 --- a/TheStrongestSnail/Assets/Scripts/LqUiScripts/PerSonalCenterPanel.cs +++ b/TheStrongestSnail/Assets/Scripts/LqUiScripts/PerSonalCenterPanel.cs @@ -17,6 +17,7 @@ public class PerSonalCenterPanel : MonoBehaviour public Text id; public TextMeshProUGUI bindtime; + public Image AvaterImage; //UI逻辑相关组件 public Transform trans; public Button editBtn;//编辑按钮 @@ -150,7 +151,7 @@ public class PerSonalCenterPanel : MonoBehaviour nickName.text = userInfomation14.data.nickName; id.text = "ID:"+ userInfomation14.data.cuteNo; DateTime bindDateTime = DateTime.Parse(userInfomation14.data.bindTime); - + AvaterImage.sprite= await Base.GlobalObj.GetComponent().LoadImageAsync(userInfomation14.data.headImg); // 获取当前时间 DateTime currentDateTime = DateTime.Now; diff --git a/TheStrongestSnail/Assets/Scripts/Scene_main/Scene_main_jiekou.cs b/TheStrongestSnail/Assets/Scripts/Scene_main/Scene_main_jiekou.cs index 6b06a6e..f01075d 100644 --- a/TheStrongestSnail/Assets/Scripts/Scene_main/Scene_main_jiekou.cs +++ b/TheStrongestSnail/Assets/Scripts/Scene_main/Scene_main_jiekou.cs @@ -52,7 +52,7 @@ public class Scene_main_jiekou :Base /*Debug.Log(_realPlayerInfo.data.nickName); Debug.Log(_realPlayerInfo.data.voluteCoin); Debug.Log(_realPlayerInfo.data.beansCoin);*/ - _mainBTN.UpDatePlayer(_realPlayerInfo.data.nickName, _realPlayerInfo.data.voluteCoin.ToString("f1") , _realPlayerInfo.data.beansCoin.ToString("f1")); + _mainBTN.UpDatePlayer(_realPlayerInfo.data.headImg, _realPlayerInfo.data.nickName, _realPlayerInfo.data.voluteCoin.ToString("f1") , _realPlayerInfo.data.beansCoin.ToString("f1")); } async Task PlayerInfoAsync() diff --git a/TheStrongestSnail/Assets/prefabs/HYLPrefabs/PersonalCenterPanel.prefab b/TheStrongestSnail/Assets/prefabs/HYLPrefabs/PersonalCenterPanel.prefab index 4632a2b..3dcc3d9 100644 --- a/TheStrongestSnail/Assets/prefabs/HYLPrefabs/PersonalCenterPanel.prefab +++ b/TheStrongestSnail/Assets/prefabs/HYLPrefabs/PersonalCenterPanel.prefab @@ -2151,6 +2151,7 @@ MonoBehaviour: nickName: {fileID: 6137192365456531787} id: {fileID: 7440098880401058131} bindtime: {fileID: 8608830216116307115} + AvaterImage: {fileID: 8197978441176617509} trans: {fileID: 6661102261700243289} editBtn: {fileID: 3317031098697572181} myorderBtn: {fileID: 2761721410439307699} diff --git a/TheStrongestSnail/Assets/prefabs/ge_ren_zhong_xing/EditPanel.prefab b/TheStrongestSnail/Assets/prefabs/ge_ren_zhong_xing/EditPanel.prefab index f849966..34444f2 100644 --- a/TheStrongestSnail/Assets/prefabs/ge_ren_zhong_xing/EditPanel.prefab +++ b/TheStrongestSnail/Assets/prefabs/ge_ren_zhong_xing/EditPanel.prefab @@ -469,6 +469,7 @@ MonoBehaviour: username: {fileID: 8468291165135306074} perSonalCenterPanel: {fileID: 0} avatar: {fileID: 2339787961763677450} + AvaImage: {fileID: 8996637367871962010} --- !u!1 &6960989442929760420 GameObject: m_ObjectHideFlags: 0