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(); } /// /// 测试基础GET请求(异步方式) /// 展示如何使用WebRequestManager发送GET请求并处理响应 /// private async UniTask TestBasicGetRequest() { Debug.Log("--- 基础GET请求测试开始 ---"); try { // 使用异步方式发送GET请求 var result = await WebRequestManager.Instance.PostRequestAsync( url: Apis.GetGameCodeTest(), signParams: new Dictionary() { { "apipost_id", "395e2022f2f05b" }, } ); Debug.Log($"GET请求成功!"); Debug.Log($"状态码: {result.ResponseCode}"); Debug.Log($"响应内容: {result.ResponseText}"); Debug.Log($"请求URL: {result.Url}"); BasicGetTest basicGetTest = JsonUtility.FromJson(result.ResponseText); Apis.SetCode(basicGetTest.data); Debug.Log($"Code设置成功: {basicGetTest.data}"); } catch (System.Exception ex) { Debug.LogError($"GET请求失败: {ex.Message}"); throw; // 重新抛出异常,让调用者知道失败 } Debug.Log("--- 基础GET请求测试结束 ---"); } /// /// 获取AccessToken(异步方式) /// 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() { { "code", code }, } ); Debug.Log($"AccessToken请求成功!"); Debug.Log($"状态码: {result.ResponseCode}"); Debug.Log($"响应内容: {result.ResponseText}"); Debug.Log($"请求URL: {result.Url}"); // 解析响应并设置token AccessTokenResponse tokenResponse = JsonUtility.FromJson(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}"); } } } /// /// 基础GET请求测试类 /// public class BasicGetTest { public int code; public string data; } /// /// AccessToken响应类 /// public class AccessTokenResponse { public int code; public string data; public string msg; }