-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameStart.cs
187 lines (149 loc) · 5.25 KB
/
GameStart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System;
using System.Text;
public class GameStart : MonoBehaviour
{
public InputField playerNameInputField;
public Text dialogText;
public GameObject currentDialogPanel;
public GameObject nextDialogPanel;
//控制游戏开始的一个单例
public GameObject startmgr;
private const string serverURL = "http://cc.savethebaby.cn/doctor/api/player/saveOrUpdate";
private const string playerNameKey = "PlayerName";
private const string playerIpKey = "PlayerIp";
private bool dialogShown = false;
private void Start()
{
//单例关闭
startmgr.SetActive(false);
}
public void StartGame()
{
ShowInputDialog("您的名字是?");
}
public void SetPlayerName()
{
//单例开启
startmgr.SetActive(true);
string playerName = playerNameInputField.text;
if (string.IsNullOrEmpty(playerName))
{
Debug.LogError("玩家名字不能为空!");
return;
}
string playerIp = Search_Ip.ins.player_ip;
//查询本地的公网ip
Debug.Log("公网ip:" + playerIp);
//存储玩家数据
PlayerPrefs.SetString(playerNameKey, playerName);
PlayerPrefs.SetString(playerIpKey, playerIp);
UploadPlayerToServer(playerName, playerIp);
playerNameInputField.gameObject.SetActive(false);
ShowNextDialog($"您好,{playerName}。");
}
void ShowInputDialog(string message)
{
playerNameInputField.gameObject.SetActive(true);
dialogText.text = message;
dialogShown = false;
}
void ShowNextDialog(string message)
{
if (dialogShown)
{
nextDialogPanel.SetActive(true);
currentDialogPanel.SetActive(false);
dialogShown = false;
}
else
{
dialogText.text = message;
ShowThisDialog();
}
}
void ShowThisDialog()
{
string playerName = PlayerPrefs.GetString("PlayerName", "默认名字");
dialogText.text = $"{playerName},你好!";
dialogShown = true;
}
void UploadPlayerToServer(string playerName, string playerIp)
{
StartCoroutine(UploadPlayerData(playerName, playerIp));
}
IEnumerator UploadPlayerData(string playerName, string playerIp)
{
Debug.Log($"JSON 数据: playerName={playerName}, playerIp={playerIp}");
Dictionary<string, string> data = new Dictionary<string, string>
{
{ "playerName", playerName },
{ "playerIp", playerIp }
};
string jsonData = DictToJson(data);
Debug.Log($"Sending JSON data to server: {jsonData}");
byte[] postData = Encoding.UTF8.GetBytes(jsonData);
foreach (var entry in data)
{
Debug.Log($"字典内容: Key = {entry.Key}, Value = {entry.Value}");
}
Debug.Log($"上传前的 JSON 参数: {JsonUtility.ToJson(data)}");
using (UnityWebRequest www = new UnityWebRequest(serverURL, UnityWebRequest.kHttpVerbPOST))
{
www.uploadHandler = new UploadHandlerRaw(postData);
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.LogError($"上传玩家数据失败。错误: {www.error}");
}
else
{
Debug.Log("成功上传玩家数据!");
Debug.Log($"服务器响应: {www.downloadHandler.text}");
HandleServerResponse(www.downloadHandler.text);
GameStartupManager.Instance.StartGame();
}
}
}
string DictToJson(Dictionary<string, string> dict)
{
StringBuilder jsonString = new StringBuilder("{");
foreach (var pair in dict)
{
jsonString.AppendFormat("\"{0}\":\"{1}\",", pair.Key, pair.Value);
}
jsonString.Length--;
jsonString.Append("}");
return jsonString.ToString();
}
void HandleServerResponse(string response)
{
if (!string.IsNullOrEmpty(response))
{
try
{
SucResponse sucResponse = JsonUtility.FromJson<SucResponse>(response);
Debug.Log($"解析服务端数据: {"msg: "+sucResponse.msg+" "+"code: "+sucResponse.code+" "+"data: "+sucResponse.data}");
}
catch (Exception ex)
{
Debug.LogError($"解析 JSON 错误: {ex.Message}");
Debug.LogError($"来自服务器的原始 JSON: {response}");
}
}
}
//注册登录接口,服务器返回信息
[Serializable]
public class SucResponse
{
public string msg;
public string code;
public string data;
}
}