头像更新,完善用户信息
This commit is contained in:
parent
6e7020034b
commit
e6b74ae6e3
@ -5,15 +5,19 @@ using TMPro;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine.Networking;
|
||||||
public class EditPanel : Base
|
public class EditPanel : Base
|
||||||
{
|
{
|
||||||
public UserInfomation14 userInfomation14;
|
public UserInfomation14 userInfomation14;
|
||||||
public InputField UserInputField;
|
public InputField UserInputField;
|
||||||
public Text username;
|
public Text username;
|
||||||
public PerSonalCenterPanel perSonalCenterPanel;
|
public PerSonalCenterPanel perSonalCenterPanel;
|
||||||
|
public Avaterdata avaterdata;
|
||||||
public Button avatar;
|
public Button avatar;
|
||||||
|
|
||||||
|
public Image AvaImage;
|
||||||
// Start is called before the first frame update
|
// Start is called before the first frame update
|
||||||
async void Start()
|
async void Start()
|
||||||
{
|
{
|
||||||
@ -26,9 +30,10 @@ public class EditPanel : Base
|
|||||||
string response14 = await web.SendRequest(web.URL + "/snail/user/queryUserInfo", "POST", "{}", head14);
|
string response14 = await web.SendRequest(web.URL + "/snail/user/queryUserInfo", "POST", "{}", head14);
|
||||||
Debug.Log("1.4²éѯÍæ¼ÒÐÅÏ¢" + response14);
|
Debug.Log("1.4²éѯÍæ¼ÒÐÅÏ¢" + response14);
|
||||||
userInfomation14 = JsonConvert.DeserializeObject<UserInfomation14>(response14);
|
userInfomation14 = JsonConvert.DeserializeObject<UserInfomation14>(response14);
|
||||||
|
UserInputField.text = "" + userInfomation14.data.nickName;
|
||||||
username.text = "" + userInfomation14.data.nickName;
|
username.text = "" + userInfomation14.data.nickName;
|
||||||
avatar.onClick.AddListener(selectavatar);
|
avatar.onClick.AddListener(selectavatar);
|
||||||
|
AvaImage.sprite = await Base.GlobalObj.GetComponent<ImageLoader>().LoadImageAsync(userInfomation14.data.headImg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void selectavatar()
|
void selectavatar()
|
||||||
@ -49,33 +54,100 @@ public class EditPanel : Base
|
|||||||
Debug.Log("Couldn't load texture from " + path);
|
Debug.Log("Couldn't load texture from " + path);
|
||||||
return;
|
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
|
// 设置 AvaImage 的 sprite
|
||||||
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
|
//AvaImage.sprite = sprite;
|
||||||
quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
|
byte[] fileBytes = File.ReadAllBytes(path);
|
||||||
quad.transform.forward = Camera.main.transform.forward;
|
SetAvater(path);
|
||||||
quad.transform.localScale = new Vector3(1f, texture.height / (float)texture.width, 1f);
|
|
||||||
|
|
||||||
Material material = quad.GetComponent<Renderer>().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);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Debug.Log("Permission result: " + permission);
|
Debug.Log("Permission result: " + permission);
|
||||||
}
|
}
|
||||||
// Update is called once per frame
|
// 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<string, string> head13 = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "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<string, string> 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<AvaterRe>(responseText);
|
||||||
|
//avaterdata.path = response.data; // 处理返回的数据
|
||||||
|
Debug.Log(response.code);
|
||||||
|
Debug.Log(response.data);
|
||||||
|
AvaImage.sprite= await Base.GlobalObj.GetComponent<ImageLoader>().LoadImageAsync(response.data);
|
||||||
|
userInfomation14.data.headImg = response.data;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("请求失败: " + request.error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public async void SetEditPanel()
|
public async void SetEditPanel()
|
||||||
{
|
{
|
||||||
@ -84,7 +156,14 @@ public class EditPanel : Base
|
|||||||
{ "Authorization", Global.global.serverResponse.data.token }
|
{ "Authorization", Global.global.serverResponse.data.token }
|
||||||
};
|
};
|
||||||
ChangeDetailbody body= new ChangeDetailbody();
|
ChangeDetailbody body= new ChangeDetailbody();
|
||||||
|
if(UserInputField.text == null)
|
||||||
|
{
|
||||||
|
body.nickName = userInfomation14.data.nickName;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
body.nickName = UserInputField.text;
|
body.nickName = UserInputField.text;
|
||||||
|
}
|
||||||
Debug.Log(UserInputField.text);
|
Debug.Log(UserInputField.text);
|
||||||
body.birthday = userInfomation14.data.birthday;
|
body.birthday = userInfomation14.data.birthday;
|
||||||
body.gender= userInfomation14.data.gender;
|
body.gender= userInfomation14.data.gender;
|
||||||
@ -107,7 +186,19 @@ public class EditPanel : Base
|
|||||||
transform.gameObject.SetActive(false);
|
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 nickName = "";
|
||||||
public string headImg = "";
|
public string headImg = "";
|
||||||
|
@ -17,6 +17,7 @@ public class PerSonalCenterPanel : MonoBehaviour
|
|||||||
public Text id;
|
public Text id;
|
||||||
public TextMeshProUGUI bindtime;
|
public TextMeshProUGUI bindtime;
|
||||||
|
|
||||||
|
public Image AvaterImage;
|
||||||
//UI逻辑相关组件
|
//UI逻辑相关组件
|
||||||
public Transform trans;
|
public Transform trans;
|
||||||
public Button editBtn;//编辑按钮
|
public Button editBtn;//编辑按钮
|
||||||
@ -150,7 +151,7 @@ public class PerSonalCenterPanel : MonoBehaviour
|
|||||||
nickName.text = userInfomation14.data.nickName;
|
nickName.text = userInfomation14.data.nickName;
|
||||||
id.text = "ID:"+ userInfomation14.data.cuteNo;
|
id.text = "ID:"+ userInfomation14.data.cuteNo;
|
||||||
DateTime bindDateTime = DateTime.Parse(userInfomation14.data.bindTime);
|
DateTime bindDateTime = DateTime.Parse(userInfomation14.data.bindTime);
|
||||||
|
AvaterImage.sprite= await Base.GlobalObj.GetComponent<ImageLoader>().LoadImageAsync(userInfomation14.data.headImg);
|
||||||
// 获取当前时间
|
// 获取当前时间
|
||||||
DateTime currentDateTime = DateTime.Now;
|
DateTime currentDateTime = DateTime.Now;
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ public class Scene_main_jiekou :Base
|
|||||||
/*Debug.Log(_realPlayerInfo.data.nickName);
|
/*Debug.Log(_realPlayerInfo.data.nickName);
|
||||||
Debug.Log(_realPlayerInfo.data.voluteCoin);
|
Debug.Log(_realPlayerInfo.data.voluteCoin);
|
||||||
Debug.Log(_realPlayerInfo.data.beansCoin);*/
|
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()
|
async Task PlayerInfoAsync()
|
||||||
|
@ -2151,6 +2151,7 @@ MonoBehaviour:
|
|||||||
nickName: {fileID: 6137192365456531787}
|
nickName: {fileID: 6137192365456531787}
|
||||||
id: {fileID: 7440098880401058131}
|
id: {fileID: 7440098880401058131}
|
||||||
bindtime: {fileID: 8608830216116307115}
|
bindtime: {fileID: 8608830216116307115}
|
||||||
|
AvaterImage: {fileID: 8197978441176617509}
|
||||||
trans: {fileID: 6661102261700243289}
|
trans: {fileID: 6661102261700243289}
|
||||||
editBtn: {fileID: 3317031098697572181}
|
editBtn: {fileID: 3317031098697572181}
|
||||||
myorderBtn: {fileID: 2761721410439307699}
|
myorderBtn: {fileID: 2761721410439307699}
|
||||||
|
@ -469,6 +469,7 @@ MonoBehaviour:
|
|||||||
username: {fileID: 8468291165135306074}
|
username: {fileID: 8468291165135306074}
|
||||||
perSonalCenterPanel: {fileID: 0}
|
perSonalCenterPanel: {fileID: 0}
|
||||||
avatar: {fileID: 2339787961763677450}
|
avatar: {fileID: 2339787961763677450}
|
||||||
|
AvaImage: {fileID: 8996637367871962010}
|
||||||
--- !u!1 &6960989442929760420
|
--- !u!1 &6960989442929760420
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
Loading…
Reference in New Issue
Block a user