一个简单的Get请求
新建一个场景(Demo7_Launch)和一个初始流程(Demo7_ProcedureLaunch),流程代码如下:
using GameFramework;
using GameFramework.Event;
using GameFramework.Procedure;
using UnityEngine;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
public class Demo7_ProcedureLaunch : ProcedureBase {
protected override void OnEnter (ProcedureOwner procedureOwner) {
base.OnEnter (procedureOwner);
// 获取框架事件组件
EventComponent Event
= UnityGameFramework.Runtime.GameEntry.GetComponent<EventComponent> ();
Event.Subscribe (WebRequestSuccessEventArgs.EventId, OnWebRequestSuccess);
Event.Subscribe (WebRequestFailureEventArgs.EventId, OnWebRequestFailure);
// 获取框架网络组件
WebRequestComponent WebRequest
= UnityGameFramework.Runtime.GameEntry.GetComponent<WebRequestComponent> ();
string url = "http://gameframework.cn/starforce/version.txt";
WebRequest.AddWebRequest (url, this);
}
private void OnWebRequestSuccess (object sender, GameEventArgs e) {
WebRequestSuccessEventArgs ne = (WebRequestSuccessEventArgs) e;
// 获取回应的数据
string responseJson = Utility.Converter.GetString (ne.GetWebResponseBytes ());
Log.Debug("responseJson:" + responseJson);
}
private void OnWebRequestFailure (object sender, GameEventArgs e) {
Log.Warning ("请求失败");
}
}
使用Event组件订阅请求成功和失败事件,使用WebRequest组件的AddWebRequest发起请求,请求的地址是官方博客的一个文本文件。
设置好初始流程后,运行游戏,如果你能看到以下日志输出,那就代表你成功了: