129 lines
4.8 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 Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using TEngine;
using System.Threading.Tasks;
using UnityEngine.Networking;
using Sirenix.Utilities.Editor.Expressions;
namespace GameLogic
{
[Window(UILayer.UI)]
class UIPetInfoWindow : UIWindow
{
public PetResponseScriptableObject petData; // 宠物数据本地配置
private const string PET_DATA_PATH = "PetResponseData"; // 宠物数据本地配置路径
#region
private Button m_btnAddtionPet;
private GameObject m_goParent;
private GameObject m_goTips;
private Button m_btnCancle;
private Button m_btnSure;
protected override void ScriptGenerator()
{
m_btnAddtionPet = FindChildComponent<Button>("ScrollView/Viewport/Content/AddtionPetGroup/AddtionPetBackImage/AddPetBottom/m_btnAddtionPet");
m_goParent = FindChild("ScrollView/Viewport/Content/m_goParent").gameObject;
m_goTips = FindChild("m_goTips").gameObject;
m_btnCancle = FindChildComponent<Button>("m_goTips/MaskLayout/Parent/Bottom/m_btnCancle");
m_btnSure = FindChildComponent<Button>("m_goTips/MaskLayout/Parent/Bottom/m_btnSure");
m_btnAddtionPet.onClick.AddListener(UniTask.UnityAction(OnClickAddtionPetBtn));
m_btnCancle.onClick.AddListener(UniTask.UnityAction(OnClickCancleBtn));
m_btnSure.onClick.AddListener(UniTask.UnityAction(OnClickSureBtn));
}
#endregion
#region
private async UniTaskVoid OnClickAddtionPetBtn() // 新增宠物
{
await UniTask.Yield();
}
private async UniTaskVoid OnClickCancleBtn() // 取消删除
{
await UniTask.Yield();
ShowTipPanel();
}
private async UniTaskVoid OnClickSureBtn() // 确认删除
{
await UniTask.Yield();
}
#endregion
protected override void OnCreate()
{
base.OnCreate();
LoadPetData();
}
// 加载数据获取图片和图片名创建widget
private async void LoadPetData()
{
petData = GameModule.Resource.LoadAsset<PetResponseScriptableObject>(PET_DATA_PATH);
if (petData == null)
{
Debug.LogError("加载宠物ScriptableObject配置数据路径错误" + PET_DATA_PATH);
}
else
{
Debug.Log("宠物数据PetResponseScriptableObject加载完成.");
if (petData.responseData.data != null && petData.responseData.data.Count > 0)
{
foreach (var tmpDta in petData.responseData.data)
{
//Sprite sprite = await LoadSprite(tmpDta.pic);
Texture2D texture = await LoadTexture(tmpDta.pic);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
var Name = ImageUtility.GetImageNameFromUrl(tmpDta.pic);
if (sprite != null)
{
CreatePetInfoPrefab(sprite, Name);
}
else
{
Debug.LogError($"宠物图片加载失败: {tmpDta.name}");
}
}
}
else
{
Debug.Log("No pet 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 CreatePetInfoPrefab(Sprite sp, string petName)
{
CreateWidgetByPath<UIPetinfoWidget>(m_goParent.transform, "UIPetinfoWidget").SetPetBanData(sp, petName);
}
// 显示tip面板
private void ShowTipPanel()
{
if (m_goTips != null)
{
if(m_goTips.activeSelf == true)
{
m_goTips.SetActive(false);
}
else
{
m_goTips.SetActive(true);
}
}
}
}
}