WX-Game1/Assets/Scripts/GameLauncher.cs

198 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using DefaultNamespace;
using UnityEngine;
using WXGame.Network;
using Cysharp.Threading.Tasks;
using MotionFramework;
using MotionFramework.Console;
using UnityEngine.SceneManagement;
public class GameLauncher : MonoBehaviour
{
void Awake()
{
#if !UNITY_EDITOR
SimulationOnEditor = false;
#endif
// 初始化控制台
if (Application.isEditor || Debug.isDebugBuild)
DeveloperConsole.Initialize();
// 初始化框架
MotionEngine.Initialize(this, HandleMotionFrameworkLog);
}
void Start()
{
// 创建游戏模块
CreateGameModules();
}
void Update()
{
// 更新框架
MotionEngine.Update();
}
void OnGUI()
{
// 绘制控制台
if (Application.isEditor || Debug.isDebugBuild)
DeveloperConsole.Draw();
}
private async UniTask CreateGameModules()
{
Debug.Log("=== 开始初始化流程 ===");
// 第一步获取code
await TestBasicGetRequest();
// 第二步使用code获取token
await GetAccessToken();
SceneManager.LoadScene("GameMain");
Debug.Log("=== 初始化流程完成 ===");
//用户数据
MotionEngine.CreateModule<UserDataNetworkManager>();
}
/// <summary>
/// 测试基础GET请求异步方式
/// 展示如何使用WebRequestManager发送GET请求并处理响应
/// </summary>
private async UniTask TestBasicGetRequest()
{
Debug.Log("--- 基础GET请求测试开始 ---");
try
{
// 使用异步方式发送GET请求
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetGameCodeTest(),
signParams: new Dictionary<string, string>()
{
{ "apipost_id", "395e2022f2f05b" },
}
);
Debug.Log($"GET请求成功");
Debug.Log($"状态码: {result.ResponseCode}");
Debug.Log($"响应内容: {result.ResponseText}");
Debug.Log($"请求URL: {result.Url}");
BasicGetTest basicGetTest = JsonUtility.FromJson<BasicGetTest>(result.ResponseText);
Apis.SetCode(basicGetTest.data);
Debug.Log($"Code设置成功: {basicGetTest.data}");
}
catch (System.Exception ex)
{
Debug.LogError($"GET请求失败: {ex.Message}");
throw; // 重新抛出异常,让调用者知道失败
}
Debug.Log("--- 基础GET请求测试结束 ---");
}
/// <summary>
/// 获取AccessToken异步方式
/// </summary>
private async UniTask GetAccessToken()
{
Debug.Log("--- 获取AccessToken测试开始 ---");
// 获取code参数
string code = Apis.GetCode();
if (string.IsNullOrEmpty(code))
{
Debug.LogError("Code为空无法获取AccessToken");
throw new System.Exception("Code为空无法获取AccessToken");
}
try
{
// 使用异步方式发送POST请求
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetAccessToken(),
signParams: new Dictionary<string, string>()
{
{ "code", code },
}
);
Debug.Log($"AccessToken请求成功");
Debug.Log($"状态码: {result.ResponseCode}");
Debug.Log($"响应内容: {result.ResponseText}");
Debug.Log($"请求URL: {result.Url}");
// 解析响应并设置token
AccessTokenResponse tokenResponse = JsonUtility.FromJson<AccessTokenResponse>(result.ResponseText);
if (tokenResponse.code == 0 && !string.IsNullOrEmpty(tokenResponse.data))
{
Apis.SetToken(tokenResponse.data);
Debug.Log($"Token设置成功: {tokenResponse.data}");
}
else
{
Debug.LogError($"获取Token失败: {tokenResponse.msg}");
throw new System.Exception($"获取Token失败: {tokenResponse.msg}");
}
}
catch (System.Exception ex)
{
Debug.LogError($"AccessToken请求失败: {ex.Message}");
throw; // 重新抛出异常,让调用者知道失败
}
Debug.Log("--- 获取AccessToken测试结束 ---");
}
private void HandleMotionFrameworkLog(ELogLevel logLevel, string log)
{
if (logLevel == ELogLevel.Log)
{
UnityEngine.Debug.Log(log);
}
else if (logLevel == ELogLevel.Error)
{
UnityEngine.Debug.LogError(log);
}
else if (logLevel == ELogLevel.Warning)
{
UnityEngine.Debug.LogWarning(log);
}
else if (logLevel == ELogLevel.Exception)
{
UnityEngine.Debug.LogError(log);
}
else
{
throw new NotImplementedException($"{logLevel}");
}
}
}
/// <summary>
/// 基础GET请求测试类
/// </summary>
public class BasicGetTest
{
public int code;
public string data;
}
/// <summary>
/// AccessToken响应类
/// </summary>
public class AccessTokenResponse
{
public int code;
public string data;
public string msg;
}