177 lines
6.8 KiB
C#
Raw Normal View History

2024-12-14 18:27:59 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using TMPro;
using System.Linq;
2024-12-16 15:58:37 +08:00
using DG.Tweening;
2024-12-30 19:15:34 +08:00
using static OperationController;
2024-12-14 18:27:59 +08:00
namespace QFramework.Example
{
public class UIToolsData : UIPanelData
{
public List<string> devices;
public string answer;
public bool SetActive = true;
public string rightLable;
public string wrongLabel;
public string rightEvent;
public string wrongEvent;
public float rightScore;
public float wrongScore;
2025-01-19 11:20:01 +08:00
public float totalScore;
2024-12-14 18:27:59 +08:00
public string scoreStepName;
2025-01-14 18:16:35 +08:00
public float autoHideResult = -1;
2024-12-14 18:27:59 +08:00
}
public partial class UITools : UIPanel
{
ResLoader mResLoader;
public List<string> answers;
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UIToolsData ?? new UIToolsData();
// please add init code here
2025-01-09 09:43:15 +08:00
TypeEventSystem.Global.Register<OnModuleQuit>(OnModuleQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
2024-12-30 19:15:34 +08:00
}
private void OnStepChanged(StepStatusOnChange change)
{
Hide();
2024-12-14 18:27:59 +08:00
}
protected override void OnOpen(IUIData uiData = null)
{
2025-01-09 13:35:04 +08:00
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged).UnRegisterWhenDisabled(gameObject);
2025-01-19 11:20:01 +08:00
if (mData.totalScore > 0)
{
ScoreController.Instance.Add(mData.scoreStepName, mData.totalScore);
}
2024-12-14 18:27:59 +08:00
mResLoader = ResLoader.Allocate();
mData = uiData as UIToolsData ?? new UIToolsData();
if (string.IsNullOrEmpty(mData.answer) == false)
{
answers = mData.answer.Split(',')?.ToList();
}
Content.RemoveAllChildren();
foreach (var device in mData.devices)
{
var item = DeviceController.Instance.GetDevice(device);
2025-01-14 18:16:35 +08:00
if (item == null)
2024-12-30 15:50:17 +08:00
{
Debug.LogError(device + ":û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6>Device<63><65><EFBFBD><EFBFBD>");
return;
}
2024-12-14 18:27:59 +08:00
GameObject obj = GameObject.Instantiate(ItemPrefab.gameObject, Content);
obj.name = item.Name;
obj.transform.Find("Name").GetComponent<TextMeshProUGUI>().text = item.Name;
2025-01-10 15:49:30 +08:00
Image icon = obj.transform.Find("IconBg/Icon").GetComponent<Image>();
2024-12-14 18:27:59 +08:00
var localImageUrl = Global.deviceIconsPath + item.Icon;
GameObject right = icon.transform.Find("Right").gameObject;
GameObject wrong = icon.transform.Find("Wrong").gameObject;
GameObject Selected = icon.transform.Find("Selected").gameObject;
mResLoader.Add2Load(localImageUrl.ToNetImageResName(),
2024-12-17 16:40:11 +08:00
(bool success, IRes res) =>
2024-12-14 18:27:59 +08:00
{
2024-12-17 16:40:11 +08:00
if (success)
2024-12-14 18:27:59 +08:00
{
2024-12-18 09:23:35 +08:00
icon.sprite = Utility.GetSprite(res.Asset as Texture2D);
2024-12-14 18:27:59 +08:00
}
});
Button btn = obj.GetComponent<Button>();
btn.onClick.AddListener(() =>
{
if (answers != null)
{
if (answers.Contains(item.Name))
{
SetSelected(obj, true);
if (mData.SetActive)
{
DeviceController.Instance.GetDeviceObj(device).SetActive(true);
}
ScoreController.Instance.Add(mData.scoreStepName, mData.rightScore);
answers.Remove(item.Name);
if (answers.Count <= 0)
{
var data = new UIResultTipData();
data.label = mData.rightLable;
data.isRight = true;
data.callback = () =>
{
2025-02-07 17:06:19 +08:00
if (string.IsNullOrEmpty(mData.rightEvent) == false)
{
StringEventSystem.Global.Send(mData.rightEvent);
}
2024-12-14 18:27:59 +08:00
};
2025-01-14 18:48:46 +08:00
data.autoHideTime = mData.autoHideResult;
2024-12-14 18:27:59 +08:00
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().StartGlobal();
}
}
else
{
ScoreController.Instance.Add(mData.scoreStepName, mData.wrongScore);
var data = new UIResultTipData();
data.label = mData.wrongLabel;
data.isRight = false;
data.callback = () =>
{
2025-02-07 17:06:19 +08:00
if (string.IsNullOrEmpty(mData.wrongEvent) == false)
{
StringEventSystem.Global.Send(mData.wrongEvent);
}
2024-12-14 18:27:59 +08:00
SetSelected(obj, false);
};
2025-01-14 18:48:46 +08:00
data.autoHideTime = mData.autoHideResult;
2024-12-14 18:27:59 +08:00
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().Start(this);
}
}
});
}
mResLoader.LoadAsync();
Scroll.verticalNormalizedPosition = 1;
}
public void SetSelected(GameObject item, bool isRight)
{
2025-01-10 15:49:30 +08:00
Transform icon = item.transform.Find("IconBg/Icon");
2024-12-16 15:58:37 +08:00
GameObject right = icon.Find("Right").gameObject;
GameObject wrong = icon.Find("Wrong").gameObject;
GameObject Selected = icon.Find("Selected").gameObject;
2024-12-14 18:27:59 +08:00
Selected.SetActive(true);
if (isRight)
{
right.SetActive(true);
wrong.SetActive(false);
}
else
{
right.SetActive(false);
wrong.SetActive(true);
}
2024-12-16 15:58:37 +08:00
icon.GetComponent<Image>().color = new Color(255f / 255f, 255f / 255f, 255f / 255f, 51f / 255f);
2024-12-14 18:27:59 +08:00
item.GetComponent<Button>().onClick.RemoveAllListeners();
}
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
//TypeEventSystem.Global.UnRegister<OnModuleQuit>(OnModuleQuit);
}
public void OnModuleQuit(OnModuleQuit arg)
{
Hide();
}
}
}