114 lines
3.8 KiB
C#

using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using TEngine;
using static PetResponseScriptableObject;
using System.Threading.Tasks;
using System.Xml.Linq;
using VoxelBusters.CoreLibrary;
namespace GameLogic
{
[Window(UILayer.UI)]
class UIARGameWindow : UIWindow
{
private string curgamename;
private ARGameScriptableObject aRGame; // 游戏数据本地配置
private const string GAME_DATA_PATH = "ARGameData"; // 游戏数据本地配置路径
#region
private GameObject m_goParentGroup;
private Button m_btnSure;
protected override void ScriptGenerator()
{
m_goParentGroup = FindChild("Group/ScrollView/Viewport/Content/m_goParentGroup").gameObject;
m_btnSure = FindChildComponent<Button>("Mask/m_btnSure");
m_btnSure.onClick.AddListener(UniTask.UnityAction(OnClickSureBtn));
}
#endregion
#region
private async UniTaskVoid OnClickSureBtn()
{
await UniTask.Yield();
Debug.LogError(curgamename);
}
#endregion
protected override void RegisterEvent()
{
base.RegisterEvent();
GameEvent.AddEventListener<string>(
"UIARGameInterface",
(gamename) =>
{
this.curgamename = gamename;
});
}
protected override void OnCreate()
{
base.OnCreate();
LoadARGameData();
RegisterEvent();
}
private async void LoadARGameData()
{
aRGame = GameModule.Resource.LoadAsset<ARGameScriptableObject>(GAME_DATA_PATH);
if (aRGame == null)
{
Debug.LogError("加载游戏ARGameScriptableObject配置数据路径错误" + GAME_DATA_PATH);
}
else
{
Debug.Log("游戏数据ARGameScriptableObject加载完成.");
if (aRGame.responseData.data != null && aRGame.responseData.data.Count > 0)
{
int widgetId = 0;
foreach (var tmp in aRGame.responseData.data)
{
Texture2D texture = await LoadTexture(tmp.GameImgURL);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
var name = ImageUtility.GetImageNameFromUrl(tmp.GameName);
if (sprite != null && name != null)
{
CreateGameWidget(sprite, name, tmp.Lock,widgetId++);
}
else
{
Debug.LogError($"宠物图片加载失败: {tmp.GameImgURL}或者名字为空");
}
}
}
else
{
Debug.Log("No Game data found in the response.");
}
}
}
// 加载纹理
private async Task<Texture2D> LoadTexture(string url)
{
var token = this.gameObject.GetCancellationTokenOnDestroy();
var trackImg = await ImageUtility.UnityWebDownloadTexture2D(url, token);
return trackImg;
}
// 创建宠物信息,设置widget信息
private void CreateGameWidget(Sprite sp, string petName,bool islock, int widgetId)
{
CreateWidgetByPath<UIARGameWidget>(m_goParentGroup.transform, "UIARGameWidget").InitGameFunc(widgetId, sp, petName, islock);
}
}
}