您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
65 行
2.1 KiB
65 行
2.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using Unity.UIWidgets.engine;
|
|
using Unity.UIWidgets.foundation;
|
|
using Unity.UIWidgets.material;
|
|
using Unity.UIWidgets.rendering;
|
|
using Unity.UIWidgets.widgets;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
public class HttpRequestSample : UIWidgetsPanel
|
|
{
|
|
protected override Widget createWidget() {
|
|
return new MaterialApp(
|
|
title: "Http Request Sample",
|
|
home: new Scaffold(
|
|
body:new AsyncRequestWidget(this.gameObject)
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
public class AsyncRequestWidget : StatefulWidget {
|
|
|
|
public readonly GameObject gameObjOfUIWidgetsPanel;
|
|
|
|
public AsyncRequestWidget(GameObject gameObjOfUiWidgetsPanel, Key key = null) : base(key) {
|
|
this.gameObjOfUIWidgetsPanel = gameObjOfUiWidgetsPanel;
|
|
}
|
|
|
|
public override State createState() {
|
|
return new _AsyncRequestWidgetState();
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class TimeData {
|
|
public long currentFileTime;
|
|
}
|
|
|
|
class _AsyncRequestWidgetState : State<AsyncRequestWidget> {
|
|
|
|
long _fileTime;
|
|
|
|
public override Widget build(BuildContext context) {
|
|
|
|
return new Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: new List<Widget>() {
|
|
new FlatButton(child: new Text("Click To Get Time"), onPressed: () => {
|
|
UnityWebRequest www = UnityWebRequest.Get("http://worldclockapi.com/api/json/est/now");
|
|
var asyncOperation = www.SendWebRequest();
|
|
asyncOperation.completed += operation => {
|
|
var timeData = JsonUtility.FromJson<TimeData>(www.downloadHandler.text);
|
|
using(WindowProvider.of(this.widget.gameObjOfUIWidgetsPanel).getScope())
|
|
{
|
|
this.setState(() => { this._fileTime = timeData.currentFileTime; });
|
|
}
|
|
|
|
};
|
|
}),
|
|
new Text($"current file time: {this._fileTime}")
|
|
});
|
|
}
|
|
}
|