This commit is contained in:
HuangZiBo 2024-11-21 13:55:05 +08:00
parent b1edcafa6c
commit dc6bcb85bf
11 changed files with 278 additions and 0 deletions

View File

@ -741,6 +741,7 @@ GameObject:
- component: {fileID: 252707971}
- component: {fileID: 252707973}
- component: {fileID: 252707972}
- component: {fileID: 252707974}
m_Layer: 5
m_Name: LoginPanel
m_TagString: Untagged
@ -808,6 +809,18 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 252707970}
m_CullTransparentMesh: 1
--- !u!114 &252707974
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 252707970}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f381a54ea0a90e3448e8ae6e3cf61375, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &287371580
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fcb636bea24d970419745267fa282f54
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Global : Singleton<Global>
{
public static Global global;
}
public class server : Response
{
public Data data;
//public string access_token;
}
public class Response
{
public int code;
public string msg;
}
public class Data
{
public string access_token;
public string companyId;
public string companyName;
public string client_id;
public int expire_in;//tolingÊ£Óàʱ¼ä
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 015522dae649528488644d01a5248b5e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
public class Singleton<T> where T : new()
{
private static T ms_instance;
public static T Instance
{
get
{
if (ms_instance == null)
{
ms_instance = new T();
}
return ms_instance;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2f0680be77c89a6408f0f224e3f199c9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,41 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
public class auth_login
{
public string clientId = "e5cd7e4891bf95d1d19206ce24a7b32e";
public string grantType = "password";
public string userType = "sys_user";
public string username = "adminyyl";
public string password = "admin123";
}
public class login : MonoBehaviour
{
public string token;//登录返回的token
public void Start()
{
loging();
//createTemplate();
}
public async void loging()
{
auth_login auth_Login = new auth_login();
string response = await web.SendRequest(web.URL + "/auth/login", "POST", JsonUtility.ToJson(auth_Login));
Debug.Log(response);
// 解析服务器返回的数据
server serverData = JsonConvert.DeserializeObject<server>(response);
//token = serverData.token;
//Debug.Log(setverData.msg);
}
public async void createTemplate()
{
string response = await web.SendRequest(web.URL + "/game/gameTemplate/list", "GET");
Debug.Log(response);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f381a54ea0a90e3448e8ae6e3cf61375
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d11e46e12fbe0404d9cbe4df1759b23a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

108
xiaofang/Assets/comm/web.cs Normal file
View File

@ -0,0 +1,108 @@
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Unity.VisualScripting.Antlr3.Runtime;
using UnityEngine;
using UnityEngine.Networking;
public class web : MonoBehaviour
{
public const string URL = "http://192.168.3.103:8089"; // 被注释掉的服务器地址(常量)
// 存储服务器返回的Cookie
private static Dictionary<string, string> cookieJar = new Dictionary<string, string>();
// 发送HTTP请求的方法支持GET和POST等不同的请求方式
public static async Task<string> SendRequest(string url, string method = "GET", string jsonData = "{}", Dictionary<string, string> head = null)
{
Debug.Log(url + "启动web"); // 调试输出,启动网络请求
// 使用UnityWebRequest类进行HTTP请求支持自定义请求方法
using (UnityWebRequest request = new UnityWebRequest(url, method))
{
// 设置下载处理器,用于处理服务器响应的数据
request.downloadHandler = new DownloadHandlerBuffer();
// 如果是POST请求设置请求体
if (method == "POST" && jsonData != null)
{
// 将JSON字符串转换为字节数组
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
// 设置上传处理器,用于发送请求体数据
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
// 设置请求头指定请求体内容类型为JSON
request.SetRequestHeader("Content-Type", "application/json");
}
// 如果请求头部存在,添加请求头部信息
if (head != null)
foreach (string key in head.Keys)
{
// 遍历传入的头部字典,逐项设置请求头部
request.SetRequestHeader(key, head[key]);
}
// 设置已存储的 Cookie
if (cookieJar.Count > 0)
{
// 用于存储多个cookie值的字符串
string cookieHeader = "";
foreach (var cookie in cookieJar)
{
// 格式化每个cookie为“键=值”的形式,并附加到字符串中,以分号分隔
cookieHeader += $"{cookie.Key}={cookie.Value}; ";
}
// 设置请求头中的Cookie字段
request.SetRequestHeader("Cookie", cookieHeader);
}
// 发送请求并等待响应
var operation = request.SendWebRequest();
while (!operation.isDone)
await Task.Yield(); // 等待请求完成使用await以非阻塞的方式处理
// 检查是否存在网络错误或协议错误
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
// 如果有错误输出错误信息并返回null
Debug.LogError($"Error: {request.error}" + url);
// 可以自定义弹窗提示用户错误信息,代码被注释掉了
// Promptmgr.Instance.PromptBubble(request.error, Color.black,Color.red);
return null;
}
else
{
// 处理返回的Cookie
string setCookieHeader;
string str = request.downloadHandler.text; // 获取服务器返回的文本内容
//Debug.Log(str); // 输出返回内容到控制台
// 尝试获取响应头中的“Set-Cookie”字段
if (request.GetResponseHeaders().TryGetValue("Set-Cookie", out setCookieHeader))
{
// 将“Set-Cookie”字段的内容按分号分隔为多个部分
string[] cookies = setCookieHeader.Split(';');
foreach (var cookie in cookies)
{
// 将每个cookie分为“键=值”的形式
var cookieParts = cookie.Split('=');
if (cookieParts.Length == 2)
{
string key = cookieParts[0].Trim(); // 去掉键的空格
string value = cookieParts[1].Trim(); // 去掉值的空格
// 存储到cookieJar中以备后续请求使用
cookieJar[key] = value;
}
}
}
// 返回响应内容(文本形式)
return request.downloadHandler.text;
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2c4b51a8b53ef05469c7aa617c131ad1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: