using Newtonsoft.Json; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using System.Threading.Tasks; public class PlayerInfo : MonoBehaviour { public static PlayerInfo instance; public static UserInfomation14 userInfomation14; public float Money = 1000; public float AllBetCoins;//总共下注蛋 public TextMeshProUGUI SelfWoniuText;//蜗牛下注显示文本 public bool HaveBet;//是否已经下注过 public Transform BetText; // Start is called before the first frame update void Start() { instance = this; EggNum.instance.eggNumText.text=Money.ToString(); QueryPlayerInfo(); } public bool SetMoney(float add) { if (Money+add>=0) { float start = Money; Money += add; EggNum.instance.SetEggNumText(start); return true; } Debug.Log("钱不够"); return false; } // Update is called once per frame void Update() { if (userInfomation14 != null) { Money = userInfomation14.data.beansCoin; EggNum.instance.eggNumText.text = Money.ToString(); } BetText.position = new Vector2(transform.position.x, transform.position.y + 100); } // 查询玩家信息 public async Task QueryPlayerInfo() // 5.1.1 { // 准备请求的头部信息,包含授权令牌 Dictionary head14 = new Dictionary { { "Authorization", Global.global.serverResponse.data.token } }; // 异步查询玩家信息 string response14 = await web.SendRequest(web.URL + "/snail/user/queryUserInfo", "POST", "{}", head14); Debug.Log("1.4查询玩家信息" + response14); userInfomation14 = JsonConvert.DeserializeObject(response14); Debug.Log("玩家剩余蜗蛋:" + userInfomation14.data.beansCoin); //Money = userInfomation14.beansCoin; // 解析服务器返回的数据 Global.global.response = JsonConvert.DeserializeObject(response14); // 不需要返回值,只需表示异步操作完成 await Task.CompletedTask; // 或者直接返回,不使用 await } }